<?php
namespace App\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class BaseService
{
/** @var HttpClientInterface */
protected $client;
/** @var SessionInterface */
protected $session;
/** @var ContainerInterface */
protected $container;
/** @var Filesystem $filesystem */
protected $filesystem;
public function __construct(HttpClientInterface $client, RequestStack $stack, ContainerInterface $container)
{
$this->client = $client;
$this->session = $stack->getSession();
$this->container = $container;
$this->filesystem = new Filesystem();
$this->session->start();
}
/**
* Devuelve los headers ya autorizados que se usan en cada petición
* @return string[]
*/
protected function getAuthorizedHeaders(?string $user = null, ?string $password = null): array
{
if (!$user && !$password) {
$user = $this->session->get('user');
$password = $this->session->get('password');
}
return [
'x-api-key' => $this->getParameter("api_key"),
'Authorization' => 'Basic ' . base64_encode($user . ':' . sha1($password))
];
}
/**
* Devuelve el parámetro del contenedor
* @param string $parameter
* @return mixed
*/
protected function getParameter(string $parameter)
{
return $this->container->getParameter($parameter);
}
}