Completed
Push — master ( ad69a5...d98011 )
by Дмитрий
03:25
created

Provider::getIdentity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Amazon;
8
9
use SocialConnect\Auth\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Exception\InvalidResponse;
11
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
15
class Provider extends \SocialConnect\Auth\Provider\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
        $result = $response->json();
83
        if (!$result) {
84
            throw new InvalidResponse(
85
                'API response is not a valid JSON object',
86
                $response->getBody()
87
            );
88
        }
89
90
        $hydrator = new ObjectMap(array(
91
            'user_id' => 'id',
92
            'name' => 'firstname',
93
        ));
94
95
        return $hydrator->hydrate(new User(), $result);
96
    }
97
}
98