infinito/application/src/Controller/SourceController.php

73 lines
2.2 KiB
PHP
Raw Normal View History

2018-09-05 15:46:14 +02:00
<?php
namespace App\Controller;
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-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-17 13:09:04 +02:00
public function modify(int $id): Response
2018-09-17 13:09:04 +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);
}
/**
*
* @Route("/source/{id}.{_format}/edit", defaults={"_format"="html"})
*/
public function edit(Request $request, int $id): Response
{
$source = $this->loadSource($request, $id);
$form = $this->createForm(NameSourceType::class, $source);
$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
}