src/EventSubscriber/RichFieldSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\PredefinedColor;
  4. use DateTime;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Exception;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class RichFieldSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     protected $entityManager;
  17.     public function __construct(EntityManagerInterface $entityManager)
  18.     {
  19.         $this->entityManager $entityManager;
  20.     }
  21.     /**
  22.      * @param RequestEvent $event
  23.      *
  24.      * @throws Exception
  25.      */
  26.     public function onKernelRequest(RequestEvent $event)
  27.     {
  28.         $richType $event->getRequest()->get('richType');
  29.         if ($richType) {
  30.             $value $event->getRequest()->request->get('value');
  31.             if (!$value instanceof $richType) {
  32.                 $colorId = (int)$value['color'];
  33.                 $color $this->entityManager->getRepository(PredefinedColor::class)->find(
  34.                     ($colorId $colorId 1)
  35.                 );
  36.                 $value = new $richType(new DateTime($value['value']), $color);
  37.             }
  38.             $event->getRequest()->request->set('value'$value);
  39.         }
  40.     }
  41.     /**
  42.      * @return array
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  48.         ];
  49.     }
  50. }