src/EventListener/SaveProvidedDataEventSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\JvnTask;
  5. use App\Entity\JvnWinteringTemplateChapter;
  6. use App\Handler\DataPersisterHandler;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class SaveProvidedDataEventSubscriber implements EventSubscriberInterface
  14. {
  15.     private DataPersisterHandler $dataPersisterHandler;
  16.     
  17.     private EntityManagerInterface $entityManager;
  18.     public function __construct(DataPersisterHandler $dataPersisterHandlerEntityManagerInterface $entityManager)
  19.     {
  20.         $this->setDataPersisterHandler($dataPersisterHandler)->setEntityManager($entityManager);
  21.     }
  22.     /**
  23.      * @inheritDoc
  24.      */
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         /**
  28.          * Just after the data provider recovers data from database and before deserialization
  29.          * In this point we've got the current database object without modifications
  30.          */
  31.         return [
  32.             KernelEvents::VIEW => ['saveData'EventPriorities::POST_VALIDATE],
  33.         ];
  34.     }
  35.     /**
  36.      * @param ViewEvent $event
  37.      * @noinspection PhpUnused Called dynamically as Event Handler
  38.      */
  39.     public function saveData(ViewEvent $event): void
  40.     {
  41.         if( $event->getRequest()->attributes->get('_route') == 'api_jvn_wintering_template_chapters_patch_item' ) {
  42.             $data json_decode($event->getRequest()->getContent(), JSON_OBJECT_AS_ARRAY);
  43.             if( array_key_exists('checkPoints'$data) && is_array($data['checkPoints']) ) {
  44.                 $checkPointsOrder = new ArrayCollection();
  45.                 $position 1;
  46.                 foreach($data['checkPoints'] as $checkPoint ) {
  47.                     if( array_key_exists('checkPoint'$checkPoint) ) {
  48.                         $key str_replace('/''|'substr($checkPoint['checkPoint'], 1));
  49.                     } elseif( array_key_exists('id'$checkPoint) ) {
  50.                         $key str_replace('/''|'substr($checkPoint['id'], 1));
  51.                     } else {
  52.                         throw new BadRequestHttpException('Contenido invalido');
  53.                     }
  54.                     $checkPointsOrder->set($key$position++);
  55.                 }
  56.                 $this->getDataPersisterHandler()->setCheckPointsOrder($checkPointsOrder);
  57.             }
  58.         } elseif(
  59.             in_array($event->getRequest()->getMethod(), ['PUT''PATCH']) &&
  60.             ($data $event->getRequest()->attributes->get('previous_data')) instanceof JvnTask
  61.         ) {
  62.             // Clone and save the original object for further use and checks.
  63.             $this->getDataPersisterHandler()->setPreviousTask($data);
  64.         }
  65.     }
  66.     /**
  67.      * @return DataPersisterHandler
  68.      */
  69.     public function getDataPersisterHandler(): DataPersisterHandler
  70.     {
  71.         return $this->dataPersisterHandler;
  72.     }
  73.     /**
  74.      * @param DataPersisterHandler $dataPersisterHandler
  75.      * @return SaveProvidedDataEventSubscriber
  76.      */
  77.     public function setDataPersisterHandler(DataPersisterHandler $dataPersisterHandler): SaveProvidedDataEventSubscriber
  78.     {
  79.         $this->dataPersisterHandler $dataPersisterHandler;
  80.         return $this;
  81.     }
  82.     /**
  83.      * @return EntityManagerInterface
  84.      */
  85.     public function getEntityManager(): EntityManagerInterface
  86.     {
  87.         return $this->entityManager;
  88.     }
  89.     /**
  90.      * @param EntityManagerInterface $entityManager
  91.      * @return SaveProvidedDataEventSubscriber
  92.      */
  93.     public function setEntityManager(EntityManagerInterface $entityManager): SaveProvidedDataEventSubscriber
  94.     {
  95.         $this->entityManager $entityManager;
  96.         return $this;
  97.     }
  98. }