Amazon   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 10
eloc 28
dl 0
loc 88
rs 10
c 0
b 0
f 0
ccs 27
cts 31
cp 0.871

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getAuthorizeUri() 0 3 1
A getRequestTokenUri() 0 3 1
A parseToken() 0 12 3
A getBaseUri() 0 3 1
A getIdentity() 0 32 3
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Amazon extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    const NAME = 'amazon';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 3
    public function getBaseUri()
24
    {
25 3
        return 'https://api.amazon.com/';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function getAuthorizeUri()
32
    {
33 2
        return 'https://www.amazon.com/ap/oa';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function getRequestTokenUri()
40
    {
41 2
        return 'https://api.amazon.com/auth/o2/token';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3
    public function getName()
48
    {
49 3
        return self::NAME;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    public function parseToken($body)
56
    {
57 3
        if (empty($body)) {
58
            throw new InvalidAccessToken('Provider response with empty body');
59
        }
60
61 3
        $result = json_decode($body, true);
62 3
        if ($result) {
63 1
            return new AccessToken($result);
64
        }
65
66 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 2
    public function getIdentity(AccessTokenInterface $accessToken)
73
    {
74 2
        $response = $this->httpClient->request(
75 2
            $this->getBaseUri() . 'user/profile',
76
            [
77 2
                'access_token' => $accessToken->getToken()
78
            ]
79
        );
80
81 2
        if (!$response->isSuccess()) {
82 1
            throw new InvalidResponse(
83 1
                'API response with error code',
84 1
                $response
85
            );
86
        }
87
88 1
        $result = $response->json();
89 1
        if (!$result) {
90 1
            throw new InvalidResponse(
91 1
                'API response is not a valid JSON object',
92 1
                $response
93
            );
94
        }
95
96
        $hydrator = new ObjectMap(
97
            [
98
                'user_id' => 'id',
99
                'name' => 'firstname',
100
            ]
101
        );
102
103
        return $hydrator->hydrate(new User(), $result);
104
    }
105
}
106