src/EventSubscriber/MaintenanceElement/MaintenanceElementPostWriteSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 15/04/2019
  6.  * Time: 10:15 PM
  7.  */
  8. namespace App\EventSubscriber\MaintenanceElement;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use ApiPlatform\Core\EventListener\EventPriorities;
  15. use App\Repository\App\MaintenanceElementRepository;
  16. use App\Repository\App\MediaObjectRepository;
  17. use App\Entity\App\MaintenanceElement;
  18. use App\Services\UtilsService;
  19. class MaintenanceElementPostWriteSubscriber implements EventSubscriberInterface
  20. {
  21.     private $entityManager;
  22.     private $mediaObjectRepository;
  23.     private $elementRepository;
  24.     private $utilsService;
  25.     public function __construct(
  26.         MediaObjectRepository $mediaObjectRepository,
  27.         MaintenanceElementRepository $elementRepository,
  28.         EntityManagerInterface $entityManager,
  29.         UtilsService $utilsService)
  30.     {
  31.         $this->entityManager $entityManager;
  32.         $this->mediaObjectRepository $mediaObjectRepository;
  33.         $this->elementRepository $elementRepository;
  34.         $this->utilsService $utilsService;
  35.     }
  36.     /**
  37.      * @param ViewEvent $event
  38.      * @throws \Doctrine\Common\Persistence\Mapping\MappingException
  39.      * @throws \Doctrine\ORM\NonUniqueResultException
  40.      */
  41.     public function onKernelView(ViewEvent $event)
  42.     {
  43.         if ($this->utilsService->isAPublicRequest($event)) {
  44.             return;
  45.         }
  46.         $maintenanceElement $event->getControllerResult();
  47.         $request $event->getRequest();
  48.         $method $request->getMethod();
  49.         if (!($maintenanceElement instanceof MaintenanceElement) ||
  50.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
  51.         )
  52.             return;
  53.         $content $request->getContent();
  54.         $params json_decode($contenttrue);
  55.         if (isset($params['mediaContent']) &&
  56.             is_array($params['mediaContent']) &&
  57.             count($params['mediaContent']) > 0
  58.         ) {
  59.             if (!$this->entityManager->isOpen()) {
  60.                 $this->entityManager $this->entityManager->create(
  61.                     $this->entityManager->getConnection(),
  62.                     $this->entityManager->getConfiguration()
  63.                 );
  64.             }
  65.             $getMediaObjects $this->mediaObjectRepository->findBy(['maintenanceElement' => $maintenanceElement->getId()]);
  66.             foreach ($getMediaObjects as $media) {
  67.                 if (!in_array($media->getId(), $params['mediaContent'])){
  68.                     $this->entityManager->remove($media);
  69.                 }
  70.             }
  71.             foreach ($params['mediaContent'] as $mediaId) {
  72.                 $mediaObject $this->mediaObjectRepository->find($mediaId);
  73.                 $mediaObject->setMaintenanceElement($maintenanceElement);
  74.                 $this->entityManager->persist($mediaObject);
  75.             }
  76.             $this->entityManager->flush();
  77.         }
  78.         if(isset($params['children']) &&
  79.             is_array($params['children']) &&
  80.             count($params['children']) > 0
  81.         ){
  82.             if (!$this->entityManager->isOpen()) {
  83.                 $this->entityManager $this->entityManager->create(
  84.                     $this->entityManager->getConnection(),
  85.                     $this->entityManager->getConfiguration()
  86.                 );
  87.             }
  88.             foreach ($maintenanceElement->getChildren() as $child) {
  89.                 $search array_search($child->getId(), array_column($params['children'], 'id'));
  90.                 if ( !is_integer($search) && !$search ){
  91.                     $child->setParent(null);
  92.                     $this->entityManager->persist($child);
  93.                 }
  94.             }
  95.             foreach ($params['children'] as $child) {
  96.                 if (isset($child['id'])) {
  97.                     $children $this->elementRepository->find($child['id']);
  98.                     $children->setParent($maintenanceElement);
  99.                     $this->entityManager->persist($children);
  100.                 }
  101.             }
  102.             $this->entityManager->flush();
  103.         }
  104.         if (Request::METHOD_POST === $method) {
  105.             $identify $this->utilsService->generateIdentify($maintenanceElement);
  106.             $maintenanceElement->setIdentify($identify);
  107.             $this->entityManager->persist($maintenanceElement);
  108.             $this->entityManager->flush();
  109.         }
  110.     }
  111.     public static function getSubscribedEvents()
  112.     {
  113.         return [
  114.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_WRITE]
  115.         ];
  116.     }
  117. }