src/EventSubscriber/Ticket/TicketPostSerializerSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 04/06/2019
  6.  * Time: 12:19 PM
  7.  */
  8. namespace App\EventSubscriber\Ticket;
  9. use ApiPlatform\Core\EventListener\EventPriorities;
  10. use App\Decorators\WorkerBudget\TicketCost;
  11. use App\Decorators\WorkerBudget\TicketCostUsers;
  12. use App\Services\UtilsService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ViewEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use App\Services\TicketService;
  17. class TicketPostSerializerSubscriber implements EventSubscriberInterface
  18. {
  19.     private $ticketService;
  20.     private $utilsService;
  21.     public function __construct(
  22.         TicketService $ticketService,
  23.         UtilsService $utilsService)
  24.     {
  25.         $this->ticketService $ticketService;
  26.         $this->utilsService $utilsService;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public static function getSubscribedEvents() {
  32.         return [
  33.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  34.         ];
  35.     }
  36.     /**
  37.      * @param ViewEvent $event
  38.      * @throws \App\Exception\AccessDeniedException
  39.      * @throws \Doctrine\ORM\NonUniqueResultException
  40.      */
  41.     public function onKernelView(ViewEvent $event)
  42.     {
  43.         if ($this->utilsService->isAPublicRequest($event)) {
  44.             return;
  45.         }
  46.         $request $event->getRequest();
  47.         $route $request->attributes->get('_route');
  48.         $routes = array(
  49.             'api_tickets_post_collection',
  50.             'api_tickets_get_collection',
  51.             'api_tickets_get_item',
  52.             'api_tickets_put_item',
  53.             'api_vendors_tickets_get_subresource',
  54.             'api_tickets_get_canvas_view_collection',
  55.             'api_tickets_update_canvas_ticket_item',
  56.             'api_tickets_get_ticket_list_collection'
  57.         );
  58.         if (!in_array($route$routes))
  59.             return;
  60.         $ticketResult $event->getControllerResult();
  61.         $ticketResult json_decode($ticketResulttrue);
  62.         if ('api_tickets_get_collection' === $route || 'api_tickets_get_ticket_list_collection' === $route ||
  63.             'api_vendors_tickets_get_subresource' === $route) {
  64.             foreach ($ticketResult as &$ticket) {
  65.                 $ticket $this->ticketService->formatNormalize($ticket);
  66.                 if(!is_null($ticket["dateExpiration"])){
  67.                     $this->setDateToTicket($ticket);
  68.                 }
  69.             }
  70.         } elseif ('api_tickets_get_canvas_view_collection' === $route) {
  71.             foreach ($ticketResult['states'] as &$ticketState) {
  72.                 foreach ($ticketState as &$ticket){
  73.                     $ticket $this->ticketService->formatNormalize($ticket);
  74.                     if (!is_null($ticket["dateExpiration"])) {
  75.                         $this->setDateToTicket($ticket);
  76.                     }
  77.                 }
  78.             }
  79.         } else {
  80.             $ticketResult $this->ticketService->formatNormalize($ticketResult);
  81.             if(!is_null($ticketResult["dateExpiration"])) {
  82.                 $this->setDateToTicket($ticketResult);
  83.             }
  84.         }
  85.         $ticketResult json_encode($ticketResult);
  86.         $event->setControllerResult($ticketResult);
  87.     }
  88.     /**
  89.      * @param $ticket
  90.      * @return int
  91.      */
  92.     private function setDateToTicket(&$ticket){
  93.         try {
  94.             $dateZone = new \DateTime($ticket["dateExpiration"], new \DateTimeZone($ticket["vendorTimeZone"]));;
  95.             $dataZoneSecondsOffSet $dateZone->getOffset();
  96.             $timeStamp $dateZone->getTimestamp() - $dataZoneSecondsOffSet;
  97.             $ticket["dateExpiration"] = $dateZone->setTimestamp($timeStamp)->format('Y-m-d\TH:i:sP');
  98.         }catch (\Exception $exception){
  99.             return -1;
  100.         }
  101.     }
  102. }