src/EventSubscriber/RequestDecryptingSubscriber.php line 76

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license SILK SOFTWARE HOUSE SP Z O O
  4.  */
  5. namespace App\EventSubscriber;
  6. use App\Decoder\Interfaces\DecoderInterface;
  7. use App\Helpers\TimezoneHelper;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. /**
  14.  * Class RequestDecryptingSubscriber.
  15.  */
  16. class RequestDecryptingSubscriber implements EventSubscriberInterface
  17. {
  18.     const MEASUREMENT_TIME 'measurement_time';
  19.     const START_TIME 'start_time';
  20.     const SERIAL_NUMBER 'serial';
  21.     /**
  22.      * Decoder.
  23.      *
  24.      * @var DecoderInterface
  25.      */
  26.     private $decoder;
  27.     /**
  28.      * Device ping path.
  29.      *
  30.      * @var string[]
  31.      */
  32.     private $encryptedPaths;
  33.     /**
  34.      * @var LoggerInterface
  35.      */
  36.     private $logger;
  37.     /**
  38.      * RequestDecryptingSubscriber constructor.
  39.      *
  40.      * @param DecoderInterface $decoder
  41.      * @param string[]         $encryptedPaths Array of encrypted paths.
  42.      */
  43.     public function __construct(DecoderInterface $decoder, array $encryptedPathsLoggerInterface $logger)
  44.     {
  45.         $this->decoder $decoder;
  46.         $this->encryptedPaths $encryptedPaths;
  47.     }
  48.     /**
  49.      * Get subscribed events.
  50.      *
  51.      * @return array
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             KernelEvents::REQUEST => [
  57.                 ['onKernelRequest'9999],
  58.             ],
  59.         ];
  60.     }
  61.     /**
  62.      * On kernel request.
  63.      *
  64.      * @param RequestEvent $event
  65.      *
  66.      * @throws AccessDeniedHttpException
  67.      */
  68.     public function onKernelRequest(RequestEvent $event)
  69.     {
  70.         $request $event->getRequest();
  71.         if (!$event->isMasterRequest() || !in_array($event->getRequest()->getPathInfo(), $this->encryptedPaths)) {
  72.             return;
  73.         }
  74.         $request->headers->set('accept''application/ld+json');
  75.         $request->headers->set('content-type''application/ld+json');
  76.         $decrypted $this->decoder->decrypt($request->getContent())
  77.             ?? $this->decoder->decrypt($request->getContent(), ['non-json' => true]);
  78.         if (null === $decrypted) {
  79.             throw new AccessDeniedHttpException();
  80.         }
  81.         foreach ($decrypted as $key => $value) {
  82.             $request->request->set($key$value);
  83.         }
  84.         $timezoneHelper = new TimezoneHelper();
  85.         if (!isset($decrypted[self::SERIAL_NUMBER])) {
  86.             $request->request->set(
  87.                 self::SERIAL_NUMBER,
  88.                 $this->decoder->getLastCorrectPassword()
  89.             );
  90.         }
  91.         if (!empty($decrypted[self::MEASUREMENT_TIME])) {
  92.             $request->request->set(
  93.                 self::MEASUREMENT_TIME,
  94.                 $timezoneHelper->getCorrectedTime($decrypted[self::MEASUREMENT_TIME], $decrypted[self::START_TIME])
  95.             );
  96.         }
  97.         if (!empty($decrypted[self::START_TIME])) {
  98.             $request->request->set(
  99.                 self::START_TIME,
  100.                 $timezoneHelper->getCorrectedTime($decrypted[self::START_TIME], $decrypted[self::START_TIME])
  101.             );
  102.         }
  103.     }
  104. }