Implemented Draft of MVCRoutineService

This commit is contained in:
Kevin Frantz 2019-02-15 18:03:57 +01:00
parent 3e1590ffbf
commit 031bff568a
5 changed files with 113 additions and 10 deletions

View File

@ -4,7 +4,10 @@ namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface; use App\Domain\MVCManagement\MVCRoutineServiceInterface;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
use App\DBAL\Types\ActionType;
use App\Domain\FixtureManagement\FixtureSource\ImpressumFixtureSource;
/** /**
* This controller offers the standart routes for the template. * This controller offers the standart routes for the template.
@ -19,8 +22,12 @@ final class DefaultController extends AbstractController
* @todo Optimize function! * @todo Optimize function!
* @Route("/imprint.{_format}", defaults={"_format"="json"}, name="imprint") * @Route("/imprint.{_format}", defaults={"_format"="json"}, name="imprint")
*/ */
public function imprint(EntityManagerInterface $entityManager): Response public function imprint(MVCRoutineServiceInterface $mvcRoutineService, RequestedActionServiceInterface $requestedActionService): Response
{ {
$requestedActionService->setActionType(ActionType::READ);
$requestedActionService->getRequestedEntity()->setSlug(ImpressumFixtureSource::SLUG);
$view = $mvcRoutineService->process();
$this->handleView($view);
} }
/** /**

View File

@ -0,0 +1,32 @@
<?php
namespace App\Domain\ActionManagement;
/**
* @author kevinfrantz
*/
final class ActionHandlerService implements ActionHandlerServiceInterface
{
/**
* @var ActionFactoryServiceInterface
*/
private $actionFactoryService;
/**
* @param ActionFactoryServiceInterface $actionFactoryService
*/
public function __construct(ActionFactoryServiceInterface $actionFactoryService)
{
$this->actionFactoryService = $actionFactoryService;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\ActionHandlerServiceInterface::handle()
*/
public function handle()
{
return $this->actionFactoryService->create()->execute();
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Domain\ActionManagement;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
interface ActionHandlerServiceInterface
{
/**
* Process an action an returns the results.
*
* @return EntityInterface|EntityInterface[]
*/
public function handle();
}

View File

@ -3,19 +3,63 @@
namespace App\Domain\MVCManagement; namespace App\Domain\MVCManagement;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use App\Entity\EntityInterface;
use App\Domain\ActionManagement\ActionHandlerServiceInterface;
use App\Domain\TemplateManagement\TemplateNameServiceInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class MVCRoutineService implements MVCRoutineServiceInterface final class MVCRoutineService implements MVCRoutineServiceInterface
{ {
/**
* @var ActionHandlerServiceInterface
*/
private $actionHandlerService;
/**
* @var TemplateNameServiceInterface
*/
private $templateNameService;
/**
* @param EntityInterface[]|EntityInterface|null $result
*
* @return array Well formated data for view
*/
private function getViewData($result): array
{
switch (gettype($result)) {
case 'object':
return ['entity' => $this->result];
case 'array':
return ['enitits' => $this->result];
case 'null':
return [];
}
}
/**
* @param ActionHandlerServiceInterface $actionHandlerService
*/
public function __construct(ActionHandlerServiceInterface $actionHandlerService, TemplateNameServiceInterface $templateNameService)
{
$this->actionHandlerService = $actionHandlerService;
$this->templateNameService = $templateNameService;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::process() * @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::process()
*/ */
public function process(): void public function process(): View
{ {
$result = $this->actionHandlerService->handle();
$data = $this->getViewData($result);
$view = $this->getView($data);
return $view;
} }
/** /**
@ -23,7 +67,12 @@ final class MVCRoutineService implements MVCRoutineServiceInterface
* *
* @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::getView() * @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::getView()
*/ */
public function getView(): View public function getView(array $data): View
{ {
$view = View::create();
$view->setTemplate($this->templateNameService->getMoleculeTemplateName());
$view->setData($data);
return $view;
} }
} }

View File

@ -12,12 +12,9 @@ use FOS\RestBundle\View\View;
interface MVCRoutineServiceInterface interface MVCRoutineServiceInterface
{ {
/** /**
* Process the injected services. * Process through the layers.
*/ *
public function process(): void;
/**
* @return View * @return View
*/ */
public function getView(): View; public function process(): View;
} }