Completed
Push — master ( f5575a...f97304 )
by Steven
02:29
created

Instagram::checkResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
namespace League\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\Exception\InstagramIdentityProviderException;
6
use League\OAuth2\Client\Token\AccessToken;
7
use Psr\Http\Message\ResponseInterface;
8
9
class Instagram extends AbstractProvider
10
{
11
    /**
12
     * @var string Key used in a token response to identify the resource owner.
13
     */
14
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'user.id';
15
16
    /**
17
     * Default scopes
18
     *
19
     * @var array
20
     */
21
    public $defaultScopes = ['basic'];
22
23
    /**
24
     * Get the string used to separate scopes.
25
     *
26
     * @return string
27
     */
28 6
    protected function getScopeSeparator()
29
    {
30 6
        return ' ';
31
    }
32
33
    /**
34
     * Get authorization url to begin OAuth flow
35
     *
36
     * @return string
37
     */
38 6
    public function getBaseAuthorizationUrl()
39
    {
40 6
        return 'https://api.instagram.com/oauth/authorize';
41
    }
42
43
    /**
44
     * Get access token url to retrieve token
45
     *
46
     * @param  array $params
47
     *
48
     * @return string
49
     */
50 12
    public function getBaseAccessTokenUrl(array $params)
51
    {
52 12
        return 'https://api.instagram.com/oauth/access_token';
53
    }
54
55
    /**
56
     * Get provider url to fetch user details
57
     *
58
     * @param  AccessToken $token
59
     *
60
     * @return string
61
     */
62 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
63
    {
64 2
        return 'https://api.instagram.com/v1/users/self?access_token='.$token;
65
    }
66
67
    /**
68
     * Returns an authenticated PSR-7 request instance.
69
     *
70
     * @param  string $method
71
     * @param  string $url
72
     * @param  AccessToken|string $token
73
     * @param  array $options Any of "headers", "body", and "protocolVersion".
74
     *
75
     * @return \Psr\Http\Message\RequestInterface
76
     */
77 4
    public function getAuthenticatedRequest($method, $url, $token, array $options = [])
78
    {
79 4
        $parsedUrl = parse_url($url);
80 4
        $queryString = array();
81
82 4
        if (isset($parsedUrl['query'])) {
83 2
            parse_str($parsedUrl['query'], $queryString);
84 2
        }
85
86 4
        if (!isset($queryString['access_token'])) {
87 2
            $queryString['access_token'] = (string) $token;
88 2
        }
89
90 4
        $url = http_build_url($url, [
91 4
            'query' => http_build_query($queryString),
92 4
        ]);
93
94 4
        return $this->createRequest($method, $url, null, $options);
95
    }
96
97
    /**
98
     * Get the default scopes used by this provider.
99
     *
100
     * This should not be a complete list of all scopes, but the minimum
101
     * required for the provider user interface!
102
     *
103
     * @return array
104
     */
105 4
    protected function getDefaultScopes()
106
    {
107 4
        return $this->defaultScopes;
108
    }
109
110
    /**
111
     * Check a provider response for errors.
112
     *
113
     * @link   https://instagram.com/developer/endpoints/
114
     * @throws IdentityProviderException
115
     * @param  ResponseInterface $response
116
     * @param  string $data Parsed response data
117
     * @return void
118
     */
119 10
    protected function checkResponse(ResponseInterface $response, $data)
120
    {
121
        // Standard error response format
122 10
        if (!empty($data['meta']['error_type'])) {
123 2
            throw InstagramIdentityProviderException::clientException($response, $data);
124
        }
125
126
        // OAuthException error response format
127 8
        if (!empty($data['error_type'])) {
128 2
            throw InstagramIdentityProviderException::oauthException($response, $data);
129
        }
130 6
    }
131
132
    /**
133
     * Generate a user object from a successful user details request.
134
     *
135
     * @param array $response
136
     * @param AccessToken $token
137
     * @return ResourceOwnerInterface
138
     */
139 2
    protected function createResourceOwner(array $response, AccessToken $token)
140
    {
141 2
        return new InstagramResourceOwner($response);
142
    }
143
}
144