<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 04/06/2019
* Time: 12:19 PM
*/
namespace App\EventSubscriber\Ticket;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Decorators\WorkerBudget\TicketCost;
use App\Decorators\WorkerBudget\TicketCostUsers;
use App\Services\UtilsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Services\TicketService;
class TicketPostSerializerSubscriber implements EventSubscriberInterface
{
private $ticketService;
private $utilsService;
public function __construct(
TicketService $ticketService,
UtilsService $utilsService)
{
$this->ticketService = $ticketService;
$this->utilsService = $utilsService;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_SERIALIZE]
];
}
/**
* @param ViewEvent $event
* @throws \App\Exception\AccessDeniedException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$request = $event->getRequest();
$route = $request->attributes->get('_route');
$routes = array(
'api_tickets_post_collection',
'api_tickets_get_collection',
'api_tickets_get_item',
'api_tickets_put_item',
'api_vendors_tickets_get_subresource',
'api_tickets_get_canvas_view_collection',
'api_tickets_update_canvas_ticket_item',
'api_tickets_get_ticket_list_collection'
);
if (!in_array($route, $routes))
return;
$ticketResult = $event->getControllerResult();
$ticketResult = json_decode($ticketResult, true);
if ('api_tickets_get_collection' === $route || 'api_tickets_get_ticket_list_collection' === $route ||
'api_vendors_tickets_get_subresource' === $route) {
foreach ($ticketResult as &$ticket) {
$ticket = $this->ticketService->formatNormalize($ticket);
if(!is_null($ticket["dateExpiration"])){
$this->setDateToTicket($ticket);
}
}
} elseif ('api_tickets_get_canvas_view_collection' === $route) {
foreach ($ticketResult['states'] as &$ticketState) {
foreach ($ticketState as &$ticket){
$ticket = $this->ticketService->formatNormalize($ticket);
if (!is_null($ticket["dateExpiration"])) {
$this->setDateToTicket($ticket);
}
}
}
} else {
$ticketResult = $this->ticketService->formatNormalize($ticketResult);
if(!is_null($ticketResult["dateExpiration"])) {
$this->setDateToTicket($ticketResult);
}
}
$ticketResult = json_encode($ticketResult);
$event->setControllerResult($ticketResult);
}
/**
* @param $ticket
* @return int
*/
private function setDateToTicket(&$ticket){
try {
$dateZone = new \DateTime($ticket["dateExpiration"], new \DateTimeZone($ticket["vendorTimeZone"]));;
$dataZoneSecondsOffSet = $dateZone->getOffset();
$timeStamp = $dateZone->getTimestamp() - $dataZoneSecondsOffSet;
$ticket["dateExpiration"] = $dateZone->setTimestamp($timeStamp)->format('Y-m-d\TH:i:sP');
}catch (\Exception $exception){
return -1;
}
}
}