|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file has been created by developers from BitBag. |
| 5 | + * Feel free to contact us once you face any issues or want to start |
| 6 | + * You can find more information about us on https://bitbag.io and write us |
| 7 | + * an email on hello@bitbag.io. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace BitBag\SyliusUserComPlugin\Api; |
| 13 | + |
| 14 | +use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 18 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 19 | + |
| 20 | +abstract class AbstractClient |
| 21 | +{ |
| 22 | + private const API_ENDPOINT_PREFIX = '/api/public/'; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private readonly HttpClientInterface $client, |
| 26 | + private readonly LoggerInterface $logger, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + protected function request( |
| 31 | + string $path, |
| 32 | + string $method, |
| 33 | + array $options, |
| 34 | + ): ?array { |
| 35 | + try { |
| 36 | + /** @var ResponseInterface $response */ |
| 37 | + $response = $this->client->request( |
| 38 | + $method, |
| 39 | + $path, |
| 40 | + $options, |
| 41 | + ); |
| 42 | + |
| 43 | + $status = $response->getStatusCode(); |
| 44 | + if ($status >= Response::HTTP_OK && $status < Response::HTTP_MULTIPLE_CHOICES) { |
| 45 | + return $response->toArray(); |
| 46 | + } |
| 47 | + |
| 48 | + throw new \Exception( |
| 49 | + sprintf( |
| 50 | + 'Response status code : %s, response : %s', |
| 51 | + $status, |
| 52 | + $response->getContent(false), |
| 53 | + ), |
| 54 | + ); |
| 55 | + } catch (\Exception $e) { |
| 56 | + $this->logger->critical(sprintf( |
| 57 | + 'User.com API request failed: %s', |
| 58 | + $e->getMessage(), |
| 59 | + ), [ |
| 60 | + 'path' => $path, |
| 61 | + 'method' => $method, |
| 62 | + 'options' => $options, |
| 63 | + ]); |
| 64 | + |
| 65 | + return null; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected function getApiEndpointUrl( |
| 70 | + UserComApiAwareInterface $resource, |
| 71 | + string $endpoint, |
| 72 | + string $query = null, |
| 73 | + ): string { |
| 74 | + if (null === $resource->getUserComUrl()) { |
| 75 | + throw new \InvalidArgumentException('User.com API key is missing.'); |
| 76 | + } |
| 77 | + |
| 78 | + $url = sprintf( |
| 79 | + '%s/%s/%s/', |
| 80 | + trim($resource->getUserComUrl(), '/'), |
| 81 | + trim(self::API_ENDPOINT_PREFIX, '/'), |
| 82 | + trim($endpoint, '/'), |
| 83 | + ); |
| 84 | + |
| 85 | + if (null === $query) { |
| 86 | + return $url; |
| 87 | + } |
| 88 | + |
| 89 | + return sprintf('%s%s/', $url, $query); |
| 90 | + } |
| 91 | + |
| 92 | + protected function buildOptions( |
| 93 | + UserComApiAwareInterface $resource, |
| 94 | + array $options = [], |
| 95 | + ): array { |
| 96 | + $options['headers']['Accept'] = ' */*; version=2'; |
| 97 | + $options['headers']['Authorization'] = $this->authorizeRequest($resource); |
| 98 | + |
| 99 | + return $options; |
| 100 | + } |
| 101 | + |
| 102 | + protected function authorizeRequest( |
| 103 | + UserComApiAwareInterface $resource, |
| 104 | + ): string { |
| 105 | + if (null === $resource->getUserComApiKey()) { |
| 106 | + throw new \InvalidArgumentException('User.com API key is missing.'); |
| 107 | + } |
| 108 | + |
| 109 | + return sprintf( |
| 110 | + 'Token %s', |
| 111 | + $resource->getUserComApiKey(), |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments