diff --git a/application/src/Controller/AbstractEntityController.php b/application/src/Controller/AbstractEntityController.php new file mode 100644 index 0000000..617785f --- /dev/null +++ b/application/src/Controller/AbstractEntityController.php @@ -0,0 +1,35 @@ +setEntityName(); + } + + abstract protected function setEntityName():void; + + protected function loadEntityById(int $id): EntityInterface + { + $entity = $this->getDoctrine() + ->getRepository($this->entityName) + ->find($id); + if (!$entity) { + throw $this->createNotFoundException('No entity found for id '.$id); + } + return $entity; + } +} \ No newline at end of file diff --git a/application/src/Controller/LawController.php b/application/src/Controller/LawController.php index 0638c22..c7e8512 100644 --- a/application/src/Controller/LawController.php +++ b/application/src/Controller/LawController.php @@ -1,17 +1,17 @@ entityName = Law::class; + } + } \ No newline at end of file diff --git a/application/src/Controller/NodeController.php b/application/src/Controller/NodeController.php index 993ecec..fb5f75b 100644 --- a/application/src/Controller/NodeController.php +++ b/application/src/Controller/NodeController.php @@ -2,7 +2,6 @@ namespace App\Controller; -use FOS\RestBundle\Controller\FOSRestController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -14,29 +13,25 @@ use App\Entity\Node; * * @author kevinfrantz */ -class NodeController extends FOSRestController +class NodeController extends AbstractEntityController { /** * @Route("/node/{id}.{_format}", defaults={"_format"="html"}) */ public function show(Request $request, int $id): Response { - $node = $this->loadSource($request, $id); + /** + * @var NodeInterface $node + */ + $node = $this->loadEntityById($id); $view = $this->view($node, 200) ->setTemplate('node/view/standard.html.twig') ->setTemplateVar('node'); - return $this->handleView($view); } - - private function loadSource(Request $request, int $id): NodeInterface + + protected function setEntityName(): void { - $node = $this->getDoctrine() - ->getRepository(Node::class) - ->find($id); - if (!$node) { - throw $this->createNotFoundException('No node found for id '.$id); - } - return $node; + $this->entityName = Node::class; } } diff --git a/application/src/Controller/SourceController.php b/application/src/Controller/SourceController.php index 1c4ec5f..5791acf 100644 --- a/application/src/Controller/SourceController.php +++ b/application/src/Controller/SourceController.php @@ -19,14 +19,14 @@ use App\Entity\Source\AbstractSource; * * @author kevinfrantz */ -class SourceController extends FOSRestController +class SourceController extends AbstractEntityController { /** * @Route("/source/{id}.{_format}", defaults={"_format"="html"}) */ public function show(Request $request, int $id): Response { - $source = $this->loadSource($request, $id); + $source = $this->loadEntityById($id); #$assembler = $this->get(SourceDTOAssember::class); #$dto = $assembler->build($source, $this->getUser()); $view = $this->view($source, 200) @@ -41,7 +41,7 @@ class SourceController extends FOSRestController */ public function edit(Request $request, int $id): Response { - $source = $this->loadSource($request, $id); + $source = $this->loadEntityById($id); $form = $this->createForm((new SourceFormFactory($source))->getNamespace(), $source); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -59,27 +59,20 @@ class SourceController extends FOSRestController */ public function node(Request $request, int $id): RedirectResponse { - $source = $this->loadSource($request, $id); - + $source = $this->loadEntityById($id); return $this->redirectToRoute('app_node_show', ['id' => $source->getNode()->getId()]); } - private function loadSource(Request $request, int $id): SourceInterface - { - $source = $this->getDoctrine() - ->getRepository(AbstractSource::class) - ->find($id); - if (!$source) { - throw $this->createNotFoundException('No source found for id '.$id); - } - - return $source; - } - private function saveSource(SourceInterface $source): void { $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($source); $entityManager->flush(); } + + protected function setEntityName(): void + { + $this->entityName = AbstractSource::class; + } + } diff --git a/application/src/Entity/AbstractEntity.php b/application/src/Entity/AbstractEntity.php index d234338..c6541cf 100644 --- a/application/src/Entity/AbstractEntity.php +++ b/application/src/Entity/AbstractEntity.php @@ -8,7 +8,7 @@ use Doctrine\ORM\Mapping as ORM; /** * @author kevinfrantz */ -class AbstractEntity +abstract class AbstractEntity implements EntityInterface { use IdAttribut; diff --git a/application/src/Entity/EntityInterface.php b/application/src/Entity/EntityInterface.php new file mode 100644 index 0000000..46f551b --- /dev/null +++ b/application/src/Entity/EntityInterface.php @@ -0,0 +1,12 @@ +