vendor/comalia/gesica_bundle/Service/HttpClient.php line 66

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Comalia\GesicaBundle\Service;
  4. use Symfony\Component\HttpClient\DecoratorTrait;
  5. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpKernel\KernelInterface;
  8. use Symfony\Contracts\HttpClient\HttpClientInterface;
  9. use Symfony\Contracts\HttpClient\ResponseInterface;
  10. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. /**
  13.  * Class HttpClient
  14.  *
  15.  * Cette class décore le HttpClient de Symfony pour ajouter automatiquement
  16.  * le correlation ID dans le header de toutes les "Request" HTTP sortantes
  17.  *
  18.  * Pour plus d'informations sur le pattern "Decorator' :
  19.  * https://symfony.com/doc/current/service_container/service_decoration.html
  20.  */
  21. class HttpClient implements HttpClientInterface
  22. {
  23.     // Implémentation de la méthode withOptions demandé par l'interface HttpClientInterface
  24.     use DecoratorTrait;
  25.     /**
  26.      * @var HttpClientInterface
  27.      */
  28.     private $decorated;
  29.     /**
  30.      * @var RequestStack
  31.      */
  32.     private $requestStack;
  33.     /**
  34.      * @var KernelInterface
  35.      */
  36.     private $kernel;
  37.     /**
  38.      * HttpClient constructor.
  39.      * @param HttpClientInterface $decorated
  40.      * @param RequestStack $requestStack
  41.      * @param KernelInterface $kernel
  42.      */
  43.     public function __construct(HttpClientInterface $decoratedRequestStack $requestStackKernelInterface $kernel)
  44.     {
  45.         $this->decorated $decorated;
  46.         $this->requestStack $requestStack;
  47.         $this->kernel $kernel;
  48.     }
  49.     /**
  50.      * @inheritDoc
  51.      */
  52.     public function request(string $methodstring $url, array $options = []): ResponseInterface
  53.     {
  54.         $session $this->getSession();
  55.         if ($session) {
  56.             $options['headers']['x-correlation-id'] = $session->get('x-correlation-id');
  57.         }
  58.         return $this->decorated->request($method$url$options);
  59.     }
  60.     /**
  61.      * @inheritDoc
  62.      */
  63.     public function stream($responsesfloat $timeout null): ResponseStreamInterface
  64.     {
  65.         return $this->decorated->stream($responses$timeout);
  66.     }
  67.     /**
  68.      * @return SessionInterface
  69.      */
  70.     private function getSession(): ?SessionInterface
  71.     {
  72.         try {
  73.             return $this->requestStack->getSession();
  74.         } catch (SessionNotFoundException $ex) {
  75.             return null;
  76.         }
  77.     }
  78. }