|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Square\Channels; |
| 4 | + |
| 5 | +use GuzzleHttp\ClientInterface; |
| 6 | +use Square\Core\Client\RawClient; |
| 7 | +use Square\Channels\Requests\ListChannelsRequest; |
| 8 | +use Square\Core\Pagination\Pager; |
| 9 | +use Square\Types\Channel; |
| 10 | +use Square\Core\Pagination\CursorPager; |
| 11 | +use Square\Types\ListChannelsResponse; |
| 12 | +use Square\Channels\Requests\BulkRetrieveChannelsRequest; |
| 13 | +use Square\Types\BulkRetrieveChannelsResponse; |
| 14 | +use Square\Exceptions\SquareException; |
| 15 | +use Square\Exceptions\SquareApiException; |
| 16 | +use Square\Core\Json\JsonApiRequest; |
| 17 | +use Square\Environments; |
| 18 | +use Square\Core\Client\HttpMethod; |
| 19 | +use JsonException; |
| 20 | +use GuzzleHttp\Exception\RequestException; |
| 21 | +use Psr\Http\Client\ClientExceptionInterface; |
| 22 | +use Square\Channels\Requests\GetChannelsRequest; |
| 23 | +use Square\Types\RetrieveChannelResponse; |
| 24 | + |
| 25 | +class ChannelsClient |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var array{ |
| 29 | + * baseUrl?: string, |
| 30 | + * client?: ClientInterface, |
| 31 | + * maxRetries?: int, |
| 32 | + * timeout?: float, |
| 33 | + * headers?: array<string, string>, |
| 34 | + * } $options |
| 35 | + */ |
| 36 | + private array $options; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var RawClient $client |
| 40 | + */ |
| 41 | + private RawClient $client; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param RawClient $client |
| 45 | + * @param ?array{ |
| 46 | + * baseUrl?: string, |
| 47 | + * client?: ClientInterface, |
| 48 | + * maxRetries?: int, |
| 49 | + * timeout?: float, |
| 50 | + * headers?: array<string, string>, |
| 51 | + * } $options |
| 52 | + */ |
| 53 | + public function __construct( |
| 54 | + RawClient $client, |
| 55 | + ?array $options = null, |
| 56 | + ) { |
| 57 | + $this->client = $client; |
| 58 | + $this->options = $options ?? []; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * |
| 63 | + * |
| 64 | + * @param ListChannelsRequest $request |
| 65 | + * @param ?array{ |
| 66 | + * baseUrl?: string, |
| 67 | + * maxRetries?: int, |
| 68 | + * timeout?: float, |
| 69 | + * headers?: array<string, string>, |
| 70 | + * queryParameters?: array<string, mixed>, |
| 71 | + * bodyProperties?: array<string, mixed>, |
| 72 | + * } $options |
| 73 | + * @return Pager<Channel> |
| 74 | + */ |
| 75 | + public function list(ListChannelsRequest $request = new ListChannelsRequest(), ?array $options = null): Pager |
| 76 | + { |
| 77 | + return new CursorPager( |
| 78 | + request: $request, |
| 79 | + getNextPage: fn (ListChannelsRequest $request) => $this->_list($request, $options), |
| 80 | + setCursor: function (ListChannelsRequest $request, ?string $cursor) { |
| 81 | + $request->setCursor($cursor); |
| 82 | + }, |
| 83 | + /* @phpstan-ignore-next-line */ |
| 84 | + getNextCursor: fn (ListChannelsResponse $response) => $response?->getCursor() ?? null, |
| 85 | + /* @phpstan-ignore-next-line */ |
| 86 | + getItems: fn (ListChannelsResponse $response) => $response?->getChannels() ?? [], |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * |
| 92 | + * |
| 93 | + * @param BulkRetrieveChannelsRequest $request |
| 94 | + * @param ?array{ |
| 95 | + * baseUrl?: string, |
| 96 | + * maxRetries?: int, |
| 97 | + * timeout?: float, |
| 98 | + * headers?: array<string, string>, |
| 99 | + * queryParameters?: array<string, mixed>, |
| 100 | + * bodyProperties?: array<string, mixed>, |
| 101 | + * } $options |
| 102 | + * @return BulkRetrieveChannelsResponse |
| 103 | + * @throws SquareException |
| 104 | + * @throws SquareApiException |
| 105 | + */ |
| 106 | + public function bulkRetrieve(BulkRetrieveChannelsRequest $request, ?array $options = null): BulkRetrieveChannelsResponse |
| 107 | + { |
| 108 | + $options = array_merge($this->options, $options ?? []); |
| 109 | + try { |
| 110 | + $response = $this->client->sendRequest( |
| 111 | + new JsonApiRequest( |
| 112 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 113 | + path: "v2/channels/bulk-retrieve", |
| 114 | + method: HttpMethod::POST, |
| 115 | + body: $request, |
| 116 | + ), |
| 117 | + $options, |
| 118 | + ); |
| 119 | + $statusCode = $response->getStatusCode(); |
| 120 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 121 | + $json = $response->getBody()->getContents(); |
| 122 | + return BulkRetrieveChannelsResponse::fromJson($json); |
| 123 | + } |
| 124 | + } catch (JsonException $e) { |
| 125 | + throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 126 | + } catch (RequestException $e) { |
| 127 | + $response = $e->getResponse(); |
| 128 | + if ($response === null) { |
| 129 | + throw new SquareException(message: $e->getMessage(), previous: $e); |
| 130 | + } |
| 131 | + throw new SquareApiException( |
| 132 | + message: "API request failed", |
| 133 | + statusCode: $response->getStatusCode(), |
| 134 | + body: $response->getBody()->getContents(), |
| 135 | + ); |
| 136 | + } catch (ClientExceptionInterface $e) { |
| 137 | + throw new SquareException(message: $e->getMessage(), previous: $e); |
| 138 | + } |
| 139 | + throw new SquareApiException( |
| 140 | + message: 'API request failed', |
| 141 | + statusCode: $statusCode, |
| 142 | + body: $response->getBody()->getContents(), |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * |
| 148 | + * |
| 149 | + * @param GetChannelsRequest $request |
| 150 | + * @param ?array{ |
| 151 | + * baseUrl?: string, |
| 152 | + * maxRetries?: int, |
| 153 | + * timeout?: float, |
| 154 | + * headers?: array<string, string>, |
| 155 | + * queryParameters?: array<string, mixed>, |
| 156 | + * bodyProperties?: array<string, mixed>, |
| 157 | + * } $options |
| 158 | + * @return RetrieveChannelResponse |
| 159 | + * @throws SquareException |
| 160 | + * @throws SquareApiException |
| 161 | + */ |
| 162 | + public function get(GetChannelsRequest $request, ?array $options = null): RetrieveChannelResponse |
| 163 | + { |
| 164 | + $options = array_merge($this->options, $options ?? []); |
| 165 | + try { |
| 166 | + $response = $this->client->sendRequest( |
| 167 | + new JsonApiRequest( |
| 168 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 169 | + path: "v2/channels/{$request->getChannelId()}", |
| 170 | + method: HttpMethod::GET, |
| 171 | + ), |
| 172 | + $options, |
| 173 | + ); |
| 174 | + $statusCode = $response->getStatusCode(); |
| 175 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 176 | + $json = $response->getBody()->getContents(); |
| 177 | + return RetrieveChannelResponse::fromJson($json); |
| 178 | + } |
| 179 | + } catch (JsonException $e) { |
| 180 | + throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 181 | + } catch (RequestException $e) { |
| 182 | + $response = $e->getResponse(); |
| 183 | + if ($response === null) { |
| 184 | + throw new SquareException(message: $e->getMessage(), previous: $e); |
| 185 | + } |
| 186 | + throw new SquareApiException( |
| 187 | + message: "API request failed", |
| 188 | + statusCode: $response->getStatusCode(), |
| 189 | + body: $response->getBody()->getContents(), |
| 190 | + ); |
| 191 | + } catch (ClientExceptionInterface $e) { |
| 192 | + throw new SquareException(message: $e->getMessage(), previous: $e); |
| 193 | + } |
| 194 | + throw new SquareApiException( |
| 195 | + message: 'API request failed', |
| 196 | + statusCode: $statusCode, |
| 197 | + body: $response->getBody()->getContents(), |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * |
| 203 | + * |
| 204 | + * @param ListChannelsRequest $request |
| 205 | + * @param ?array{ |
| 206 | + * baseUrl?: string, |
| 207 | + * maxRetries?: int, |
| 208 | + * timeout?: float, |
| 209 | + * headers?: array<string, string>, |
| 210 | + * queryParameters?: array<string, mixed>, |
| 211 | + * bodyProperties?: array<string, mixed>, |
| 212 | + * } $options |
| 213 | + * @return ListChannelsResponse |
| 214 | + * @throws SquareException |
| 215 | + * @throws SquareApiException |
| 216 | + */ |
| 217 | + private function _list(ListChannelsRequest $request = new ListChannelsRequest(), ?array $options = null): ListChannelsResponse |
| 218 | + { |
| 219 | + $options = array_merge($this->options, $options ?? []); |
| 220 | + $query = []; |
| 221 | + if ($request->getReferenceType() != null) { |
| 222 | + $query['reference_type'] = $request->getReferenceType(); |
| 223 | + } |
| 224 | + if ($request->getReferenceId() != null) { |
| 225 | + $query['reference_id'] = $request->getReferenceId(); |
| 226 | + } |
| 227 | + if ($request->getStatus() != null) { |
| 228 | + $query['status'] = $request->getStatus(); |
| 229 | + } |
| 230 | + if ($request->getCursor() != null) { |
| 231 | + $query['cursor'] = $request->getCursor(); |
| 232 | + } |
| 233 | + if ($request->getLimit() != null) { |
| 234 | + $query['limit'] = $request->getLimit(); |
| 235 | + } |
| 236 | + try { |
| 237 | + $response = $this->client->sendRequest( |
| 238 | + new JsonApiRequest( |
| 239 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 240 | + path: "v2/channels", |
| 241 | + method: HttpMethod::GET, |
| 242 | + query: $query, |
| 243 | + ), |
| 244 | + $options, |
| 245 | + ); |
| 246 | + $statusCode = $response->getStatusCode(); |
| 247 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 248 | + $json = $response->getBody()->getContents(); |
| 249 | + return ListChannelsResponse::fromJson($json); |
| 250 | + } |
| 251 | + } catch (JsonException $e) { |
| 252 | + throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 253 | + } catch (RequestException $e) { |
| 254 | + $response = $e->getResponse(); |
| 255 | + if ($response === null) { |
| 256 | + throw new SquareException(message: $e->getMessage(), previous: $e); |
| 257 | + } |
| 258 | + throw new SquareApiException( |
| 259 | + message: "API request failed", |
| 260 | + statusCode: $response->getStatusCode(), |
| 261 | + body: $response->getBody()->getContents(), |
| 262 | + ); |
| 263 | + } catch (ClientExceptionInterface $e) { |
| 264 | + throw new SquareException(message: $e->getMessage(), previous: $e); |
| 265 | + } |
| 266 | + throw new SquareApiException( |
| 267 | + message: 'API request failed', |
| 268 | + statusCode: $statusCode, |
| 269 | + body: $response->getBody()->getContents(), |
| 270 | + ); |
| 271 | + } |
| 272 | +} |
0 commit comments