src/EventListener/NeutralApiAuthListener.php line 82

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEventListenerInterface;
  5. use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
  6. use GuzzleHttp\Client;
  7. use Psr\Cache\CacheItemPoolInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class NeutralApiAuthListener implements GuzzleEventListenerInterface
  10. {
  11.     protected $serviceName;
  12.     /**
  13.      * @var CacheItemPoolInterface
  14.      */
  15.     protected $cache;
  16.     /**
  17.      * @var Client
  18.      */
  19.     protected $client;
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     protected $requestStack;
  24.     public function __construct(CacheItemPoolInterface $cacheClient $clientRequestStack $requestStack)
  25.     {
  26.         $this->cache $cache;
  27.         $this->client $client;
  28.         $this->requestStack $requestStack;
  29.     }
  30.     public function getServiceName()
  31.     {
  32.         return $this->serviceName;
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public function setServiceName($serviceName)
  38.     {
  39.         $this->serviceName $serviceName;
  40.         return $this;
  41.     }
  42.     /**
  43.      * @return string
  44.      * @throws \Psr\Cache\InvalidArgumentException
  45.      */
  46.     protected function getToken(): string
  47.     {
  48.         $cacheItem $this->cache->getItem('neutral_token');
  49.         if (!$cacheItem->isHit()) {
  50.             $response $this->client->post('/restV/oauth/token', [
  51.                 'multipart' => [
  52.                     ['name' => 'grant_type''contents' => 'client_credentials']
  53.                 ]
  54.             ]);
  55.             $token json_decode($response->getBody()->getContents(), true);
  56.             $cacheItem->set($token);
  57.             $minutes intval($token['expires_in'] / 60);
  58.             $cacheItem->expiresAfter(\DateInterval::createFromDateString(sprintf('+ %s minutes'$minutes)));
  59.             $this->cache->save($cacheItem);
  60.         }
  61.         $token $cacheItem->get();
  62.         return $token['access_token'];
  63.     }
  64.     /**
  65.      * @param PreTransactionEvent $event
  66.      *
  67.      * @throws \Psr\Cache\InvalidArgumentException
  68.      */
  69.     public function onPreTransaction(PreTransactionEvent $event)
  70.     {
  71.         if ($event->getServiceName() !== $this->getServiceName()) {
  72.             return;
  73.         }
  74.         $token $this->getToken();
  75.         $transaction $event->getTransaction();
  76.         $modifiedRequest $transaction->withHeader('Authorization'sprintf('Bearer %s'$token));
  77.         if ($request $this->requestStack->getMasterRequest()) {
  78.             if (!$language $modifiedRequest->getHeader('Accept-Language')) {
  79.                 $modifiedRequest $modifiedRequest->withHeader('Accept-Language'$request->getLocale());
  80.             }
  81.         }
  82.         $event->setTransaction($modifiedRequest);
  83.     }
  84. }