|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\PostNord\Client; |
| 6 | + |
| 7 | +use Psr\Http\Client\ClientExceptionInterface; |
| 8 | +use Psr\Http\Client\ClientInterface as HttpClientInterface; |
| 9 | +use Psr\Http\Message\RequestFactoryInterface; |
| 10 | +use Psr\Http\Message\StreamFactoryInterface; |
| 11 | +use Safe\Exceptions\JsonException; |
| 12 | +use Safe\Exceptions\StringsException; |
| 13 | +use function Safe\json_decode; |
| 14 | +use function Safe\json_encode; |
| 15 | +use Setono\PostNord\Exception\RequestFailedException; |
| 16 | + |
| 17 | +final class Client implements ClientInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var HttpClientInterface |
| 21 | + */ |
| 22 | + private $httpClient; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var RequestFactoryInterface |
| 26 | + */ |
| 27 | + private $requestFactory; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var StreamFactoryInterface |
| 31 | + */ |
| 32 | + private $streamFactory; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + private $apiKey; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + private $baseUrl; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + HttpClientInterface $httpClient, |
| 46 | + RequestFactoryInterface $requestFactory, |
| 47 | + StreamFactoryInterface $streamFactory, |
| 48 | + string $apiKey, |
| 49 | + string $baseUrl = 'https://api2.postnord.com' |
| 50 | + ) { |
| 51 | + $this->httpClient = $httpClient; |
| 52 | + $this->requestFactory = $requestFactory; |
| 53 | + $this->streamFactory = $streamFactory; |
| 54 | + $this->apiKey = $apiKey; |
| 55 | + $this->baseUrl = $baseUrl; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param string $endpoint |
| 60 | + * @param array $params |
| 61 | + * |
| 62 | + * @return array |
| 63 | + * |
| 64 | + * @throws ClientExceptionInterface |
| 65 | + * @throws JsonException |
| 66 | + * @throws StringsException |
| 67 | + */ |
| 68 | + public function get(string $endpoint, array $params = []): array |
| 69 | + { |
| 70 | + return $this->sendRequest('GET', $endpoint, $params); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param string $endpoint |
| 75 | + * @param array $params |
| 76 | + * @param array $body |
| 77 | + * |
| 78 | + * @return array |
| 79 | + * |
| 80 | + * @throws ClientExceptionInterface |
| 81 | + * @throws JsonException |
| 82 | + * @throws StringsException |
| 83 | + */ |
| 84 | + public function post(string $endpoint, array $params = [], array $body = []): array |
| 85 | + { |
| 86 | + return $this->sendRequest('POST', $endpoint, $params, $body); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param string $method |
| 91 | + * @param string $endpoint |
| 92 | + * @param array $params |
| 93 | + * @param array $body |
| 94 | + * |
| 95 | + * @return array |
| 96 | + * |
| 97 | + * @throws ClientExceptionInterface |
| 98 | + * @throws JsonException |
| 99 | + * @throws StringsException |
| 100 | + */ |
| 101 | + private function sendRequest(string $method, string $endpoint, array $params = [], array $body = []): array |
| 102 | + { |
| 103 | + $params = array_merge([ |
| 104 | + 'apikey' => $this->apiKey, |
| 105 | + ], $params); |
| 106 | + |
| 107 | + $url = $this->baseUrl.'/'.$endpoint.'?'.http_build_query($params, '', '&', PHP_QUERY_RFC3986); |
| 108 | + |
| 109 | + $request = $this->requestFactory->createRequest($method, $url); |
| 110 | + |
| 111 | + if (!empty($body)) { |
| 112 | + $request = $request->withBody($this->streamFactory->createStream(json_encode($body))); |
| 113 | + } |
| 114 | + |
| 115 | + $response = $this->httpClient->sendRequest($request); |
| 116 | + |
| 117 | + if (200 !== $response->getStatusCode()) { |
| 118 | + throw new RequestFailedException($request, $response, $response->getStatusCode()); |
| 119 | + } |
| 120 | + |
| 121 | + return (array) json_decode((string) $response->getBody(), true); |
| 122 | + } |
| 123 | +} |
0 commit comments