vendredi 17 février 2017

Coding standard- should renderer be in service or controller?

I'm not sure where should I put class responsible for rendering data. In controller or service? If it should be in service? Where should I invoke it? Also in service?

I have following classes:

Controller:

class ExampleController extends AbstractController
{
    public function __construct(Service $serviceExample)
    {
        $this->serviceExample = $serviceExample;
    }

    /**
     * Get list
     *
     * @return mixed
     */
    public function getList()
    {
        // code
        $renderer = $this->serviceExample->getRenderer(); // #1
        $list = $this->serviceExample->getList();
        $renderedList = $renderer->renderList($list);

        $result = [
            [
                "order" => 0,
                "list" => $renderedList
            ]
        ];

        $this->viewModel->setVariables($result);
        return $this->viewModel;
    }
}

Service:

class ServiceExample extends AbstractService
{
    /**
     * @param EntityManager $em
     * @param RendererInterface $renderer
     */
    public function __construct(EntityManager $em, RendererInterface $renderer)
    {
        parent::__construct($em, $renderer);
    }


    /**
     * Get list
     *
     * @return array
     */
    public function getList()
    {
        //SQL query

        return $result;
    }

    // renderer and em getters and setters
}

In every action of controller I have to do $renderer = $service->getRenderer() and then $result = $renderer->renderSomething(). Not sure if it's right way to to duplicate this steps for every action. On the other hand. Controllers should be tiny, so initialize many classes/services it's also not a good idea.

How should this work in perfect project?

Aucun commentaire:

Enregistrer un commentaire