| 1 |
<?php |
| 2 |
|
| 3 |
namespace Square\Sites; |
| 4 |
|
| 5 |
use GuzzleHttp\ClientInterface; |
| 6 |
use Square\Core\Client\RawClient; |
| 7 |
use Square\Types\ListSitesResponse; |
| 8 |
use Square\Exceptions\SquareException; |
| 9 |
use Square\Exceptions\SquareApiException; |
| 10 |
use Square\Core\Json\JsonApiRequest; |
| 11 |
use Square\Environments; |
| 12 |
use Square\Core\Client\HttpMethod; |
| 13 |
use JsonException; |
| 14 |
use GuzzleHttp\Exception\RequestException; |
| 15 |
use Psr\Http\Client\ClientExceptionInterface; |
| 16 |
|
| 17 |
class SitesClient |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var array{ |
| 21 |
* baseUrl?: string, |
| 22 |
* client?: ClientInterface, |
| 23 |
* maxRetries?: int, |
| 24 |
* timeout?: float, |
| 25 |
* headers?: array<string, string>, |
| 26 |
* } $options |
| 27 |
*/ |
| 28 |
private array $options; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var RawClient $client |
| 32 |
*/ |
| 33 |
private RawClient $client; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param RawClient $client |
| 37 |
* @param ?array{ |
| 38 |
* baseUrl?: string, |
| 39 |
* client?: ClientInterface, |
| 40 |
* maxRetries?: int, |
| 41 |
* timeout?: float, |
| 42 |
* headers?: array<string, string>, |
| 43 |
* } $options |
| 44 |
*/ |
| 45 |
public function __construct( |
| 46 |
RawClient $client, |
| 47 |
?array $options = null, |
| 48 |
) { |
| 49 |
$this->client = $client; |
| 50 |
$this->options = $options ?? []; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Lists the Square Online sites that belong to a seller. Sites are listed in descending order by the `created_at` date. |
| 55 |
* |
| 56 |
* |
| 57 |
* __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis). |
| 58 |
* |
| 59 |
* @param ?array{ |
| 60 |
* baseUrl?: string, |
| 61 |
* maxRetries?: int, |
| 62 |
* timeout?: float, |
| 63 |
* headers?: array<string, string>, |
| 64 |
* queryParameters?: array<string, mixed>, |
| 65 |
* bodyProperties?: array<string, mixed>, |
| 66 |
* } $options |
| 67 |
* @return ListSitesResponse |
| 68 |
* @throws SquareException |
| 69 |
* @throws SquareApiException |
| 70 |
*/ |
| 71 |
public function list(?array $options = null): ListSitesResponse |
| 72 |
{ |
| 73 |
$options = array_merge($this->options, $options ?? []); |
| 74 |
try { |
| 75 |
$response = $this->client->sendRequest( |
| 76 |
new JsonApiRequest( |
| 77 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 78 |
path: "v2/sites", |
| 79 |
method: HttpMethod::GET, |
| 80 |
), |
| 81 |
$options, |
| 82 |
); |
| 83 |
$statusCode = $response->getStatusCode(); |
| 84 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 85 |
$json = $response->getBody()->getContents(); |
| 86 |
return ListSitesResponse::fromJson($json); |
| 87 |
} |
| 88 |
} catch (JsonException $e) { |
| 89 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 90 |
} catch (RequestException $e) { |
| 91 |
$response = $e->getResponse(); |
| 92 |
if ($response === null) { |
| 93 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 94 |
} |
| 95 |
throw new SquareApiException( |
| 96 |
message: "API request failed", |
| 97 |
statusCode: $response->getStatusCode(), |
| 98 |
body: $response->getBody()->getContents(), |
| 99 |
); |
| 100 |
} catch (ClientExceptionInterface $e) { |
| 101 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 102 |
} |
| 103 |
throw new SquareApiException( |
| 104 |
message: 'API request failed', |
| 105 |
statusCode: $statusCode, |
| 106 |
body: $response->getBody()->getContents(), |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|