maxbeckers /
amazon-alexa-php
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace MaxBeckers\AmazonAlexa\Helper; |
||||
| 6 | |||||
| 7 | use GuzzleHttp\Client; |
||||
| 8 | use MaxBeckers\AmazonAlexa\Exception\DeviceApiCallException; |
||||
| 9 | use MaxBeckers\AmazonAlexa\Exception\MissingRequestDataException; |
||||
| 10 | use MaxBeckers\AmazonAlexa\Request\Device\DeviceAddressInformation; |
||||
| 11 | use MaxBeckers\AmazonAlexa\Request\Request; |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * This helper class can call the amazon api to get address information. |
||||
| 15 | * For more details @see https=>//developer.amazon.com/de/docs/custom-skills/device-address-api.html. |
||||
| 16 | */ |
||||
| 17 | class DeviceAddressInformationHelper |
||||
| 18 | { |
||||
| 19 | 7 | public function __construct( |
|||
| 20 | private readonly Client $client = new Client() |
||||
| 21 | ) { |
||||
| 22 | 7 | } |
|||
| 23 | |||||
| 24 | /** |
||||
| 25 | * @throws MissingRequestDataException |
||||
| 26 | */ |
||||
| 27 | 4 | public function getCountryAndPostalCode(Request $request): DeviceAddressInformation |
|||
| 28 | { |
||||
| 29 | 4 | if (!isset($request->context->system->device->deviceId, $request->context->system->apiAccessToken, $request->context->system->apiEndpoint)) { |
|||
| 30 | 2 | throw new MissingRequestDataException(); |
|||
| 31 | } |
||||
| 32 | |||||
| 33 | 2 | $deviceId = $request->context->system->device->deviceId; |
|||
| 34 | 2 | $token = $request->context->system->apiAccessToken; |
|||
| 35 | 2 | $endpoint = $request->context->system->apiEndpoint; |
|||
| 36 | |||||
| 37 | 2 | $url = sprintf('%s/v1/devices/%s/settings/address/countryAndPostalCode', $endpoint, $deviceId); |
|||
| 38 | |||||
| 39 | 2 | return $this->apiCall($url, $token); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * @throws MissingRequestDataException |
||||
| 44 | */ |
||||
| 45 | 3 | public function getAddress(Request $request): DeviceAddressInformation |
|||
| 46 | { |
||||
| 47 | 3 | if (!isset($request->context->system->device->deviceId, $request->context->system->apiAccessToken, $request->context->system->apiEndpoint)) { |
|||
| 48 | 1 | throw new MissingRequestDataException(); |
|||
| 49 | } |
||||
| 50 | |||||
| 51 | 2 | $deviceId = $request->context->system->device->deviceId; |
|||
| 52 | 2 | $token = $request->context->system->apiAccessToken; |
|||
| 53 | 2 | $endpoint = $request->context->system->apiEndpoint; |
|||
| 54 | |||||
| 55 | 2 | $url = sprintf('%s/v1/devices/%s/settings/address', $endpoint, $deviceId); |
|||
| 56 | |||||
| 57 | 2 | return $this->apiCall($url, $token); |
|||
|
0 ignored issues
–
show
It seems like
$token can also be of type null; however, parameter $token of MaxBeckers\AmazonAlexa\H...mationHelper::apiCall() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * @throws DeviceApiCallException |
||||
| 62 | */ |
||||
| 63 | 4 | private function apiCall(string $url, string $token): DeviceAddressInformation |
|||
| 64 | { |
||||
| 65 | 4 | $response = $this->client->request('GET', $url, [ |
|||
| 66 | 4 | 'headers' => [ |
|||
| 67 | 4 | 'Authorization' => 'Bearer ' . $token, |
|||
| 68 | 4 | 'Accept' => 'application/json', |
|||
| 69 | 4 | ], |
|||
| 70 | 4 | ]); |
|||
| 71 | |||||
| 72 | /* |
||||
| 73 | * Api Call response codes: |
||||
| 74 | * 200 OK Successfully got the address associated with this deviceId. |
||||
| 75 | * 204 No Content The query did not return any results. |
||||
| 76 | * 403 Forbidden The authentication token is invalid or doesn’t have access to the resource. |
||||
| 77 | * 405 Method Not Allowed The method is not supported. |
||||
| 78 | * 429 Too Many Requests The skill has been throttled due to an excessive number of requests. |
||||
| 79 | * 500 Internal Error An unexpected error occurred. |
||||
| 80 | */ |
||||
| 81 | 4 | if (200 !== $response->getStatusCode()) { |
|||
| 82 | 2 | throw new DeviceApiCallException(sprintf('Error in api call (status code:"%s")', $response->getStatusCode())); |
|||
| 83 | } |
||||
| 84 | |||||
| 85 | 2 | return DeviceAddressInformation::fromApiResponse(json_decode($response->getBody()->getContents(), true)); |
|||
| 86 | } |
||||
| 87 | } |
||||
| 88 |