Completed
Push — master ( 483eda...cb59ac )
by Дмитрий
03:04
created

Amazon   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 90
rs 10

6 Methods

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