<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Contracts\HttpFoundation\RequestInfoInterface;
use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEventListenerInterface;
use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class SalesChannelListener implements GuzzleEventListenerInterface
{
const BASE_URL = '/WSVistaWebClient';
/**
* @var string
*/
private $serviceName;
/**
* @var string[]
*/
private $urlStack = [
'/RESTBooking.svc',
'/RESTLoyalty.svc/member/transactions',
'/RESTTicketing.svc/order',
'/orders',
'/RESTLoyalty.svc/member/validate',
'/RESTData.svc/concession-items'
];
/** @var string[] */
private $excludeUrlStack = [
'/RESTTicketing.svc/order/cancel'
];
/**
* @var RequestInfoInterface
*/
private $requestInfo;
public function __construct(RequestInfoInterface $requestInfo)
{
$this->requestInfo = $requestInfo;
}
/**
* @inheritDoc
*/
public function setServiceName($serviceName)
{
$this->serviceName = $serviceName;
return $this;
}
/**
* @return string
*/
public function getServiceName()
{
return $this->serviceName;
}
private function match(string $url, array $urlStack): bool
{
foreach ($urlStack as $match) {
if (false !== strpos($url, $this::BASE_URL . $match)) {
return true;
}
}
return false;
}
/**
* @see https://jira.scnsoft.com/browse/VAPCTCKETINGSUITE-325
*
* @param RequestInterface $transaction
*
* @return string
*/
private function getPlatform(RequestInterface $transaction): ?string
{
if ($this->match($transaction->getUri()->getPath(), $this->excludeUrlStack)) {
return null;
}
if ($this->match($transaction->getUri()->getPath(), $this->urlStack)) {
return $this->requestInfo->getPlatformName();
}
return null;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$request->attributes->set($this->requestInfo::PLATFORM_ATTRIBUTE, $this->requestInfo->getPlatformName());
}
public function onPreTransaction(PreTransactionEvent $event)
{
if ($event->getServiceName() !== $this->getServiceName()) {
return null;
}
$transaction = $event->getTransaction();
if ($transaction->hasHeader('OptionalClientId') || $transaction->hasHeader('connectapitoken')) {
return null;
}
switch ($this->getPlatform($transaction)) {
case $this->requestInfo::WEB_PLATFORM:
$modifiedRequest = $transaction
->withHeader('Saleschannel', getenv('VISTA_WEB_CHANNEL'))
->withHeader('OptionalClientId', getenv('VISTA_WEB_CLIENT_ID'))
->withHeader('connectapitoken', getenv('VISTA_WEB_TOKEN'));
break;
case $this->requestInfo::MOBILE_PLATFORM:
$modifiedRequest = $transaction
->withHeader('Saleschannel', getenv('VISTA_MOBILE_CHANNEL'))
->withHeader('OptionalClientId', getenv('VISTA_MOBILE_CLIENT_ID'))
->withHeader('connectapitoken', getenv('VISTA_MOBILE_TOKEN'));
break;
default:
$modifiedRequest = $transaction
->withHeader('Saleschannel', getenv('VISTA_MOBILE_CHANNEL'))
->withHeader('OptionalClientId', getenv('VISTA_CLIENT_ID'))
->withHeader('connectapitoken', getenv('VISTA_TOKEN'));
break;
}
$event->setTransaction($modifiedRequest);
}
}