src/EventSubscriber/Ticket/TicketPostValidateSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 11/03/2019
  6.  * Time: 09:55 AM
  7.  */
  8. namespace App\EventSubscriber\Ticket;
  9. use App\Entity\App\Category;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use ApiPlatform\Core\EventListener\EventPriorities;
  17. use App\Repository\App\Ticket\TicketTypeRepository;
  18. use App\Repository\App\MediaObjectRepository;
  19. use App\Exception\InvalidArgumentException;
  20. use App\Exception\NotFoundException;
  21. use App\Services\VendorService;
  22. use App\Services\UtilsService;
  23. use App\Entity\App\MediaObject;
  24. use App\Entity\App\Ticket\Ticket;
  25. class TicketPostValidateSubscriber implements EventSubscriberInterface
  26. {
  27.     private $tokenStorage;
  28.     private $ticketTypeRepository;
  29.     private $mediaObjectRepository;
  30.     private $vendorService;
  31.     private $utilsService;
  32.     private $translator;
  33.     public function __construct(
  34.         TokenStorageInterface $tokenStorage,
  35.         TicketTypeRepository $ticketTypeRepository,
  36.         MediaObjectRepository $mediaObjectRepository,
  37.         VendorService $vendorService,
  38.         UtilsService $utilsService,
  39.         TranslatorInterface $translator
  40.     ){
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->ticketTypeRepository $ticketTypeRepository;
  43.         $this->mediaObjectRepository $mediaObjectRepository;
  44.         $this->vendorService $vendorService;
  45.         $this->utilsService $utilsService;
  46.         $this->translator $translator;
  47.     }
  48.     /**
  49.      * @param ViewEvent $event
  50.      * @throws InvalidArgumentException
  51.      * @throws NotFoundException
  52.      */
  53.     public function onKernelView(ViewEvent $event)
  54.     {
  55.         if ($this->utilsService->isAPublicRequest($event)) {
  56.             return;
  57.         }
  58.         $ticket $event->getControllerResult();
  59.         $request $event->getRequest();
  60.         $method $request->getMethod();
  61.         if (!($ticket instanceof Ticket) ||
  62.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
  63.         )
  64.             return;
  65.         $content $request->getContent();
  66.         $params json_decode($contenttrue);
  67.         $this->validateRequiredFields($ticket$params$method);
  68.     }
  69.     /**
  70.      * @param Ticket $ticket
  71.      * @param array $params
  72.      * @param string $method
  73.      * @throws InvalidArgumentException
  74.      * @throws NotFoundException
  75.      */
  76.     protected function validateRequiredFields(Ticket $ticket, array $paramsstring $method)
  77.     {
  78.         if (Request::METHOD_POST === $method) {
  79.             if (!isset($params['vendor'])) {
  80.                 throw new InvalidArgumentException(
  81.                     $this->translator->trans('ticket.field_validate.required', ['%field%' => 'vendor'])
  82.                 );
  83.             }
  84.             if (!isset($params['type'])) {
  85.                 throw new InvalidArgumentException(
  86.                     $this->translator->trans('ticket.field_validate.required', ['%field%' => 'type'])
  87.                 );
  88.             }
  89.         }
  90.         if (!empty($params['incidenceType']) && $ticket->getIncidenceType()) {
  91.             $vendor $ticket->getVendor();
  92.             $incidenceType $ticket->getIncidenceType();
  93.             if ($vendor !== $incidenceType->getVendor()) {
  94.                 throw new InvalidArgumentException(
  95.                     $this->translator->trans('vendor.validate.does_not_belongs',
  96.                         [
  97.                             '%entity%' => $this->translator->trans('incidenceType'),
  98.                             '%vendorName%' => $vendor->getName()
  99.                         ]
  100.                     )
  101.                 );
  102.             }
  103.         }
  104.         if (!empty($params['location']) && $ticket->getLocation()) {
  105.             $vendor $ticket->getVendor();
  106.             $location $ticket->getLocation();
  107.             if (!in_array($location$vendor->getLocations()->toArray())) {
  108.                 throw new InvalidArgumentException(
  109.                     $this->translator->trans('vendor.validate.does_not_belongs',
  110.                         [
  111.                             '%entity%' => $this->translator->trans('location.title'),
  112.                             '%vendorName%' => $vendor->getName()
  113.                         ]
  114.                     )
  115.                 );
  116.             }
  117.         }
  118.         if (!empty($params['category']) && $ticket->getCategory()) {
  119.             if ($ticket->getCategory()->getVendor() !== $ticket->getVendor()) {
  120.                 throw new InvalidArgumentException(
  121.                     $this->translator->trans('vendor.validate.does_not_belongs',
  122.                         [
  123.                             '%entity%' => $this->translator->trans('category.title'),
  124.                             '%vendorName%' => $ticket->getVendor()->getName()
  125.                         ]
  126.                     )
  127.                 );
  128.             }
  129.             if ($ticket->getCategory()->getType() !== Category::TYPE_TASK) {
  130.                 throw new InvalidArgumentException(
  131.                     $this->translator->trans('category.validate.type_not_allowed',
  132.                         [
  133.                             '%type%' => $ticket->getCategory()->getType(),
  134.                             '%typeAllowed%' => Category::TYPE_TASK
  135.                         ]
  136.                     )
  137.                 );
  138.             }
  139.         }
  140.         if(isset($params['mediaContent']) &&
  141.             is_array($params['mediaContent']) &&
  142.             count($params['mediaContent']) > 0
  143.         ){
  144.             foreach ($ticket->getMediaContent() as $mediaId){
  145.                 $media $this->mediaObjectRepository->find($mediaId);
  146.                 if (!$media instanceof MediaObject) {
  147.                     throw new NotFoundException(
  148.                         $this->translator->trans('general.validate.not_exists',
  149.                             [
  150.                                 '%entityName%' => $this->translator->trans('mediaObject.name'),
  151.                                 '%entityId%' => $mediaId
  152.                             ]
  153.                         )
  154.                     );
  155.                 }
  156.                 if ($media->getTicket() !== null && $media->getTicket() !== $ticket) {
  157.                     throw new InvalidArgumentException(
  158.                         $this->translator->trans('mediaObject.validate.does_not_belongs',
  159.                             [
  160.                                 '%mediaId%' => $mediaId,
  161.                                 '%entity%' => $this->translator->trans("ticket.type.{$media->getTicket()->getType()->getName()}")
  162.                             ]
  163.                         )
  164.                     );
  165.                 }
  166.                 if ($media->getType() !== MediaObject::TYPE_TICKET) {
  167.                     throw new InvalidArgumentException(
  168.                         $this->translator->trans(
  169.                             'mediaObject.validate.type_not_allowed',
  170.                             [
  171.                                 '%mediaId%' => $mediaId,
  172.                                 '%mediaType%' => $media->getType(),
  173.                                 '%mediaTypeAvailable%' => MediaObject::TYPE_TICKET
  174.                             ]
  175.                         )
  176.                     );
  177.                 }
  178.             }
  179.         }
  180.         //Validamos los campos de los mantenimientos programados
  181.         $scheduledTask $this->ticketTypeRepository->findOneBy(['name' => 'scheduled_task']);
  182.         if ($ticket->getType() === $scheduledTask) {
  183.             $validateReminderDate false;
  184.             $validateReminderDateToday false;
  185.             if (isset($params['repeat']) && $params['repeat']) {
  186.                 $frequency = ['week''month''year'];
  187.                 $week = ['monday''tuesday''wednesday''thursday''friday''saturday''sunday'];
  188.                 if (!isset($params['frequency']) || is_null($params['frequency'])) {
  189.                     throw new InvalidArgumentException(
  190.                         $this->translator->trans(
  191.                             'general.validate.required_allowed',
  192.                             [
  193.                                 '%field%' => 'frequency',
  194.                                 '%allowedValue%' => implode(', '$frequency)
  195.                             ]
  196.                         )
  197.                     );
  198.                 }
  199.                 if ($params['frequency'] == 'week') {
  200.                     if (!isset($params['frequencyWeek']) ||
  201.                         is_null($params['frequencyWeek'])
  202.                     ) {
  203.                         throw new InvalidArgumentException(
  204.                             $this->translator->trans(
  205.                                 'general.validate.required_allowed',
  206.                                 [
  207.                                     '%field%' => 'frequencyWeek',
  208.                                     '%allowedValue%' => implode(', '$week)
  209.                                 ]
  210.                             )
  211.                         );
  212.                     }
  213.                     if (is_array($params['frequencyWeek'])) {
  214.                         foreach ($params['frequencyWeek'] as $frequencyWeek) {
  215.                             if (!in_array($frequencyWeekarray_keys($week))) {
  216.                                 throw new InvalidArgumentException(
  217.                                     $this->translator->trans(
  218.                                         'general.validate.value_not_allowed',
  219.                                         [
  220.                                             '%value%' => $frequencyWeek
  221.                                         ]
  222.                                     )
  223.                                 );
  224.                             }
  225.                         }
  226.                     }
  227.                 }
  228.                 if ($params['frequency'] == 'week') {
  229.                     $validateReminderDateToday true;
  230.                 }
  231.                 if ($params['frequency'] == 'month' || $params['frequency'] ==  'year') {
  232.                     $validateReminderDate true;
  233.                 }
  234.             } else {
  235.                 $validateReminderDate true;
  236.                 $validateReminderDateToday true;
  237.             }
  238.             if ($validateReminderDate) {
  239.                 if (!isset($params['reminderDate']) && is_null($params['reminderDate'])) {
  240.                     throw new InvalidArgumentException(
  241.                         $this->translator->trans(
  242.                             'general.validate.required_not_null',
  243.                             [
  244.                                 '%field%' => 'reminderDate'
  245.                             ]
  246.                         )
  247.                     );
  248.                 }
  249.             }
  250.             if ($validateReminderDateToday) {
  251.                 if (!isset($params['reminderDate']) && !is_null($params['reminderDate']) &&
  252.                     $params['reminderDate']->format('Y-m-d') < date('Y-m-d')
  253.                 ) {
  254.                     throw new InvalidArgumentException(
  255.                         $this->translator->trans(
  256.                             'general.validate.date_must_be_greater',
  257.                             [
  258.                                 '%field%' => 'repeatDateEnd',
  259.                                 '%dateCompare%' => date('d-m-Y')
  260.                             ]
  261.                         )
  262.                     );
  263.                 }
  264.             }
  265.         }
  266.     }
  267.     public static function getSubscribedEvents()
  268.     {
  269.         return [
  270.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_VALIDATE]
  271.         ];
  272.     }
  273. }