2019-02-15 16:55:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Domain\MVCManagement;
|
|
|
|
|
|
|
|
use FOS\RestBundle\View\View;
|
2019-02-15 18:03:57 +01:00
|
|
|
use App\Entity\EntityInterface;
|
|
|
|
use App\Domain\ActionManagement\ActionHandlerServiceInterface;
|
|
|
|
use App\Domain\TemplateManagement\TemplateNameServiceInterface;
|
2019-02-15 16:55:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author kevinfrantz
|
|
|
|
*/
|
|
|
|
final class MVCRoutineService implements MVCRoutineServiceInterface
|
|
|
|
{
|
2019-02-15 18:03:57 +01:00
|
|
|
/**
|
|
|
|
* @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':
|
2019-02-16 18:02:28 +01:00
|
|
|
return ['entity' => $result];
|
2019-02-15 18:03:57 +01:00
|
|
|
case 'array':
|
2019-02-16 18:02:28 +01:00
|
|
|
return ['entities' => $result];
|
2019-02-15 18:03:57 +01:00
|
|
|
case 'null':
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ActionHandlerServiceInterface $actionHandlerService
|
|
|
|
*/
|
|
|
|
public function __construct(ActionHandlerServiceInterface $actionHandlerService, TemplateNameServiceInterface $templateNameService)
|
|
|
|
{
|
|
|
|
$this->actionHandlerService = $actionHandlerService;
|
|
|
|
$this->templateNameService = $templateNameService;
|
|
|
|
}
|
|
|
|
|
2019-02-15 16:55:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::process()
|
|
|
|
*/
|
2019-02-15 18:03:57 +01:00
|
|
|
public function process(): View
|
2019-02-15 16:55:49 +01:00
|
|
|
{
|
2019-02-15 18:03:57 +01:00
|
|
|
$result = $this->actionHandlerService->handle();
|
|
|
|
$data = $this->getViewData($result);
|
|
|
|
$view = $this->getView($data);
|
|
|
|
|
|
|
|
return $view;
|
2019-02-15 16:55:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::getView()
|
|
|
|
*/
|
2019-02-15 18:03:57 +01:00
|
|
|
public function getView(array $data): View
|
2019-02-15 16:55:49 +01:00
|
|
|
{
|
2019-02-15 18:03:57 +01:00
|
|
|
$view = View::create();
|
|
|
|
$view->setTemplate($this->templateNameService->getMoleculeTemplateName());
|
|
|
|
$view->setData($data);
|
|
|
|
|
|
|
|
return $view;
|
2019-02-15 16:55:49 +01:00
|
|
|
}
|
|
|
|
}
|