Adri HM
Adri HM

Reputation: 3090

I don't know how to implement a logic in symfony

I added a status to my Task Entity, and I will know which can be the best way to put the logic to set this status before send it to the front. Should be in the TaskController. Should be on the Services ? I don't think so but I'm not sure. I put a sample of the logic I want to add to set the status :

private function loadStatus(Task $task): string
    {
        $object = new \stdClass();

        $object->isDone = $task->isTaskDone();
        $object->isLate = date("Y-m-d") > $task->getDateStart();
        $object->isScheduled = date("Y-m-d") < $task->getDateStart() && !$task->isTaskDone();


        if ($object->isDone) {
                return 'task is done';
        }

        if ($object->isLate) {
            return 'task is late';
        }

        if ($object->isScheduled) {
            return 'task is scheduled'
        }
    }

Thank you for your help.

Upvotes: 1

Views: 46

Answers (1)

msg
msg

Reputation: 8181

You can put the logic in the entity itself, as additional methods; after all it's entity behavior, following doctrine's best practices.

class Task 
{
    const STATE_DONE = 'done';
    // Other constants ommited

    public function isScheduled()
    {
        return date("Y-m-d") < $this->getDateStart() && !$this->isTaskDone();
    }

    public function getStatus()
    {
        if ($this->isTaskDone()) {
                return self::STATE_DONE;
        }
        // Rest ommited 
    }
}

Then you can use them in the templates to generate the descriptions, where you have access to the translator.

{{ task.status | trans }}

Upvotes: 2

Related Questions