mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 14:07:25 +01:00
Added AbstractEntityController
This commit is contained in:
parent
94bc8c5da4
commit
af860429b9
35
application/src/Controller/AbstractEntityController.php
Normal file
35
application/src/Controller/AbstractEntityController.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\EntityInterface;
|
||||
use FOS\RestBundle\Controller\FOSRestController;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
abstract class AbstractEntityController extends FOSRestController
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $entityName;
|
||||
|
||||
public function __construct(){
|
||||
$this->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;
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
<?php
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Entity\Law;
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
class LawController extends AbstractController
|
||||
class LawController extends AbstractEntityController
|
||||
{
|
||||
/**
|
||||
* @Route("/law/{id}.{_format}", defaults={"_format"="html"})
|
||||
@ -33,4 +33,10 @@ class LawController extends AbstractController
|
||||
public function node(int $id):RedirectResponse{
|
||||
//Implement
|
||||
}
|
||||
|
||||
protected function setEntityName(): void
|
||||
{
|
||||
$this->entityName = Law::class;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use Doctrine\ORM\Mapping as ORM;
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class AbstractEntity
|
||||
abstract class AbstractEntity implements EntityInterface
|
||||
{
|
||||
use IdAttribut;
|
||||
|
||||
|
12
application/src/Entity/EntityInterface.php
Normal file
12
application/src/Entity/EntityInterface.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace App\Entity;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
interface EntityInterface
|
||||
{
|
||||
}
|
||||
|
@ -3,11 +3,12 @@ namespace App\Entity\Source;
|
||||
|
||||
use App\Entity\Attribut\NodeAttributInterface;
|
||||
use App\Entity\Attribut\IdAttributInterface;
|
||||
use App\Entity\EntityInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
interface SourceInterface extends IdAttributInterface, NodeAttributInterface
|
||||
interface SourceInterface extends IdAttributInterface, NodeAttributInterface, EntityInterface
|
||||
{
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user