TextControl /
textcontrol-reportingcloud
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | /** |
||
| 5 | * ReportingCloud PHP SDK |
||
| 6 | * |
||
| 7 | * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
||
| 8 | * |
||
| 9 | * @link https://www.reporting.cloud to learn more about ReportingCloud |
||
| 10 | * @link https://tinyurl.com/vmbbh6kd for the canonical source repository |
||
| 11 | * @license https://tinyurl.com/3pc9am89 |
||
| 12 | * @copyright © 2023 Text Control GmbH |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace TextControl\ReportingCloud; |
||
| 16 | |||
| 17 | use Ctw\Http\HttpMethod; |
||
| 18 | use Ctw\Http\HttpStatus; |
||
| 19 | use GuzzleHttp\RequestOptions; |
||
| 20 | use JsonException; |
||
| 21 | use Psr\Http\Message\ResponseInterface; |
||
| 22 | use TextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Trait PutTrait |
||
| 26 | */ |
||
| 27 | trait PutTrait |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Create an API key |
||
| 31 | */ |
||
| 32 | 10 | public function createApiKey(): string |
|
| 33 | { |
||
| 34 | 10 | return $this->put('/account/apikey', [], '', HttpStatus::STATUS_CREATED); |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Construct URI with version number |
||
| 39 | * |
||
| 40 | * @param string $uri URI |
||
| 41 | */ |
||
| 42 | abstract protected function uri(string $uri): string; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Request the URI with options |
||
| 46 | * |
||
| 47 | * @param string $method HTTP method |
||
| 48 | * @param string $uri URI |
||
| 49 | * @param array $options Options |
||
| 50 | */ |
||
| 51 | abstract protected function request(string $method, string $uri, array $options): ResponseInterface; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Using the passed propertyMap, recursively build array |
||
| 55 | * |
||
| 56 | * @param array $array Array |
||
| 57 | * @param PropertyMap $propertyMap PropertyMap |
||
| 58 | */ |
||
| 59 | abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Execute a PUT request via REST client |
||
| 63 | * |
||
| 64 | * @param string $uri URI |
||
| 65 | * @param array[] $query Query |
||
| 66 | * @param mixed $json JSON |
||
| 67 | * @param int $statusCode Required HTTP status code for response |
||
| 68 | */ |
||
| 69 | 10 | private function put(string $uri, array $query = [], mixed $json = '', int $statusCode = 0): string |
|
| 70 | { |
||
| 71 | 10 | $ret = ''; |
|
| 72 | |||
| 73 | 10 | $response = $this->request(HttpMethod::METHOD_PUT, $this->uri($uri), [ |
|
| 74 | 10 | RequestOptions::QUERY => $query, |
|
| 75 | 10 | RequestOptions::JSON => $json, |
|
| 76 | 10 | ]); |
|
| 77 | |||
| 78 | 10 | if ($statusCode === $response->getStatusCode()) { |
|
| 79 | try { |
||
| 80 | 10 | $body = $response->getBody(); |
|
| 81 | 10 | $content = $body->getContents(); |
|
| 82 | 10 | $ret = json_decode($content, true, 512, JSON_THROW_ON_ERROR); |
|
| 83 | } catch (JsonException) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | 10 | assert(is_string($ret)); |
|
| 88 | |||
| 89 | 10 | return $ret; |
|
| 90 | } |
||
| 91 | } |
||
| 92 |