|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Khandurdyiev\MonoClient; |
| 6 | + |
| 7 | +use GuzzleHttp\Client as GuzzleClient; |
| 8 | +use GuzzleHttp\Exception\RequestException; |
| 9 | +use GuzzleHttp\Psr7\Message; |
| 10 | +use Khandurdyiev\MonoClient\Exceptions\MonobankApiException; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +class Client |
| 15 | +{ |
| 16 | + private const URL = 'https://api.monobank.ua'; |
| 17 | + |
| 18 | + private GuzzleClient $client; |
| 19 | + |
| 20 | + /** |
| 21 | + * @param array<string, mixed> $customConfig |
| 22 | + */ |
| 23 | + public function __construct(array $customConfig) |
| 24 | + { |
| 25 | + $config = array_merge(['base_uri' => self::URL], $customConfig); |
| 26 | + |
| 27 | + $this->client = new GuzzleClient($config); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param array<string, mixed> $customConfig |
| 32 | + * |
| 33 | + * @return Client |
| 34 | + */ |
| 35 | + public static function create(array $customConfig = []): Client |
| 36 | + { |
| 37 | + return new self($customConfig); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param string $uri |
| 42 | + * @param array<string, mixed> $options |
| 43 | + * |
| 44 | + * @return ResponseInterface |
| 45 | + * |
| 46 | + * @throws MonobankApiException |
| 47 | + */ |
| 48 | + public function get(string $uri, array $options = []): ResponseInterface |
| 49 | + { |
| 50 | + return $this->request('GET', $uri, $options); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param string $uri |
| 55 | + * @param array<string, mixed> $options |
| 56 | + * |
| 57 | + * @return ResponseInterface |
| 58 | + * |
| 59 | + * @throws MonobankApiException |
| 60 | + */ |
| 61 | + public function head(string $uri, array $options = []): ResponseInterface |
| 62 | + { |
| 63 | + return $this->request('HEAD', $uri, $options); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param string $uri |
| 68 | + * @param array<string, mixed> $options |
| 69 | + * |
| 70 | + * @return ResponseInterface |
| 71 | + * |
| 72 | + * @throws MonobankApiException |
| 73 | + */ |
| 74 | + public function put(string $uri, array $options = []): ResponseInterface |
| 75 | + { |
| 76 | + return $this->request('PUT', $uri, $options); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param string $uri |
| 81 | + * @param array<string, mixed> $options |
| 82 | + * |
| 83 | + * @return ResponseInterface |
| 84 | + * |
| 85 | + * @throws MonobankApiException |
| 86 | + */ |
| 87 | + public function post(string $uri, array $options = []): ResponseInterface |
| 88 | + { |
| 89 | + return $this->request('POST', $uri, $options); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param string $uri |
| 94 | + * @param array<string, mixed> $options |
| 95 | + * |
| 96 | + * @return ResponseInterface |
| 97 | + * |
| 98 | + * @throws MonobankApiException |
| 99 | + */ |
| 100 | + public function patch(string $uri, array $options = []): ResponseInterface |
| 101 | + { |
| 102 | + return $this->request('PATCH', $uri, $options); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param string $uri |
| 107 | + * @param array<string, mixed> $options |
| 108 | + * |
| 109 | + * @return ResponseInterface |
| 110 | + * |
| 111 | + * @throws MonobankApiException |
| 112 | + */ |
| 113 | + public function delete(string $uri, array $options = []): ResponseInterface |
| 114 | + { |
| 115 | + return $this->request('DELETE', $uri, $options); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param string $method |
| 120 | + * @param string $uri |
| 121 | + * @param array<string, mixed> $options |
| 122 | + * |
| 123 | + * @return ResponseInterface |
| 124 | + * |
| 125 | + * @throws MonobankApiException |
| 126 | + */ |
| 127 | + public function request(string $method, $uri = '', array $options = []): ResponseInterface |
| 128 | + { |
| 129 | + try { |
| 130 | + $response = $this->client->request($method, $uri, $options); |
| 131 | + } catch (Throwable $exception) { |
| 132 | + $message = $exception->getMessage(); |
| 133 | + |
| 134 | + if ($exception instanceof RequestException && $exception->hasResponse()) { |
| 135 | + /** @var ResponseInterface $response */ |
| 136 | + $response = $exception->getResponse(); |
| 137 | + |
| 138 | + $message = Message::toString($response); |
| 139 | + } |
| 140 | + |
| 141 | + throw new MonobankApiException($message, (int) $exception->getCode(), $exception); |
| 142 | + } |
| 143 | + |
| 144 | + return $response; |
| 145 | + } |
| 146 | +} |
0 commit comments