Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Amazon::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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