src/EventListener/SalesChannelListener.php line 96

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Contracts\HttpFoundation\RequestInfoInterface;
  5. use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEventListenerInterface;
  6. use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
  7. use Psr\Http\Message\RequestInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. class SalesChannelListener implements GuzzleEventListenerInterface
  10. {
  11.     const BASE_URL '/WSVistaWebClient';
  12.     /**
  13.      * @var string
  14.      */
  15.     private $serviceName;
  16.     /**
  17.      * @var string[]
  18.      */
  19.     private $urlStack = [
  20.         '/RESTBooking.svc',
  21.         '/RESTLoyalty.svc/member/transactions',
  22.         '/RESTTicketing.svc/order',
  23.         '/orders',
  24.         '/RESTLoyalty.svc/member/validate',
  25.         '/RESTData.svc/concession-items'
  26.     ];
  27.     /** @var string[] */
  28.     private $excludeUrlStack = [
  29.         '/RESTTicketing.svc/order/cancel'
  30.     ];
  31.     /**
  32.      * @var RequestInfoInterface
  33.      */
  34.     private $requestInfo;
  35.     public function __construct(RequestInfoInterface $requestInfo)
  36.     {
  37.         $this->requestInfo $requestInfo;
  38.     }
  39.     /**
  40.      * @inheritDoc
  41.      */
  42.     public function setServiceName($serviceName)
  43.     {
  44.         $this->serviceName $serviceName;
  45.         return $this;
  46.     }
  47.     /**
  48.      * @return string
  49.      */
  50.     public function getServiceName()
  51.     {
  52.         return $this->serviceName;
  53.     }
  54.     private function match(string $url, array $urlStack): bool
  55.     {
  56.         foreach ($urlStack as $match) {
  57.             if (false !== strpos($url$this::BASE_URL $match)) {
  58.                 return true;
  59.             }
  60.         }
  61.         return false;
  62.     }
  63.     /**
  64.      * @see https://jira.scnsoft.com/browse/VAPCTCKETINGSUITE-325
  65.      *
  66.      * @param RequestInterface $transaction
  67.      *
  68.      * @return string
  69.      */
  70.     private function getPlatform(RequestInterface $transaction): ?string
  71.     {
  72.         if ($this->match($transaction->getUri()->getPath(), $this->excludeUrlStack)) {
  73.             return null;
  74.         }
  75.         if ($this->match($transaction->getUri()->getPath(), $this->urlStack)) {
  76.             return $this->requestInfo->getPlatformName();
  77.         }
  78.         return null;
  79.     }
  80.     public function onKernelRequest(RequestEvent $event)
  81.     {
  82.         $request $event->getRequest();
  83.         $request->attributes->set($this->requestInfo::PLATFORM_ATTRIBUTE$this->requestInfo->getPlatformName());
  84.     }
  85.     public function onPreTransaction(PreTransactionEvent $event)
  86.     {
  87.         if ($event->getServiceName() !== $this->getServiceName()) {
  88.             return null;
  89.         }
  90.         $transaction $event->getTransaction();
  91.         if ($transaction->hasHeader('OptionalClientId') || $transaction->hasHeader('connectapitoken')) {
  92.             return null;
  93.         }
  94.         switch ($this->getPlatform($transaction)) {
  95.             case $this->requestInfo::WEB_PLATFORM:
  96.                 $modifiedRequest $transaction
  97.                     ->withHeader('Saleschannel'getenv('VISTA_WEB_CHANNEL'))
  98.                     ->withHeader('OptionalClientId'getenv('VISTA_WEB_CLIENT_ID'))
  99.                     ->withHeader('connectapitoken'getenv('VISTA_WEB_TOKEN'));
  100.                 break;
  101.             case $this->requestInfo::MOBILE_PLATFORM:
  102.                 $modifiedRequest $transaction
  103.                     ->withHeader('Saleschannel'getenv('VISTA_MOBILE_CHANNEL'))
  104.                     ->withHeader('OptionalClientId'getenv('VISTA_MOBILE_CLIENT_ID'))
  105.                     ->withHeader('connectapitoken'getenv('VISTA_MOBILE_TOKEN'));
  106.                 break;
  107.             default:
  108.                 $modifiedRequest $transaction
  109.                     ->withHeader('Saleschannel'getenv('VISTA_MOBILE_CHANNEL'))
  110.                     ->withHeader('OptionalClientId'getenv('VISTA_CLIENT_ID'))
  111.                     ->withHeader('connectapitoken'getenv('VISTA_TOKEN'));
  112.                 break;
  113.         }
  114.         $event->setTransaction($modifiedRequest);
  115.     }
  116. }