Completed
Push — master ( bda8d5...d74c6d )
by Дмитрий
03:32 queued 01:52
created

Instagram::getBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
use SocialConnect\OAuth2\AccessToken;
15
16
class Instagram extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    const NAME = 'instagram';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 3
    public function getBaseUri()
24
    {
25 3
        return 'https://api.instagram.com/v1/';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function getAuthorizeUri()
32
    {
33 2
        return 'https://api.instagram.com/oauth/authorize';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function getRequestTokenUri()
40
    {
41 2
        return 'https://api.instagram.com/oauth/access_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
        $result = json_decode($body, true);
58 3
        if ($result) {
59 1
            return new AccessToken($result);
60
        }
61
62 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function getIdentity(AccessTokenInterface $accessToken)
69
    {
70 2
        $response = $this->httpClient->request(
71 2
            $this->getBaseUri() . 'users/self',
72
            [
73 2
                'access_token' => $accessToken->getToken()
74
            ]
75
        );
76
77 2
        if (!$response->isSuccess()) {
78 1
            throw new InvalidResponse(
79 1
                'API response with error code',
80 1
                $response
81
            );
82
        }
83
84 1
        $result = $response->json();
85 1
        if (!$result) {
86 1
            throw new InvalidResponse(
87 1
                'API response is not a valid JSON object',
88 1
                $response
89
            );
90
        }
91
92
        $hydrator = new ObjectMap(
93
            [
94
                'id' => 'id',
95
                'username' => 'username',
96
                'bio' => 'bio',
97
                'website' => 'website',
98
                'profile_picture' => 'pictureURL',
99
                'full_name' => 'fullname'
100
            ]
101
        );
102
103
        return $hydrator->hydrate(new User(), $result->data);
104
    }
105
}
106