src/Client/Tapfiliate/Service/ClientAbstract.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Client\Tapfiliate\Service;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\Serializer\SerializerInterface;
  9. abstract class ClientAbstract
  10. {
  11.     private const BASE_URI 'https://api.tapfiliate.com';
  12.     private string $baseUri;
  13.     private string $token;
  14.     private Client $client;
  15.     private SerializerInterface $serializer;
  16.     private LoggerInterface $logger;
  17.     public function __construct(string $tokenSerializerInterface $serializerLoggerInterface $logger)
  18.     {
  19.         $this->baseUri self::BASE_URI;
  20.         $this->token $token;
  21.         $this->client = new Client(
  22.             [
  23.                 'headers' => [
  24.                     'X-Api-Key' => $this->token,
  25.                 ],
  26.                 'connect_timeout' => 10,
  27.                 'base_uri' => $this->baseUri,
  28.             ]
  29.         );
  30.         $this->serializer $serializer;
  31.         $this->logger $logger;
  32.     }
  33.     /**
  34.      * @throws GuzzleException|\Throwable
  35.      */
  36.     protected function get(string $endpoint, array $requestData = []): ResponseInterface
  37.     {
  38.         return $this->client->request('GET'$endpoint$requestData);
  39.     }
  40.     /**
  41.      * @throws GuzzleException
  42.      */
  43.     protected function post(string $endpoint, array $bodyRequestData = []): ResponseInterface
  44.     {
  45.         $bodyRequestContents $this->serializer->serialize($bodyRequestData'json');
  46.         return $this->client->request('POST'$endpoint, ['body' => $bodyRequestContents]);
  47.     }
  48.     /**
  49.      * @throws GuzzleException
  50.      */
  51.     protected function patch(string $endpoint, array $bodyRequestData = []): ResponseInterface
  52.     {
  53.         $bodyRequestContents $this->serializer->serialize($bodyRequestData'json');
  54.         return $this->client->request('PATCH'$endpoint, ['body' => $bodyRequestContents]);
  55.     }
  56.     public function getSerializer(): SerializerInterface
  57.     {
  58.         return $this->serializer;
  59.     }
  60.     public function getLogger(): LoggerInterface
  61.     {
  62.         return $this->logger;
  63.     }
  64. }