Optimized Controller Draft

This commit is contained in:
Kevin Frantz 2019-01-05 21:26:36 +01:00
parent bc9c8f02a9
commit 609f156bfd
9 changed files with 93 additions and 97 deletions

View File

@ -2,11 +2,11 @@
namespace App\Controller\API;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Controller\AbstractController;
/**
* @author kevinfrantz
*/
abstract class AbstractAPIController extends Controller implements APIControllerInterface
abstract class AbstractAPIController extends AbstractController implements APIControllerInterface
{
}

View File

@ -6,6 +6,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Controller\API\AbstractAPIController;
use App\Entity\Source\PureSource;
/**
* @author kevinfrantz
@ -28,7 +29,7 @@ class SourceApiController extends AbstractAPIController
/**
* @Route("/{_locale}/api/source.{_format}",
* defaults={"_format"="json"} ,
* methods={"POST"}
* methods={"POST","GET"}
* )
* {@inheritdoc}
*
@ -36,6 +37,27 @@ class SourceApiController extends AbstractAPIController
*/
public function create(Request $request): Response
{
if (Request::METHOD_POST === $request->getMethod()) {
$response = new Response();
$response->setContent('Post Request!');
return $response;
}
$response = new Response();
$response->setContent('GET Request!');
return $response;
$requestedSource = new PureSource();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$requestedRight = new Right();
$requestedRight->setSource($requestedSource);
$requestedRight->setLayer(LayerType::SOURCE);
$requestedRight->setType(CRUDType::READ);
$sourceResponseManager = new SourceRESTResponseManager($this->getUser(), $entityManager, $requestedRight, $this->getViewHandler());
return $sourceResponseManager->getResponse();
}
/**

View File

@ -0,0 +1,12 @@
<?php
namespace App\Controller;
use FOS\RestBundle\Controller\FOSRestController;
/**
* @author kevinfrantz
*/
abstract class AbstractController extends FOSRestController
{
}

View File

@ -1,88 +0,0 @@
<?php
namespace App\Controller;
use App\Entity\EntityInterface;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* @todo Check which of the as deprecated declared functions make sense to remove
*
* @author kevinfrantz
*/
abstract class AbstractEntityController extends FOSRestController
{
/**
* @deprecated
*
* @var string
*/
protected $entityName;
/**
* @deprecated
*/
public function __construct()
{
$this->setEntityName();
}
/**
* @deprecated
*/
abstract protected function setEntityName(): void;
/**
* @deprecated
*
* @param int $id
*
* @return EntityInterface
*/
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;
}
/**
* @deprecated
*
* @param string $slug
*
* @return EntityInterface
*/
protected function loadEntityBySlug(string $slug): EntityInterface
{
$entity = $this->getDoctrine()
->getRepository($this->entityName)
->findOneBy(['slug' => $slug]);
if (!$entity) {
throw $this->createNotFoundException('No entity found for slug '.$slug);
}
return $entity;
}
/**
* @deprecated
*
* @param string $route
* @param int $id
*
* @return RedirectResponse
*/
protected function redirectToRouteById(string $route, int $id): RedirectResponse
{
return $this->redirectToRoute($route, [
'id' => $id,
]);
}
}

View File

@ -18,7 +18,7 @@ use App\Domain\ResponseManagement\SourceRESTResponseManager;
*
* @author kevinfrantz
*/
final class DefaultController extends AbstractEntityController
final class DefaultController extends AbstractController
{
/**
* @todo Optimize function!

View File

@ -0,0 +1,22 @@
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
/**
* Offers an static webpage with bootstrap.
*
* @see https://getbootstrap.com/
*
* @author kevinfrantz
*
* @todo Write tests!
*/
final class HtmlController extends AbstractController
{
public function html(Request $request): Response
{
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
/**
* Offers an SPA with Vue.js.
*
* @see https://vuejs.org/
* @see https://de.wikipedia.org/wiki/Single-Page-Webanwendung
*
* @author kevinfrantz
*
* @todo Write tests!
*/
class SPAController extends AbstractController
{
public function spa(Request $request): Response
{
}
}

View File

@ -5,7 +5,10 @@ namespace Tests\Integration\Controller;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
class UrlIntegrationTest extends KernelTestCase
/**
* @author kevinfrantz
*/
class RoutesGetStatusIntegrationTest extends KernelTestCase
{
const GET_URLS_STATUS = [
'login' => 200,
@ -13,6 +16,7 @@ class UrlIntegrationTest extends KernelTestCase
'register' => 301,
'logout' => 302,
'profile/edit' => 302,
'spa' => 200,
];
public function setUp(): void

View File

@ -1,6 +1,6 @@
<?php
namespace Tests\Integration\Controller\API;
namespace Tests\Integration\Controller;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\DBAL\Types\LanguageType;
@ -13,14 +13,14 @@ use Symfony\Component\HttpFoundation\Request;
*
* @todo Implement more tests for success etc.
*/
class ApiUrlReachableIntegrationTest extends KernelTestCase
class RoutesReachableIntegrationTest extends KernelTestCase
{
public function setUp(): void
{
self::bootKernel();
}
public function testAllAPIRoutePossibilities()
public function testAllRoutePossibilities()
{
foreach (LayerType::getChoices() as $layer => $layerDescription) {
$this->controller($layer);
@ -52,8 +52,9 @@ class ApiUrlReachableIntegrationTest extends KernelTestCase
private function language(string $entity, string $method): void
{
//$this->type('api/'.$entity, $method);
foreach (LanguageType::getChoices() as $language => $value) {
foreach (LanguageType::getChoices() as $language) {
$this->type("$language/api/$entity", $method);
$this->type("$language/html/$entity", $method);
}
}