diff --git a/application/src/Controller/DefaultController.php b/application/src/Controller/DefaultController.php index 4abe19c..fe441ba 100644 --- a/application/src/Controller/DefaultController.php +++ b/application/src/Controller/DefaultController.php @@ -6,13 +6,12 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Response; use App\DBAL\Types\SystemSlugType; use App\Entity\Source\AbstractSource; -use App\Domain\SecureLoadManagement\SecureSourceLoader; use App\Entity\Meta\Right; use App\DBAL\Types\LayerType; use App\DBAL\Types\RightType; -use App\Domain\UserManagement\UserIdentityManager; use Doctrine\ORM\EntityManagerInterface; use App\Entity\Source\PureSource; +use App\Domain\ResponseManagement\SourceRESTResponseManager; /** * This controller offers the standart routes for the template. @@ -23,25 +22,19 @@ final class DefaultController extends AbstractEntityController { /** * @todo Optimize function! - * @Route("/imprint", name="imprint") + * @Route("/imprint.{_format}", defaults={"_format"="json"}, name="imprint") */ public function imprint(EntityManagerInterface $entityManager): Response { - $userIdentityManager = new UserIdentityManager($entityManager, $this->getUser()); - $user = $userIdentityManager->getUser(); $requestedSource = new PureSource(); $requestedSource->setSlug(SystemSlugType::IMPRINT); $requestedRight = new Right(); $requestedRight->setSource($requestedSource); - $requestedRight->setReciever($user->getSource()); $requestedRight->setLayer(LayerType::SOURCE); $requestedRight->setType(RightType::READ); - $secureSourceLoader = new SecureSourceLoader($this->getDoctrine()->getManager(), $requestedRight); - $view = $this->view($secureSourceLoader->getSource(), 200) - ->setTemplate('standard/imprint.html.twig') - ->setTemplateVar('source'); + $sourceResponseManager = new SourceRESTResponseManager($this->getUser(), $entityManager, $requestedRight, $this->getViewHandler()); - return $this->handleView($view); + return $sourceResponseManager->getResponse(); } /** diff --git a/application/src/Domain/AbstractDomainService.php b/application/src/Domain/AbstractDomainService.php index 6a0381c..ff5dee7 100644 --- a/application/src/Domain/AbstractDomainService.php +++ b/application/src/Domain/AbstractDomainService.php @@ -2,6 +2,11 @@ namespace App\Domain; +/** + * @deprecated Doesn't make sense! + * + * @author kevinfrantz + */ abstract class AbstractDomainService { } diff --git a/application/src/Domain/ResponseManagement/SourceRESTResponseManager.php b/application/src/Domain/ResponseManagement/SourceRESTResponseManager.php new file mode 100644 index 0000000..6f8b96a --- /dev/null +++ b/application/src/Domain/ResponseManagement/SourceRESTResponseManager.php @@ -0,0 +1,107 @@ +entityManager = $entityManager; + $this->viewHandler = $viewHandler; + $this->setUser($user); + $this->setRequestedRight($requestedRight); + $this->setLoadedSource(); + $this->setView(); + } + + protected function setView(): void + { + $this->view = new View($this->loadedSource, 200); + } + + private function setLoadedSource(): void + { + $secureSourceLoader = new SecureSourceLoader($this->entityManager, $this->requestedRight); + $this->loadedSource = $secureSourceLoader->getSource(); + } + + /** + * @param UserInterface $user + */ + private function setUser(?UserInterface $user): void + { + $userIdentityManager = new UserIdentityManager($this->entityManager, $user); + $this->user = $userIdentityManager->getUser(); + } + + /** + * @param RightInterface $requestedRight + * + * @throws AllreadyDefinedException + */ + private function setRequestedRight(RightInterface $requestedRight): void + { + try { + $requestedRight->getReciever(); + throw new AllreadyDefinedException('The reciever is allready defined.'); + } catch (\TypeError $error) { + $requestedRight->setReciever($this->user->getSource()); + $this->requestedRight = $requestedRight; + } + } + + public function getResponse(): Response + { + return $this->viewHandler->handle($this->view); + } +} diff --git a/application/src/Domain/ResponseManagement/SourceRESTResponseManagerInterface.php b/application/src/Domain/ResponseManagement/SourceRESTResponseManagerInterface.php new file mode 100644 index 0000000..25cc7bd --- /dev/null +++ b/application/src/Domain/ResponseManagement/SourceRESTResponseManagerInterface.php @@ -0,0 +1,13 @@ + -{% trans %} -Imprint -{% endtrans %} - -{{ source.text }} -{% endblock %} diff --git a/application/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php b/application/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php new file mode 100644 index 0000000..fece7f2 --- /dev/null +++ b/application/tests/Unit/Domain/ResponseManagement/SourceRESTReponseManagerTest.php @@ -0,0 +1,75 @@ +requestedRight = new Right(); + } + + private function setEntityManager(): void + { + $this->entityManager = self::$container->get('doctrine.orm.default_entity_manager'); + } + + private function setViewHandler(): void + { + $this->viewHandler = $this->createMock(ViewHandlerInterface::class); + } + + public function setUp(): void + { + self::bootKernel(); + $this->setEntityManager(); + $this->setRequestedRight(); + $this->setViewHandler(); + } + + public function testAllreadyDefinedException(): void + { + $requestedSource = new PureSource(); + $requestedSource->setSlug(SystemSlugType::IMPRINT); + $requestedRight = new Right(); + $requestedRight->setSource($requestedSource); + $requestedRight->setReciever(new PureSource()); + $requestedRight->setLayer(LayerType::SOURCE); + $requestedRight->setType(RightType::READ); + $this->expectException(AllreadyDefinedException::class); + $sourceResponseManager = new SourceRESTResponseManager(null, $this->entityManager, $requestedRight, $this->viewHandler); + $sourceResponseManager->getResponse(); + } +}