Completed
Push — master ( 34d916...cca307 )
by Дмитрий
03:10
created

Provider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 10
c 5
b 0
f 0
lcom 1
cbo 9
dl 0
loc 85
ccs 0
cts 53
cp 0
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 13 4
B getIdentity() 0 30 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Instagram;
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\Http\Client\Client;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Provider extends \SocialConnect\Auth\Provider\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);
56
        if ($result) {
57
            if (isset($result->access_token) && $result->access_token) {
58
                return new AccessToken($result->access_token);
59
            }
60
61
            throw new InvalidAccessToken('Instagram response with not valid token');
62
        }
63
64
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getIdentity(AccessToken $accessToken)
71
    {
72
        $response = $this->service->getHttpClient()->request(
73
            $this->getBaseUri() . 'users/self',
74
            [
75
                'access_token' => $accessToken->getToken()
76
            ]
77
        );
78
79
        if (!$response->isSuccess()) {
80
            throw new InvalidResponse(
81
                'API response with error code',
82
                $response
83
            );
84
        }
85
86
        $body = $response->getBody();
87
        $result = json_decode($body);
88
89
        $hydrator = new ObjectMap(array(
90
            'id' => 'id',
91
            'username' => 'username',
92
            'bio' => 'bio',
93
            'website' => 'website',
94
            'profile_picture' => 'profile_picture',
95
            'full_name' => 'fullname'
96
        ));
97
98
        return $hydrator->hydrate(new User(), $result->data);
99
    }
100
}
101