Amazon   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 77
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseAccessTokenResponse() 0 26 6
A getAuthorizationMethod() 0 3 1
A getAccessTokenEndpoint() 0 3 1
A getAuthorizationEndpoint() 0 3 1
A __construct() 0 11 2
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
/**
14
 * Amazon service.
15
 *
16
 * @author Flávio Heleno <[email protected]>
17
 *
18
 * @see https://images-na.ssl-images-amazon.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
19
 */
20
class Amazon extends AbstractService
21
{
22
    /**
23
     * Defined scopes.
24
     *
25
     * @see https://images-na.ssl-images-amazon.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
26
     */
27
    const SCOPE_PROFILE = 'profile';
28
    const SCOPE_POSTAL_CODE = 'postal_code';
29
30
    public function __construct(
31
        CredentialsInterface $credentials,
32
        ClientInterface $httpClient,
33
        TokenStorageInterface $storage,
34
        $scopes = [],
35
        ?UriInterface $baseApiUri = null
36
    ) {
37
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
38
39
        if (null === $baseApiUri) {
40
            $this->baseApiUri = new Uri('https://api.amazon.com/');
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getAuthorizationEndpoint()
48
    {
49
        return new Uri('https://www.amazon.com/ap/oa');
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAccessTokenEndpoint()
56
    {
57
        return new Uri('https://www.amazon.com/ap/oatoken');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function getAuthorizationMethod()
64
    {
65
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function parseAccessTokenResponse($responseBody)
72
    {
73
        $data = json_decode($responseBody, true);
74
75
        if (null === $data || !is_array($data)) {
76
            throw new TokenResponseException('Unable to parse response.');
77
        } elseif (isset($data['error_description'])) {
78
            throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
79
        } elseif (isset($data['error'])) {
80
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
81
        }
82
83
        $token = new StdOAuth2Token();
84
        $token->setAccessToken($data['access_token']);
85
        $token->setLifeTime($data['expires_in']);
86
87
        if (isset($data['refresh_token'])) {
88
            $token->setRefreshToken($data['refresh_token']);
89
            unset($data['refresh_token']);
90
        }
91
92
        unset($data['access_token'], $data['expires_in']);
93
94
        $token->setExtraParams($data);
95
96
        return $token;
97
    }
98
}
99