src/Controller/AdServerMappingController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ClickTrackerDomain;
  4. use App\Form\Type\ClickTrackerDomainType;
  5. use App\Model\AdServerMap\Report as AdServerReport;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * Ad Server Mapping Controller
  14.  *
  15.  * @Route(path="mapping")
  16.  */
  17. class AdServerMappingController extends AbstractController
  18. {
  19.     const MAPPING_ID           'mappingId';
  20.     const NOTIFICATION_SUCCESS 'notification-success';
  21.     /**
  22.      * @var AdServerReport
  23.      */
  24.     private AdServerReport $report;
  25.     /**
  26.      * constructor
  27.      *
  28.      * @param AdServerReport $report
  29.      */
  30.     public function __construct(AdServerReport $report)
  31.     {
  32.         $this->report $report;
  33.     }
  34.     /**
  35.      * AdServer-Domain Index.
  36.      *
  37.      * @Route(path="/", name="server_mapping_index", methods={"GET"})
  38.      *
  39.      * @return Response
  40.      */
  41.     public function indexAction(): Response
  42.     {
  43.         $lstMapping $this->report->getAll();
  44.         return $this->render('adserver/list.html.twig', [
  45.             'lstMapping' => $lstMapping,
  46.         ]);
  47.     }
  48.     /**
  49.      * AdServer-Domain New.
  50.      *
  51.      * @Route(path="/new", name="server_mapping_new", methods={"GET", "POST"})
  52.      *
  53.      * @param Request $request
  54.      * @return Response
  55.      */
  56.     public function newAction(Request $request): Response
  57.     {
  58.         $mapping = new ClickTrackerDomain();
  59.         $form    $this->createForm(ClickTrackerDomainType::class, $mapping);
  60.         $form->handleRequest($request);
  61.         $errors $this->report->validate($mapping);
  62.         if ($errors->count() === 0) {
  63.             if ($form->isSubmitted() && $form->isValid()) {
  64.                 $this->report->saveMapping($mapping);
  65.                 $this->addFlash(self::NOTIFICATION_SUCCESS'Mapping created');
  66.                 return $this->redirectToRoute('server_mapping_index');
  67.             }
  68.         }
  69.         return $this->renderForm('adserver/new.html.twig', [
  70.             'form' => $form,
  71.             'error' => $errors,
  72.         ]);
  73.     }
  74.     /**
  75.      * AdServer-Domain Edit.
  76.      *
  77.      * @Route(path="/edit/{id}", name="server_mapping_edit", methods={"GET", "POST"})
  78.      *
  79.      * @param Request $request
  80.      * @param int     $id
  81.      * @return Response
  82.      */
  83.     public function editAction(Request $requestint $id): Response
  84.     {
  85.         $mapping $this->report->getById($id);
  86.         if (empty($mapping)) {
  87.             throw new NotFoundHttpException('Mapping not found!');
  88.         }
  89.         $form $this->createForm(ClickTrackerDomainType::class, $mapping);
  90.         $form->handleRequest($request);
  91.         $errors $this->report->validate($mapping);
  92.         if ($errors->count() === 0) {
  93.             if ($form->isSubmitted() && $form->isValid()) {
  94.                 $this->report->saveMapping($mapping);
  95.                 $this->addFlash(self::NOTIFICATION_SUCCESS'Mapping updated');
  96.                 return $this->redirectToRoute('server_mapping_index');
  97.             }
  98.         }
  99.         return $this->renderForm('adserver/edit.html.twig', [
  100.             'form' => $form,
  101.             'error' => $errors,
  102.         ]);
  103.     }
  104.     /**
  105.      * AdServer-Domain New.
  106.      *
  107.      * @Route(path="/delete", name="server_mapping_delete", methods={"POST"})
  108.      *
  109.      * @param Request $request
  110.      * @return RedirectResponse
  111.      */
  112.     public function deleteAction(Request $request): RedirectResponse
  113.     {
  114.         $id $request->request->all()[self::MAPPING_ID] ?? null;
  115.         if (is_null($id)) {
  116.             throw new \InvalidArgumentException();
  117.         }
  118.         $mapping $this->report->getById($id);
  119.         if (empty($mapping)) {
  120.             throw new NotFoundHttpException('Mapping not found!');
  121.         }
  122.         $this->report->deleteMapping($mapping);
  123.         $this->addFlash(self::NOTIFICATION_SUCCESS'Mapping removed');
  124.         return $this->redirectToRoute('server_mapping_index');
  125.     }
  126. }