infinito/application/src/Controller/AbstractEntityController.php

45 lines
972 B
PHP
Raw Normal View History

2018-10-03 18:55:33 +02:00
<?php
2018-10-29 19:01:00 +01:00
2018-10-03 18:55:33 +02:00
namespace App\Controller;
use App\Entity\EntityInterface;
use FOS\RestBundle\Controller\FOSRestController;
2018-10-04 20:48:12 +02:00
use Symfony\Component\HttpFoundation\RedirectResponse;
2018-10-03 18:55:33 +02:00
/**
* @author kevinfrantz
*/
abstract class AbstractEntityController extends FOSRestController
{
/**
* @var string
*/
protected $entityName;
2018-10-29 19:01:00 +01:00
public function __construct()
{
2018-10-03 18:55:33 +02:00
$this->setEntityName();
}
2018-10-29 19:01:00 +01:00
abstract protected function setEntityName(): void;
2018-10-03 18:55:33 +02:00
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);
}
2018-10-29 19:01:00 +01:00
2018-10-03 18:55:33 +02:00
return $entity;
}
2018-10-29 19:01:00 +01:00
2018-10-04 20:48:12 +02:00
protected function redirectToRouteById(string $route, int $id): RedirectResponse
{
return $this->redirectToRoute($route, [
2018-10-29 19:01:00 +01:00
'id' => $id,
2018-10-04 20:48:12 +02:00
]);
}
2018-10-29 19:01:00 +01:00
}