src/EventListener/USTServiceEventListener.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Event\USTServicePayloadEvent;
  4. use App\Logger\UstServiceTrafficLogger;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. class USTServiceEventListener implements EventSubscriberInterface
  8. {
  9.     private $trafficLoggingEnabled;
  10.     private $logger;
  11.     public function __construct(bool $trafficLoggingEnabledUstServiceTrafficLogger $logger)
  12.     {
  13.         $this->trafficLoggingEnabled $trafficLoggingEnabled;
  14.         $this->logger $logger;
  15.     }
  16.     /**
  17.      * Logs traffic sent from mobile devices and UST service automatically.
  18.      *
  19.      * Note: If the route name is changed it should also be reflected here.
  20.      *
  21.      * @param RequestEvent $event
  22.      */
  23.     public function onKernelRequest(RequestEvent $event)
  24.     {
  25.         if (!$event->isMasterRequest()) {
  26.             // don't do anything if it's not the master request
  27.             return;
  28.         }
  29.         $request $event->getRequest();
  30.         $route $request->attributes->get('_route');
  31.         // Update package, called by mobile devices
  32.         if ($route == 'app_api_ibeaconlastseen_package') {
  33.             $this->logTrafficContent('package_log'$request->getContent());
  34.         }
  35.         // Update package, called by UST service
  36.         if ($route == 'app_api_ibeaconlastseen_update') {
  37.             $decoded base64_decode($request->getContent());
  38.             $packages json_decode($decodedtrue);
  39.             $payload json_encode($packagesJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE);
  40.             $this->logTrafficContent('update_request_log'$payload);
  41.         }
  42.         // Update post test completetion status, called by google form submission
  43.         if ($route == 'app_api_posttestcompletion_update') {
  44.             $this->logTrafficContent('test_update_request_log'$request->getContent());
  45.         }
  46.     }
  47.     /**
  48.      * Logs UST service traffic event raised by application code.
  49.      *
  50.      * @param USTServicePayloadEvent $event
  51.      */
  52.     public function onPayloadEvent(USTServicePayloadEvent $event)
  53.     {
  54.         if ($this->trafficLoggingEnabled) {
  55.             $this->logTrafficContent(
  56.                 $event->getCategory(),
  57.                 $event->getPayload()
  58.             );
  59.         }
  60.     }
  61.     private function logTrafficContent($category$payload)
  62.     {
  63.         if ($this->trafficLoggingEnabled) {
  64.             $this->logger->write($category$payload);
  65.         }
  66.     }
  67.     public static function getSubscribedEvents()
  68.     {
  69.         return [
  70.             RequestEvent::class => 'onKernelRequest',
  71.             USTServicePayloadEvent::class => 'onPayloadEvent',
  72.         ];
  73.     }
  74. }