src/EventSubscriber/Location/LocationPostValidateSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 26/04/2019
  6.  * Time: 11:50 AM
  7.  */
  8. namespace App\EventSubscriber\Location;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use ApiPlatform\Core\EventListener\EventPriorities;
  16. use App\Exception\InvalidArgumentException;
  17. use App\Exception\NotFoundException;
  18. use App\Repository\App\MediaObjectRepository;
  19. use App\Repository\App\MaintenanceElementRepository;
  20. use App\Repository\App\ResourceRepository;
  21. use App\Repository\App\CategoryRepository;
  22. use App\Repository\App\UserRepository;
  23. use App\Repository\App\VendorStaffRepository;
  24. use App\Entity\App\Location;
  25. use App\Entity\App\MediaObject;
  26. use App\Entity\App\Category;
  27. use App\Entity\App\User;
  28. use App\Entity\App\VendorStaff;
  29. class LocationPostValidateSubscriber implements EventSubscriberInterface
  30. {
  31.     private $tokenStorage;
  32.     private $request;
  33.     private $mediaObjectRepository;
  34.     private $elementRepository;
  35.     private $resourceRepository;
  36.     private $categoryRepository;
  37.     private $userRepository;
  38.     private $vendorStaffRepository;
  39.     private $translator;
  40.     public function __construct(
  41.         MediaObjectRepository $mediaObjectRepository,
  42.         MaintenanceElementRepository $elementRepository,
  43.         ResourceRepository $resourceRepository,
  44.         CategoryRepository $categoryRepository,
  45.         UserRepository $userRepository,
  46.         VendorStaffRepository $vendorStaffRepository,
  47.         TokenStorageInterface $tokenStorage,
  48.         TranslatorInterface $translator
  49.     ){
  50.         $this->tokenStorage $tokenStorage;
  51.         $this->mediaObjectRepository $mediaObjectRepository;
  52.         $this->elementRepository $elementRepository;
  53.         $this->resourceRepository $resourceRepository;
  54.         $this->categoryRepository $categoryRepository;
  55.         $this->userRepository $userRepository;
  56.         $this->vendorStaffRepository $vendorStaffRepository;
  57.         $this->translator $translator;
  58.     }
  59.     /**
  60.      * @param ViewEvent $event
  61.      * @throws InvalidArgumentException
  62.      * @throws NotFoundException
  63.      * @throws \Doctrine\ORM\NonUniqueResultException
  64.      */
  65.     public function onKernelView(ViewEvent $event)
  66.     {
  67.         $location $event->getControllerResult();
  68.         $this->request $event->getRequest();
  69.         $method $this->request->getMethod();
  70.         if (!($location instanceof Location) ||
  71.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
  72.         )
  73.             return;
  74.         $content $this->request->getContent();
  75.         $params json_decode($contenttrue);
  76.         if(isset($params['mediaContent']) &&
  77.             is_array($params['mediaContent']) &&
  78.             count($params['mediaContent']) > 0
  79.         ){
  80.             foreach ($location->getMediaContent() as $mediaId){
  81.                 $media $this->mediaObjectRepository->find($mediaId);
  82.                 if (!$media instanceof MediaObject) {
  83.                     throw new NotFoundException(
  84.                         $this->translator->trans('general.validate.not_exists',
  85.                             [
  86.                                 '%entityName%' => $this->translator->trans('mediaObject.name'),
  87.                                 '%entityId%' => $mediaId
  88.                             ]
  89.                         )
  90.                     );
  91.                 }
  92.                 if ($media->getLocation() !== null &&
  93.                     $media->getLocation() !== $location){
  94.                     throw new InvalidArgumentException(
  95.                         $this->translator->trans('mediaObject.validate.does_not_belongs',
  96.                             [
  97.                                 '%mediaId%' => $mediaId,
  98.                                 '%entity%' => $this->translator->trans('location.name')
  99.                             ]
  100.                         )
  101.                     );
  102.                 }
  103.                 if ($media->getType() !== MediaObject::TYPE_LOCATION) {
  104.                     throw new InvalidArgumentException(
  105.                         $this->translator->trans(
  106.                             'mediaObject.validate.type_not_allowed',
  107.                             [
  108.                                 '%mediaId%' => $mediaId,
  109.                                 '%mediaType%' => $media->getType(),
  110.                                 '%mediaTypeAvailable%' => MediaObject::TYPE_LOCATION
  111.                             ]
  112.                         )
  113.                     );
  114.                 }
  115.             }
  116.         }
  117.         if(isset($params['maintenanceElements']) && is_array($params['maintenanceElements'])){
  118.             foreach ($params['maintenanceElements'] as $element) {
  119.                 if (isset($element['id'])) {
  120.                     $maintenanceElement $this->elementRepository->findOneById($element['id']);
  121.                     if ($maintenanceElement['vendorId'] !== $location->getVendor()->getId()) {
  122.                         throw new InvalidArgumentException(
  123.                             $this->translator->trans('vendor.validate.does_not_belongs',
  124.                                 [
  125.                                     '%entity%' => $this->translator->trans('maintenanceElement.title'),
  126.                                     '%vendorName%' => $location->getVendor()->getName()
  127.                                 ]
  128.                             )
  129.                         );
  130.                     }
  131.                     if ($maintenanceElement['locationId'] !== null &&
  132.                         $maintenanceElement['locationId'] !== $location->getId()) {
  133.                         throw new InvalidArgumentException(
  134.                             $this->translator->trans(
  135.                                 'This entity has belong to another entity already',
  136.                                 [
  137.                                     '%entity1%' => $this->translator->trans('maintenanceElement.title'),
  138.                                     '%entity2%' => $this->translator->trans('location.title'),
  139.                                     '%entityId1%' => $element['id']
  140.                                 ]
  141.                             )
  142.                         );
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         if (isset($params['resources']) && is_array($params['resources'])) {
  148.             foreach ($params['resources'] as $resource) {
  149.                 if (isset($resource['id'])) {
  150.                     $getResource $this->resourceRepository->findOneById($resource['id']);
  151.                     if ($getResource['vendorId'] !== $location->getVendor()->getId()) {
  152.                         throw new InvalidArgumentException(
  153.                             $this->translator->trans('vendor.validate.does_not_belongs',
  154.                                 [
  155.                                     '%entity%' => $this->translator->trans('resource.title'),
  156.                                     '%vendorName%' => $location->getVendor()->getName()
  157.                                 ]
  158.                             )
  159.                         );
  160.                     }
  161.                     if ($getResource['locationId'] !== null &&
  162.                         $getResource['locationId'] !== $location->getId()) {
  163.                         throw new InvalidArgumentException(
  164.                             $this->translator->trans(
  165.                                 'This entity has belong to another entity already',
  166.                                 [
  167.                                     '%entity1%' => $this->translator->trans('resource.title'),
  168.                                     '%entity2%' => $this->translator->trans('location.title'),
  169.                                     '%entityId1%' => $resource['id']
  170.                                 ]
  171.                             )
  172.                         );
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.         if(isset($params['categories']) && is_array($params['categories'])){
  178.             foreach ($params['categories'] as $field) {
  179.                 if (isset($field['id'])) {
  180.                     $category $this->categoryRepository->find($field['id']);
  181.                     if ($category->getVendor() !== $location->getVendor()) {
  182.                         throw new InvalidArgumentException(
  183.                             $this->translator->trans('vendor.validate.does_not_belongs',
  184.                                 [
  185.                                     '%entity%' => $this->translator->trans('category.title'),
  186.                                     '%vendorName%' => $location->getVendor()->getName()
  187.                                 ]
  188.                             )
  189.                         );
  190.                     }
  191.                     if ($category->getType() !== Category::TYPE_LOCATION) {
  192.                         throw new InvalidArgumentException(
  193.                             $this->translator->trans('category.validate.type_not_allowed',
  194.                                 [
  195.                                     '%type%' => $category->getType(),
  196.                                     '%typeAllowed%' => Category::TYPE_LOCATION
  197.                                 ]
  198.                             )
  199.                         );
  200.                     }
  201.                 }
  202.             }
  203.         }
  204.         if (isset($params['users']) && count($params['users']) > 0) {
  205.             foreach ($params['users'] as $userId) {
  206.                 if (is_integer($userId)) {
  207.                     $user $this->userRepository->find($userId);
  208.                     if (!$user instanceof User) {
  209.                         throw new NotFoundException($this->translator->trans('User not found'));
  210.                     }
  211.                     $vendorStaff $this->vendorStaffRepository->findOneBy(
  212.                         [
  213.                             'user' => $user,
  214.                             'vendor' => $location->getVendor()
  215.                         ]
  216.                     );
  217.                     if (!$vendorStaff instanceof VendorStaff) {
  218.                         throw new InvalidArgumentException(
  219.                             $this->translator->trans(
  220.                                 'This entity does not belong to vendor',
  221.                                 [
  222.                                     '%entity%' => $this->translator->trans('user'),
  223.                                     '%entityId%' => $userId
  224.                                 ]
  225.                             )
  226.                         );
  227.                     }
  228.                 } else {
  229.                     throw new InvalidArgumentException(
  230.                         $this->translator->trans('invalid field format', ['%field%' => 'users'])
  231.                     );
  232.                 }
  233.             }
  234.         }
  235.         if($location->getLng() && $location->getLat()) {
  236.             $location->setPoint("POINT({$location->getLng()} {$location->getLat()})");
  237.         }
  238.         return;
  239.     }
  240.     public static function getSubscribedEvents()
  241.     {
  242.         return [
  243.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_VALIDATE]
  244.         ];
  245.     }
  246. }