mardi 8 mars 2022

Struggle with interfaces multiple implements service symfony 5.4 [closed]

I'm trying to work with interfaces on a symfony 5.4 project.

I would like to call in my AnswerManager the interface: AnswerInterface without the ugly AnswerDispatch whose dispatching with a switch depending on the type of the Answer.

I would like to make my code more SOLID. Especially that I might have other type later that will implement the same Interface.

Could you help me ? What design pattern should I use and how ?

Thanks in advance.

Here is my code :

AnswerController.php

    /**
     * @Route("/edit-answer/{process}/{grid}/{press<\d+>?null}", name="edit_answer", options={"expose"=true})
     * @param Request $request
     * @param Process $process
     * @param Grid $grid
     * @param Press|null $press
     * @return Response
     */
    public function edit(Request $request, Process $process, Grid $grid, ?Press $press): Response
    {
        if ($request->isXmlHttpRequest()) {
            $answer = $this->answerManager->getAnswerIdByProcess($process, $grid, $press);

            return new JsonResponse([
                'answer' => $answer->getId(),
            ]);
        }

        return $this->render("lpa/{$this->getTypeName($grid->getType())}/grid/create_grid.html.twig", [
            'grid' => $grid,
            'press' => $press
        ]);
    }

AnswerManager.php

    /**
     * @var AnswerDispatch
     */
    private $answerDispatch;

    public function __construct(
        AnswerDispatch $answerDispatch
    )
    {
        $this->answerDispatch = $answerDispatch;
    }

    /**
     * @param Process $process
     * @param Grid $grid
     * @param Press|null $press
     * @return int|mixed|string
     */
    public function getAnswerIdByProcess(Process $process, Grid $grid, Press $press = null)
    {
        $answerManager = $this->answerDispatch->getAnswerManager($grid->getType());

        return $answerManager->getAnswerIdByProcess($process, $grid, $press);
    }

AnswerDispatch.php

class AnswerDispatch
{
    /**
     * @var AnswerLogisticManager
     */
    private $answerLogisticManager;

    /**
     * @var AnswerFabManager
     */
    private $answerFabManager;

    public function __construct(
        AnswerLogisticManager $answerLogisticManager,
        AnswerFabManager $answerFabManager,
    )
    {
        $this->answerLogisticManager = $answerLogisticManager;
        $this->answerFabManager = $answerFabManager;
    }

    /**
     * @param int $type
     * @return object
     */
    public function getAnswerManager(int $type): object
    {
        switch ($type) {
            case GlobalConstant::LPA_LOGISTIC :
                return $this->answerLogisticManager;
            default :
                return $this->answerFabManager;
        }
    }

AnswerLogisticManager.php

class AnswerLogisticManager implements AnswerInterface
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    public function __construct(
        EntityManagerInterface $entityManager
    )
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @param array $answersByThemes
     * @return array
     */
    public function getThemes(array $answersByThemes): array
    {
********
    }

    /**
     * @param Process $process
     * @param Grid $grid
     * @param Press|null $press
     * @return int|mixed|string
     */
    public function getAnswerIdByProcess(Process $process, Grid $grid, Press $press = null)
    {
        return $this->entityManager->getRepository(Answer::class)->getAnswerIdByProcess($process, $grid);
    }
}

AnswerFabManager.php

class AnswerFabManager implements AnswerInterface
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    public function __construct(
        EntityManagerInterface $entityManager
    )
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @param array $answersByThemes
     * @return array
     */
    public function getThemes(array $answersByThemes): array
    {
******
    }

    /**
     * @param Process $process
     * @param Grid $grid
     * @param Press|null $press
     * @return int|mixed|string
     */
    public function getAnswerIdByProcess(Process $process, Grid $grid, Press $press = null)
    {
        return $this->entityManager->getRepository(Answer::class)->getAnswerIdByProcess($process, $grid, $press);
    }

AnswerInterface.php

interface AnswerInterface
{
    /**
     * @param array $answersByThemes
     * @return array
     */
    public function getThemes(array $answersByThemes): array;

    /**
     * @param Process $process
     * @param Grid $grid
     * @param Press|null $press
     * @return int|mixed|string
     */
    public function getAnswerIdByProcess(Process $process, Grid $grid, Press $press = null);
}

Aucun commentaire:

Enregistrer un commentaire