src/EventSubscriber/Resource/ResourcePostValidateSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 24/04/2019
  6.  * Time: 01:31 PM
  7.  */
  8. namespace App\EventSubscriber\Resource;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  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\ResourceRepository;
  20. use App\Services\VendorService;
  21. use App\Services\UtilsService;
  22. use App\Entity\App\ResourceAndTool;
  23. use App\Entity\App\MediaObject;
  24. use App\Entity\App\Category;
  25. use App\Entity\App\User;
  26. class ResourcePostValidateSubscriber implements EventSubscriberInterface
  27. {
  28.     private $tokenStorage;
  29.     private $request;
  30.     private $mediaObjectRepository;
  31.     private $resourceRepository;
  32.     private $utilsService;
  33.     private $vendorService;
  34.     private $translator;
  35.     public function __construct(
  36.         MediaObjectRepository $mediaObjectRepository,
  37.         ResourceRepository $resourceRepository,
  38.         TokenStorageInterface $tokenStorage,
  39.         UtilsService $utilsService,
  40.         VendorService $vendorService,
  41.         TranslatorInterface $translator
  42.     ){
  43.         $this->tokenStorage $tokenStorage;
  44.         $this->mediaObjectRepository $mediaObjectRepository;
  45.         $this->resourceRepository $resourceRepository;
  46.         $this->utilsService $utilsService;
  47.         $this->vendorService $vendorService;
  48.         $this->translator $translator;
  49.     }
  50.     /**
  51.      * @param ViewEvent $event
  52.      * @throws InvalidArgumentException
  53.      * @throws NotFoundException
  54.      */
  55.     public function onKernelView(ViewEvent $event)
  56.     {
  57.         if ($this->utilsService->isAPublicRequest($event)) {
  58.             return;
  59.         }
  60.         $resource $event->getControllerResult();
  61.         $this->request $event->getRequest();
  62.         $method $this->request->getMethod();
  63.         $userCurrent $this->tokenStorage->getToken()->getUser();
  64.         if (!($resource instanceof ResourceAndTool) ||
  65.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
  66.         )
  67.             return;
  68.         if(!($userCurrent instanceof User))
  69.             throw new NotFoundException($this->translator->trans('User current not found'));
  70.         $vendor $resource->getVendor();
  71.         if($resource->getCategory()) {
  72.             if ($resource->getCategory()->getVendor() !== $vendor) {
  73.                 throw new InvalidArgumentException(
  74.                     $this->translator->trans('The category does not belongs to the vendor',
  75.                         ['%vendor%' => $vendor->getName()]
  76.                     )
  77.                 );
  78.             }
  79.             if ($resource->getCategory()->getType() !== Category::TYPE_RESOURCE) {
  80.                 throw new InvalidArgumentException(
  81.                     $this->translator->trans('category.validate.type_not_allowed',
  82.                         [
  83.                             '%type%' => $resource->getCategory()->getType(),
  84.                             '%typeAllowed%' => Category::TYPE_RESOURCE
  85.                         ]
  86.                     )
  87.                 );
  88.             }
  89.         }
  90.         if ($resource->getLocation()) {
  91.             if ($resource->getLocation()->getVendor() !== $vendor) {
  92.                 throw new InvalidArgumentException(
  93.                     $this->translator->trans('vendor.validate.does_not_belongs',
  94.                         [
  95.                             '%entity%' => $this->translator->trans('location.title'),
  96.                             '%vendorName%' => $vendor->getName()
  97.                         ]
  98.                     )
  99.                 );
  100.             }
  101.         }
  102.         $content $this->request->getContent();
  103.         $params json_decode($contenttrue);
  104.         if(isset($params['mediaContent']) &&
  105.             is_array($params['mediaContent']) &&
  106.             count($params['mediaContent']) > 0
  107.         ){
  108.             foreach ($resource->getMediaContent() as $mediaId){
  109.                 $media $this->mediaObjectRepository->find($mediaId);
  110.                 if (!$media instanceof MediaObject) {
  111.                     throw new NotFoundException(
  112.                         $this->translator->trans('general.validate.not_exists',
  113.                             [
  114.                                 '%entityName%' => $this->translator->trans('mediaObject.name'),
  115.                                 '%entityId%' => $mediaId
  116.                             ]
  117.                         )
  118.                     );
  119.                 }
  120.                 if ($media->getResource() !== null &&
  121.                     $media->getresource() !== $resource) {
  122.                     throw new InvalidArgumentException(
  123.                         $this->translator->trans('mediaObject.validate.does_not_belongs',
  124.                             [
  125.                                 '%mediaId%' => $mediaId,
  126.                                 '%entity%' => $this->translator->trans('resource.name')
  127.                             ]
  128.                         )
  129.                     );
  130.                 }
  131.                 if ($media->getType() !== MediaObject::TYPE_RESOURCE) {
  132.                     throw new InvalidArgumentException(
  133.                         $this->translator->trans(
  134.                             'mediaObject.validate.type_not_allowed',
  135.                             [
  136.                                 '%mediaId%' => $mediaId,
  137.                                 '%mediaType%' => $media->getType(),
  138.                                 '%mediaTypeAvailable%' => MediaObject::TYPE_RESOURCE
  139.                             ]
  140.                         )
  141.                     );
  142.                 }
  143.             }
  144.         }
  145.         if(isset($params['children']) &&
  146.             is_array($params['children']) &&
  147.             count($params['children']) > 0
  148.         ){
  149.             foreach ($params['children'] as $child) {
  150.                 if (isset($child['id'])) {
  151.                     $children $this->resourceRepository->find($child['id']);
  152.                     $this->validateParent($children$resource);
  153.                     if ($children->getParent() && $children->getParent() !== $resource) {
  154.                         throw new InvalidArgumentException(
  155.                             $this->translator->trans(
  156.                                 'This children has belong to another resource already',
  157.                                 ['%id%' => $child['id']]
  158.                             )
  159.                         );
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.         return;
  165.     }
  166.     /**
  167.      * @param ResourceAndTool $resource
  168.      * @param ResourceAndTool $parent
  169.      * @throws InvalidArgumentException
  170.      */
  171.     public function validateParent(ResourceAndTool $resourceResourceAndTool $parent)
  172.     {
  173.         if ($resource->getVendor() !== $parent->getVendor()){
  174.             throw new InvalidArgumentException(
  175.                 $this->translator->trans('The vendor between the parent and the new resource is not equal')
  176.             );
  177.         }
  178.     }
  179.     public static function getSubscribedEvents()
  180.     {
  181.         return [
  182.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_VALIDATE]
  183.         ];
  184.     }
  185. }