src/EventSubscriber/Location/LocationPostWriteSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 26/04/2019
  6.  * Time: 11:56 AM
  7.  */
  8. namespace App\EventSubscriber\Location;
  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\LocationRepository;
  17. use App\Repository\App\MediaObjectRepository;
  18. use App\Repository\App\ResourceRepository;
  19. use App\Repository\App\UserRepository;
  20. use App\Repository\App\VendorStaffRepository;
  21. use App\Entity\App\Location;
  22. use App\Entity\App\Role;
  23. use App\Services\VendorService;
  24. use App\Services\UtilsService;
  25. class LocationPostWriteSubscriber implements EventSubscriberInterface
  26. {
  27.     private $entityManager;
  28.     private $mediaObjectRepository;
  29.     private $locationRepository;
  30.     private $elementRepository;
  31.     private $resourceRepository;
  32.     private $userRepository;
  33.     private $vendorStaffRepository;
  34.     private $vendorService;
  35.     private $utilsService;
  36.     public function __construct(
  37.         MediaObjectRepository $mediaObjectRepository,
  38.         LocationRepository $locationRepository,
  39.         MaintenanceElementRepository $elementRepository,
  40.         ResourceRepository $resourceRepository,
  41.         UserRepository $userRepository,
  42.         VendorStaffRepository $vendorStaffRepository,
  43.         VendorService $vendorService,
  44.         UtilsService $utilsService,
  45.         EntityManagerInterface $entityManager)
  46.     {
  47.         $this->entityManager $entityManager;
  48.         $this->mediaObjectRepository $mediaObjectRepository;
  49.         $this->locationRepository $locationRepository;
  50.         $this->elementRepository $elementRepository;
  51.         $this->resourceRepository $resourceRepository;
  52.         $this->userRepository $userRepository;
  53.         $this->vendorStaffRepository $vendorStaffRepository;
  54.         $this->vendorService $vendorService;
  55.         $this->utilsService $utilsService;
  56.     }
  57.     /**
  58.      * @param ViewEvent $event
  59.      * @throws \Doctrine\Common\Persistence\Mapping\MappingException
  60.      * @throws \Doctrine\ORM\NonUniqueResultException
  61.      */
  62.     public function onKernelView(ViewEvent $event)
  63.     {
  64.         $location $event->getControllerResult();
  65.         $request $event->getRequest();
  66.         $method $request->getMethod();
  67.         if (!($location instanceof Location) ||
  68.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
  69.         )
  70.             return;
  71.         $content $request->getContent();
  72.         $params json_decode($contenttrue);
  73.         if (!$this->entityManager->isOpen()) {
  74.             $this->entityManager $this->entityManager->create(
  75.                 $this->entityManager->getConnection(),
  76.                 $this->entityManager->getConfiguration()
  77.             );
  78.         }
  79.         if (isset($params['mediaContent']) &&
  80.             is_array($params['mediaContent'])
  81.         ) {
  82.             $getMediaObjects $this->mediaObjectRepository->findBy(['location' => $location->getId()]);
  83.             foreach ($getMediaObjects as $media) {
  84.                 if (!in_array($media->getId(), $params['mediaContent'])){
  85.                     $this->entityManager->remove($media);
  86.                 }
  87.             }
  88.             foreach ($params['mediaContent'] as $mediaId) {
  89.                 $mediaObject $this->mediaObjectRepository->find($mediaId);
  90.                 $mediaObject->setLocation($location);
  91.                 $this->entityManager->persist($mediaObject);
  92.             }
  93.             $this->entityManager->flush();
  94.         }
  95.         /*
  96.  * /-----------------------------deprecated---------------------------------
  97.         if(isset($params['maintenanceElements']) &&
  98.             is_array($params['maintenanceElements'])
  99.         ){
  100.             if (!$this->entityManager->isOpen()) {
  101.                 $this->entityManager = $this->entityManager->create(
  102.                     $this->entityManager->getConnection(),
  103.                     $this->entityManager->getConfiguration()
  104.                 );
  105.             }
  106.             foreach ($location->getMaintenanceElements() as $element) {
  107.                 $search = array_search($element->getId(), array_column($params['maintenanceElements'], 'id'));
  108.                 if ( !is_integer($search) && !$search ){
  109.                     $element->setLocation(null);
  110.                     $this->entityManager->persist($element);
  111.                 }
  112.             }
  113.             foreach ($params['maintenanceElements'] as $element) {
  114.                 if (isset($element['id'])) {
  115.                     $getElement = $this->elementRepository->find($element['id']);
  116.                     $getElement->setLocation($location);
  117.                     $this->entityManager->persist($getElement);
  118.                 }
  119.             }
  120.             $this->entityManager->flush();
  121.         }
  122.         if(isset($params['resources']) &&
  123.             is_array($params['resources'])
  124.         ){
  125.             if (!$this->entityManager->isOpen()) {
  126.                 $this->entityManager = $this->entityManager->create(
  127.                     $this->entityManager->getConnection(),
  128.                     $this->entityManager->getConfiguration()
  129.                 );
  130.             }
  131.             foreach ($location->getResources() as $resource) {
  132.                 $search = array_search($resource->getId(), array_column($params['resources'], 'id'));
  133.                 if ( !is_integer($search) && !$search ){
  134.                     $resource->setLocation(null);
  135.                     $this->entityManager->persist($resource);
  136.                 }
  137.             }
  138.             foreach ($params['resources'] as $resource) {
  139.                 if (isset($resource['id'])) {
  140.                     $getResource = $this->resourceRepository->find($resource['id']);
  141.                     $getResource->setLocation($location);
  142.                     $this->entityManager->persist($getResource);
  143.                 }
  144.             }
  145.             $this->entityManager->flush();
  146.         }
  147. -----------------------------deprecated---------------------------------
  148. */
  149.         if(isset($params['users']) &&
  150.             is_array($params['users'])
  151.         ){
  152.             $roleLocation = [Role::ROLE_TASKMASTERRole::ROLE_OPERATORRole::ROLE_USER];
  153.             foreach ($location->getVendorStaff() as $vendorStaff) {
  154.                 $search array_search($vendorStaff->getUser()->getId(), $params['users']);
  155.                 if ( !is_integer($search) && !$search ) {
  156.                     if ($this->vendorService->isUserRoleInToVendor($location->getVendor(), $vendorStaff->getUser(), $roleLocation)) {
  157.                         $vendorStaff->removeLocation($location);
  158.                     }
  159.                 }
  160.             }
  161.             foreach ($params['users'] as $userId) {
  162.                 $user $this->userRepository->find($userId);
  163.                 $vendorStaff $this->vendorStaffRepository->findOneBy([
  164.                     'user' => $user,
  165.                     'vendor' => $location->getVendor()
  166.                 ]);
  167.                 $vendorStaff->addLocation($location);
  168.                 $this->entityManager->persist($vendorStaff);
  169.             }
  170.             $this->entityManager->flush();
  171.         }
  172.         if (Request::METHOD_POST === $method) {
  173.             $identify $this->utilsService->generateIdentify($location);
  174.             $location->setIdentify($identify);
  175.             $this->entityManager->persist($location);
  176.             $this->entityManager->flush();
  177.         }
  178.     }
  179.     public static function getSubscribedEvents()
  180.     {
  181.         return [
  182.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_WRITE]
  183.         ];
  184.     }
  185. }