apps/uvdesk/custom-fields/src/EventListeners/RequestEventListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace UVDesk\CommunityPackages\UVDesk\CustomFields\EventListeners;
  3. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use UVDesk\CommunityPackages\UVDesk\CustomFields\Services\CustomFieldsService;
  11. class RequestEventListener
  12. {
  13.     private $firewall;
  14.     public function __construct(FirewallMap $firewallCustomFieldsService $customFields)
  15.     {
  16.         $this->firewall $firewall;
  17.         $this->customFields $customFields;
  18.     }
  19.     public function onKernelRequest(RequestEvent $event)
  20.     {
  21.         if (!$event->isMasterRequest()) {
  22.             return;
  23.         }
  24.         
  25.         $request $event->getRequest();
  26.         if ('OPTIONS' == $request->getRealMethod()) {
  27.             $event->setResponse(new Response());
  28.         }
  29.         return;
  30.     }
  31.     public function onKernelResponse(ResponseEvent $event)
  32.     {
  33.         $request $event->getRequest();
  34.         
  35.         if (!$event->isMasterRequest()) {
  36.             return;
  37.         }
  38.         $response $event->getResponse();
  39.         if ('OPTIONS' == $request->getRealMethod() || 'POST' == $request->getRealMethod() || 'GET' == $request->getRealMethod()) {
  40.             $response->headers->set('Access-Control-Allow-Origin''*');
  41.             $response->headers->set('Access-Control-Allow-Methods''GET,POST,PUT,OPTIONS');
  42.             $response->headers->set('Access-Control-Allow-Headers', ['Access-Control-Allow-Origin''Authorization''Content-Type']);
  43.         }
  44.         $supportedRoutes = [
  45.             'uvdesk_api_bundle_api_tickets_v1.0_view_ticket'
  46.             'uvdesk_api_bundle_api_tickets_v1.0_fetch_tickets'
  47.         ];
  48.         if (in_array($request->get('_route'), $supportedRoutes)) {
  49.             $responseContent json_decode($response->getContent(), true);
  50.             switch ($request->get('_route')) {
  51.                 case 'uvdesk_api_bundle_api_tickets_v1.0_view_ticket':
  52.                     if (!empty($responseContent['ticket'])) {
  53.                         $responseContent['ticket']['customFields'] = $this->customFields->getTicketCustomFieldDetails($responseContent['ticket']['id']);
  54.                     }
  55.                     break;
  56.                 case 'uvdesk_api_bundle_api_tickets_v1.0_fetch_tickets':
  57.                     if (!empty($responseContent['tickets'])) {
  58.                         $ticketReferenceIds array_map(function ($ticket) {
  59.                             return $ticket['id'];
  60.                         }, $responseContent['tickets']);
  61.                         $ticketCustomFieldDetailsCollection $this->customFields->getTicketCustomFieldDetailsCollection($ticketReferenceIds);
  62.                         foreach ($responseContent['tickets'] as $index => $ticket) {
  63.                             $responseContent['tickets'][$index]['customFields'] = [];
  64.                             
  65.                             if (!empty($ticketCustomFieldDetailsCollection[$ticket['id']])) {
  66.                                 $responseContent['tickets'][$index]['customFields'] = $ticketCustomFieldDetailsCollection[$ticket['id']];
  67.                             }
  68.                         }
  69.                     }
  70.                     break;
  71.                 default:
  72.                     break;
  73.             }
  74.             $response->setContent(json_encode($responseContent));
  75.         }
  76.         
  77.         return;
  78.     }
  79. }