Passed
Push — master ( c81968...b18996 )
by Maximilian
01:27 queued 12s
created

DeviceAddressInformationHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
eloc 24
c 1
b 1
f 0
dl 0
loc 90
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountryAndPostalCode() 0 13 2
A getAddress() 0 13 2
A __construct() 0 3 2
A apiCall() 0 23 2
1
<?php
2
3
namespace MaxBeckers\AmazonAlexa\Helper;
4
5
use GuzzleHttp\Client;
6
use MaxBeckers\AmazonAlexa\Exception\DeviceApiCallException;
7
use MaxBeckers\AmazonAlexa\Exception\MissingRequestDataException;
8
use MaxBeckers\AmazonAlexa\Request\Device\DeviceAddressInformation;
9
use MaxBeckers\AmazonAlexa\Request\Request;
10
11
/**
12
 * This helper class can call the amazon api to get address information.
13
 * For more details @see https=>//developer.amazon.com/de/docs/custom-skills/device-address-api.html.
14
 *
15
 * @author Maximilian Beckers <[email protected]>
16
 */
17
class DeviceAddressInformationHelper
18
{
19
    /**
20
     * @var Client
21
     */
22
    private $client;
23
24
    /**
25
     * @param Client|null $client
26
     */
27 7
    public function __construct(Client $client = null)
28
    {
29 7
        $this->client = $client ?: new Client();
30
    }
31
32
    /**
33
     * @param Request $request
34
     *
35
     * @throws MissingRequestDataException
36
     *
37
     * @return DeviceAddressInformation
38
     */
39 4
    public function getCountryAndPostalCode(Request $request): DeviceAddressInformation
40
    {
41 4
        if (!isset($request->context->system->device->deviceId, $request->context->system->apiAccessToken, $request->context->system->apiEndpoint)) {
42 2
            throw new MissingRequestDataException();
43
        }
44
45 2
        $deviceId = $request->context->system->device->deviceId;
46 2
        $token    = $request->context->system->apiAccessToken;
47 2
        $endpoint = $request->context->system->apiEndpoint;
48
49 2
        $url = sprintf('%s/v1/devices/%s/settings/address/countryAndPostalCode', $endpoint, $deviceId);
50
51 2
        return $this->apiCall($url, $token);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

51
        return $this->apiCall($url, /** @scrutinizer ignore-type */ $token);
Loading history...
52
    }
53
54
    /**
55
     * @param Request $request
56
     *
57
     * @throws MissingRequestDataException
58
     *
59
     * @return DeviceAddressInformation
60
     */
61 3
    public function getAddress(Request $request): DeviceAddressInformation
62
    {
63 3
        if (!isset($request->context->system->device->deviceId, $request->context->system->apiAccessToken, $request->context->system->apiEndpoint)) {
64 1
            throw new MissingRequestDataException();
65
        }
66
67 2
        $deviceId = $request->context->system->device->deviceId;
68 2
        $token    = $request->context->system->apiAccessToken;
69 2
        $endpoint = $request->context->system->apiEndpoint;
70
71 2
        $url = sprintf('%s/v1/devices/%s/settings/address', $endpoint, $deviceId);
72
73 2
        return $this->apiCall($url, $token);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

73
        return $this->apiCall($url, /** @scrutinizer ignore-type */ $token);
Loading history...
74
    }
75
76
    /**
77
     * @param string $url
78
     * @param string $token
79
     *
80
     * @throws DeviceApiCallException
81
     *
82
     * @return DeviceAddressInformation
83
     */
84 4
    private function apiCall(string $url, string $token): DeviceAddressInformation
85
    {
86 4
        $response = $this->client->request('GET', $url, [
87
            'headers' => [
88 4
                'Authorization' => 'Bearer '.$token,
89
                'Accept'        => 'application/json',
90
            ],
91
        ]);
92
93
        /*
94
         * Api Call response codes:
95
         * 200 OK                   Successfully got the address associated with this deviceId.
96
         * 204 No Content           The query did not return any results.
97
         * 403 Forbidden            The authentication token is invalid or doesn’t have access to the resource.
98
         * 405 Method Not Allowed   The method is not supported.
99
         * 429 Too Many Requests    The skill has been throttled due to an excessive number of requests.
100
         * 500 Internal Error       An unexpected error occurred.
101
         */
102 4
        if (200 !== $response->getStatusCode()) {
103 2
            throw new DeviceApiCallException(sprintf('Error in api call (status code:"%s")', $response->getStatusCode()));
104
        }
105
106 2
        return DeviceAddressInformation::fromApiResponse(json_decode($response->getBody()->getContents(), true));
107
    }
108
}
109