Completed
Push — master ( a178a2...762452 )
by Дмитрий
03:58
created

Instagram   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

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 9 2
B getIdentity() 0 32 2
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
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getBaseUri()
22
    {
23
        return 'https://api.instagram.com/v1/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getAuthorizeUri()
30
    {
31
        return 'https://api.instagram.com/oauth/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getRequestTokenUri()
38
    {
39
        return 'https://api.instagram.com/oauth/access_token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getName()
46
    {
47
        return 'instagram';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function parseToken($body)
54
    {
55
        $result = json_decode($body, true);
56
        if ($result) {
57
            return new AccessToken($result);
58
        }
59
60
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getIdentity(AccessTokenInterface $accessToken)
67
    {
68
        $response = $this->httpClient->request(
69
            $this->getBaseUri() . 'users/self',
70
            [
71
                'access_token' => $accessToken->getToken()
72
            ]
73
        );
74
75
        if (!$response->isSuccess()) {
76
            throw new InvalidResponse(
77
                'API response with error code',
78
                $response
79
            );
80
        }
81
82
        $body = $response->getBody();
83
        $result = json_decode($body);
84
85
        $hydrator = new ObjectMap(
86
            [
87
                'id' => 'id',
88
                'username' => 'username',
89
                'bio' => 'bio',
90
                'website' => 'website',
91
                'profile_picture' => 'profile_picture',
92
                'full_name' => 'fullname'
93
            ]
94
        );
95
96
        return $hydrator->hydrate(new User(), $result->data);
97
    }
98
}
99