2018-09-05 15:46:14 +02:00
|
|
|
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2018-09-05 18:17:04 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2018-09-14 16:05:47 +02:00
|
|
|
use App\Entity\AbstractSource;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2018-09-17 13:09:04 +02:00
|
|
|
use App\Creator\Factory\Template\Source\SourceTemplateFactory;
|
2018-09-16 17:51:43 +02:00
|
|
|
use FOS\RestBundle\Controller\FOSRestController;
|
2018-09-17 13:09:04 +02:00
|
|
|
use App\Form\NameSourceType;
|
|
|
|
use App\Entity\SourceInterface;
|
|
|
|
use App\Creator\Factory\Template\Source\SourceTemplateFormFactory;
|
2018-09-17 13:38:38 +02:00
|
|
|
use App\Creator\Factory\Form\Source\SourceFormFactory;
|
2018-09-05 18:17:04 +02:00
|
|
|
|
2018-09-05 15:46:14 +02:00
|
|
|
/**
|
2018-09-17 13:09:04 +02:00
|
|
|
* @todo IMPLEMENT SECURITY!
|
2018-09-05 15:46:14 +02:00
|
|
|
* @author kevinfrantz
|
|
|
|
*/
|
2018-09-16 17:51:43 +02:00
|
|
|
class SourceController extends FOSRestController
|
2018-09-05 15:46:14 +02:00
|
|
|
{
|
2018-09-14 16:05:47 +02:00
|
|
|
/**
|
2018-09-17 13:09:04 +02:00
|
|
|
*
|
2018-09-14 16:05:47 +02:00
|
|
|
* @Route("/source/{id}.{_format}", defaults={"_format"="html"})
|
|
|
|
*/
|
|
|
|
public function show(Request $request, int $id): Response
|
2018-09-17 13:09:04 +02:00
|
|
|
{
|
|
|
|
$source = $this->loadSource($request, $id);
|
|
|
|
$view = $this->view($source, 200)
|
|
|
|
->setTemplate((new SourceTemplateFactory($source, $request))->getTemplatePath())
|
|
|
|
->setTemplateVar('source');
|
|
|
|
return $this->handleView($view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2018-09-17 14:04:58 +02:00
|
|
|
* @Route("/source/{id}/edit.{_format}", defaults={"_format"="html"})
|
2018-09-17 13:09:04 +02:00
|
|
|
*/
|
|
|
|
public function edit(Request $request, int $id): Response
|
|
|
|
{
|
|
|
|
$source = $this->loadSource($request, $id);
|
2018-09-17 13:38:38 +02:00
|
|
|
$form = $this->createForm((new SourceFormFactory($source))->getNamespace(), $source);
|
2018-09-17 13:09:04 +02:00
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$source = $form->getData();
|
|
|
|
$this->saveSource($source);
|
|
|
|
}
|
|
|
|
return $this->render((new SourceTemplateFormFactory($source, $request))->getTemplatePath(), array(
|
|
|
|
'form' => $form->createView(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadSource(Request $request, int $id): SourceInterface
|
2018-09-12 22:25:22 +02:00
|
|
|
{
|
2018-09-14 16:05:47 +02:00
|
|
|
$source = $this->getDoctrine()
|
|
|
|
->getRepository(AbstractSource::class)
|
|
|
|
->find($id);
|
2018-09-17 13:09:04 +02:00
|
|
|
if (! $source) {
|
|
|
|
throw $this->createNotFoundException('No source found for id ' . $id);
|
2018-09-14 16:05:47 +02:00
|
|
|
}
|
2018-09-17 13:09:04 +02:00
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function saveSource(SourceInterface $source): void
|
|
|
|
{
|
|
|
|
$entityManager = $this->getDoctrine()->getManager();
|
|
|
|
$entityManager->persist($source);
|
|
|
|
$entityManager->flush();
|
2018-09-12 22:25:22 +02:00
|
|
|
}
|
2018-09-05 15:46:14 +02:00
|
|
|
}
|