src/Service/BaseService.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\Filesystem\Filesystem;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  9. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  11. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. class BaseService
  14. {
  15.     /** @var HttpClientInterface */
  16.     protected $client;
  17.     /** @var SessionInterface */
  18.     protected $session;
  19.     /** @var ContainerInterface */
  20.     protected $container;
  21.     /** @var Filesystem $filesystem */
  22.     protected $filesystem;
  23.     public function __construct(HttpClientInterface $clientRequestStack $stackContainerInterface $container)
  24.     {
  25.         $this->client $client;
  26.         $this->session $stack->getSession();
  27.         $this->container $container;
  28.         $this->filesystem = new Filesystem();
  29.         $this->session->start();
  30.     }
  31.     /**
  32.      * Devuelve los headers ya autorizados que se usan en cada petición
  33.      * @return string[]
  34.      */
  35.     protected function getAuthorizedHeaders(?string $user null, ?string $password null): array
  36.     {
  37.         if (!$user && !$password) {
  38.             $user $this->session->get('user');
  39.             $password $this->session->get('password');
  40.         }
  41.         return [
  42.             'x-api-key' => $this->getParameter("api_key"),
  43.             'Authorization' => 'Basic ' base64_encode($user ':' sha1($password))
  44.         ];
  45.     }
  46.     /**
  47.      * Devuelve el parámetro del contenedor
  48.      * @param string $parameter
  49.      * @return mixed
  50.      */
  51.     protected function getParameter(string $parameter)
  52.     {
  53.         return $this->container->getParameter($parameter);
  54.     }
  55. }