src/Bundle/Croatia/EventListener/GenticsMeshApiAuthListener.php line 88

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundle\Croatia\EventListener;
  4. use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEventListenerInterface;
  5. use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
  6. use GuzzleHttp\Client;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider\JWSProviderInterface;
  8. use Psr\Cache\CacheItemPoolInterface;
  9. class GenticsMeshApiAuthListener implements GuzzleEventListenerInterface
  10. {
  11.     /**
  12.      * @var string
  13.      */
  14.     protected $serviceName;
  15.     /**
  16.      * @var CacheItemPoolInterface
  17.      */
  18.     protected $cache;
  19.     /**
  20.      * @var Client
  21.      */
  22.     protected $client;
  23.     /**
  24.      * @var JWSProviderInterface
  25.      */
  26.     protected $JWSProvider;
  27.     public function __construct(CacheItemPoolInterface $cacheClient $clientJWSProviderInterface $JWSProvider)
  28.     {
  29.         $this->cache $cache;
  30.         $this->client $client;
  31.         $this->JWSProvider $JWSProvider;
  32.     }
  33.     public function getServiceName()
  34.     {
  35.         return $this->serviceName;
  36.     }
  37.     /**
  38.      * @inheritDoc
  39.      */
  40.     public function setServiceName($serviceName)
  41.     {
  42.         $this->serviceName $serviceName;
  43.         return $this;
  44.     }
  45.     /**
  46.      * @return string
  47.      * @throws \Psr\Cache\InvalidArgumentException
  48.      */
  49.     protected function getToken(): string
  50.     {
  51.         $cacheItem $this->cache->getItem('gentics_mesh_token');
  52.         if ($cacheItem->isHit()) {
  53.             $token $cacheItem->get();
  54.             return $token['token'];
  55.         }
  56.         $body = [
  57.             'username' => getenv('GETMESH_USER'),
  58.             'password' => getenv('GETMESH_PASSWORD')
  59.         ];
  60.         $response $this->client->post('/api/v1/auth/login', ['body' => json_encode($body)]);
  61.         $token json_decode($response->getBody()->getContents(), true);
  62.         $payload $this->JWSProvider->load($token['token'])->getPayload();
  63.         $expiresAt = (new \DateTimeImmutable())->setTimestamp($payload['exp']);
  64.         $cacheItem->expiresAt($expiresAt);
  65.         $cacheItem->set($token);
  66.         $this->cache->save($cacheItem);
  67.         return $token['token'];
  68.     }
  69.     /**
  70.      * @param PreTransactionEvent $event
  71.      *
  72.      * @throws \Psr\Cache\InvalidArgumentException
  73.      */
  74.     public function onPreTransaction(PreTransactionEvent $event)
  75.     {
  76.         if ($event->getServiceName() !== $this->getServiceName()) {
  77.             return;
  78.         }
  79.         $token $this->getToken();
  80.         $request $event->getTransaction();
  81.         $modifiedRequest $request->withHeader('Authorization'sprintf('Bearer %s'$token));
  82.         $event->setTransaction($modifiedRequest);
  83.     }
  84. }