src/EventSubscriber/MaintenanceElement/MaintenanceElementPostValidateSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 15/04/2019
  6.  * Time: 10:05 PM
  7.  */
  8. namespace App\EventSubscriber\MaintenanceElement;
  9. use App\Services\UtilsService;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\HttpKernel\Event\ViewEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use ApiPlatform\Core\EventListener\EventPriorities;
  19. use App\Exception\InvalidArgumentException;
  20. use App\Exception\NotFoundException;
  21. use App\Repository\App\MediaObjectRepository;
  22. use App\Repository\App\MaintenanceElementRepository;
  23. use App\Entity\App\MaintenanceElement;
  24. use App\Entity\App\MediaObject;
  25. use App\Entity\App\Category;
  26. use App\Entity\App\User;
  27. class MaintenanceElementPostValidateSubscriber implements EventSubscriberInterface
  28. {
  29.     private $tokenStorage;
  30.     private $request;
  31.     private $requestStack;
  32.     private $mediaObjectRepository;
  33.     private $elementRepository;
  34.     private $utilsService;
  35.     private $translator;
  36.     public function __construct(
  37.         MediaObjectRepository $mediaObjectRepository,
  38.         MaintenanceElementRepository $elementRepository,
  39.         TokenStorageInterface $tokenStorage,
  40.         UtilsService $utilsService,
  41.         TranslatorInterface $translator,
  42.         RequestStack $requestStack
  43.     ){
  44.         $this->tokenStorage $tokenStorage;
  45.         $this->mediaObjectRepository $mediaObjectRepository;
  46.         $this->elementRepository $elementRepository;
  47.         $this->utilsService $utilsService;
  48.         $this->translator $translator;
  49.         $this->requestStack $requestStack;
  50.     }
  51.     /**
  52.      * @param ViewEvent $event
  53.      * @throws InvalidArgumentException
  54.      * @throws NotFoundException
  55.      * @throws \Doctrine\ORM\NonUniqueResultException
  56.      */
  57.     public function onKernelView(ViewEvent $event)
  58.     {
  59.         if ($this->utilsService->isAPublicRequest($event)) {
  60.             return;
  61.         }
  62.         $maintenanceElement $event->getControllerResult();
  63.         $this->request $event->getRequest();
  64.         $method $this->request->getMethod();
  65.         if (!($maintenanceElement instanceof MaintenanceElement) ||
  66.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
  67.         )
  68.             return;
  69.         $locale $this->requestStack->getCurrentRequest()->getLocale();
  70.         $userCurrent $this->tokenStorage->getToken()->getUser();
  71.         if ($userCurrent instanceof User) {
  72.             $locale $userCurrent->getLocale();
  73.         }
  74.         $parent $maintenanceElement->getParent();
  75.         if ($parent !== null && $parent !== 0)
  76.             $this->validateParent($maintenanceElement$parent);
  77.         if($maintenanceElement->getFamily()) {
  78.             $vendor $maintenanceElement->getVendor();
  79.             if ($maintenanceElement->getFamily()->getVendor() !== $vendor) {
  80.                 throw new InvalidArgumentException(
  81.                     $this->translator->trans('The family does not belongs to the vendor',
  82.                         ['%vendor%' => $vendor->getName()],
  83.                         null,
  84.                         $locale
  85.                     )
  86.                 );
  87.             }
  88.             if ($maintenanceElement->getFamily()->getType() !== Category::TYPE_MAINTENANCE_ELEMENT) {
  89.                 throw new InvalidArgumentException(
  90.                     $this->translator->trans('Family type is not allowed for this operation. Allowed',
  91.                         [
  92.                             '%maintenanceElement%' => $maintenanceElement->getFamily()->getType(),
  93.                             '%category%' => Category::TYPE_MAINTENANCE_ELEMENT
  94.                         ],
  95.                         null,
  96.                         $locale
  97.                     )
  98.                 );
  99.             }
  100.         }
  101.         if ($maintenanceElement->getLocation()) {
  102.             $vendor $maintenanceElement->getVendor();
  103.             if ($maintenanceElement->getLocation()->getVendor() !== $vendor) {
  104.                 throw new InvalidArgumentException(
  105.                     $this->translator->trans('vendor.validate.does_not_belongs',
  106.                         [
  107.                             '%entity%' => $this->translator->trans('location.title'),
  108.                             '%vendorName%' => $vendor->getName()
  109.                         ],
  110.                         null,
  111.                         $locale
  112.                     )
  113.                 );
  114.             }
  115.         }
  116.         $content $this->request->getContent();
  117.         $params json_decode($contenttrue);
  118.         if(isset($params['mediaContent']) &&
  119.             is_array($params['mediaContent']) &&
  120.             count($params['mediaContent']) > 0
  121.         ){
  122.             foreach ($maintenanceElement->getMediaContent() as $mediaId){
  123.                 $media $this->mediaObjectRepository->find($mediaId);
  124.                 if (!$media instanceof MediaObject) {
  125.                     throw new NotFoundException(
  126.                         $this->translator->trans('general.validate.not_exists',
  127.                             [
  128.                                 '%entityName%' => $this->translator->trans('mediaObject.name'),
  129.                                 '%entityId%' => $mediaId
  130.                             ],
  131.                             null,
  132.                             $locale
  133.                         )
  134.                     );
  135.                 }
  136.                 if ($media->getMaintenanceElement() !== null &&
  137.                     $media->getMaintenanceElement() !== $maintenanceElement) {
  138.                     throw new InvalidArgumentException(
  139.                         $this->translator->trans('mediaObject.validate.does_not_belongs',
  140.                             [
  141.                                 '%mediaId%' => $mediaId,
  142.                                 '%entity%' => $this->translator->trans('maintenanceElement.name')
  143.                             ],
  144.                             null,
  145.                             $locale
  146.                         )
  147.                     );
  148.                 }
  149.                 if ($media->getType() !== MediaObject::TYPE_MAINTENANCE_ELEMENT) {
  150.                     throw new InvalidArgumentException(
  151.                         $this->translator->trans(
  152.                             'mediaObject.validate.type_not_allowed',
  153.                             [
  154.                                 '%mediaId%' => $mediaId,
  155.                                 '%mediaType%' => $media->getType(),
  156.                                 '%mediaTypeAvailable%' => MediaObject::TYPE_MAINTENANCE_ELEMENT
  157.                             ],
  158.                             null,
  159.                             $locale
  160.                         )
  161.                     );
  162.                 }
  163.             }
  164.         }
  165.         if(isset($params['children']) &&
  166.             is_array($params['children']) &&
  167.             count($params['children']) > 0
  168.         ){
  169.            foreach ($params['children'] as $child) {
  170.                if (isset($child['id'])) {
  171.                    $children $this->elementRepository->findOneById($child['id']);
  172.                    if ($children['vendorId'] !== $maintenanceElement->getVendor()->getId()) {
  173.                        throw new InvalidArgumentException(
  174.                            $this->translator->trans(
  175.                                'The vendor between the parent and the new maintenance element is not equal',
  176.                                [],
  177.                                null,
  178.                                $locale
  179.                            )
  180.                        );
  181.                    }
  182.                    if ($children['parentId'] && $children['parentId'] !== $maintenanceElement->getId()) {
  183.                        $response = new Response();
  184.                        $response->setContent(json_encode([
  185.                            'detail' => $this->translator->trans(
  186.                                'This children has belong to another maintenance element already',
  187.                                ['%id%' => $children['name']],
  188.                                null,
  189.                                $locale
  190.                            )
  191.                        ]));
  192.                        $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  193.                        $event->setResponse($response);
  194.                        return;
  195.                    }
  196.                }
  197.            }
  198.         }
  199.         if($maintenanceElement->getLng() && $maintenanceElement->getLat()) {
  200.             $maintenanceElement->setPoint("POINT({$maintenanceElement->getLng()} {$maintenanceElement->getLat()})");
  201.         }
  202.         return;
  203.     }
  204.     /**
  205.      * @param MaintenanceElement $maintenanceElement
  206.      * @param MaintenanceElement $parent
  207.      * @throws InvalidArgumentException
  208.      */
  209.     public function validateParent(MaintenanceElement $maintenanceElementMaintenanceElement $parent)
  210.     {
  211.         if ($maintenanceElement->getVendor() !== $parent->getVendor()){
  212.             throw new InvalidArgumentException(
  213.                 $this->translator->trans('The vendor between the parent and the new maintenance element is not equal')
  214.             );
  215.         }
  216.     }
  217.     public static function getSubscribedEvents()
  218.     {
  219.         return [
  220.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_VALIDATE]
  221.         ];
  222.     }
  223. }