Completed
Push — master ( f97304...6b4a86 )
by Steven
02:43
created

Instagram::setHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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
     * Default host
25
     *
26
     * @var string
27
     */
28
    protected $host = 'https://api.instagram.com';
29
30
    /**
31
     * Gets host.
32
     *
33
     * @return string
34
     */
35 4
    public function getHost()
36
    {
37 4
        return $this->host;
38
    }
39
40
    /**
41
     * Get the string used to separate scopes.
42
     *
43
     * @return string
44
     */
45 6
    protected function getScopeSeparator()
46
    {
47 6
        return ' ';
48
    }
49
50
    /**
51
     * Get authorization url to begin OAuth flow
52
     *
53
     * @return string
54
     */
55 6
    public function getBaseAuthorizationUrl()
56
    {
57 6
        return $this->host.'/oauth/authorize';
58
    }
59
60
    /**
61
     * Get access token url to retrieve token
62
     *
63
     * @param  array $params
64
     *
65
     * @return string
66
     */
67 12
    public function getBaseAccessTokenUrl(array $params)
68
    {
69 12
        return $this->host.'/oauth/access_token';
70
    }
71
72
    /**
73
     * Get provider url to fetch user details
74
     *
75
     * @param  AccessToken $token
76
     *
77
     * @return string
78
     */
79 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
80
    {
81 2
        return $this->host.'/v1/users/self?access_token='.$token;
82
    }
83
84
    /**
85
     * Returns an authenticated PSR-7 request instance.
86
     *
87
     * @param  string $method
88
     * @param  string $url
89
     * @param  AccessToken|string $token
90
     * @param  array $options Any of "headers", "body", and "protocolVersion".
91
     *
92
     * @return \Psr\Http\Message\RequestInterface
93
     */
94 4
    public function getAuthenticatedRequest($method, $url, $token, array $options = [])
95
    {
96 4
        $parsedUrl = parse_url($url);
97 4
        $queryString = array();
98
99 4
        if (isset($parsedUrl['query'])) {
100 2
            parse_str($parsedUrl['query'], $queryString);
101 2
        }
102
103 4
        if (!isset($queryString['access_token'])) {
104 2
            $queryString['access_token'] = (string) $token;
105 2
        }
106
107 4
        $url = http_build_url($url, [
108 4
            'query' => http_build_query($queryString),
109 4
        ]);
110
111 4
        return $this->createRequest($method, $url, null, $options);
112
    }
113
114
    /**
115
     * Get the default scopes used by this provider.
116
     *
117
     * This should not be a complete list of all scopes, but the minimum
118
     * required for the provider user interface!
119
     *
120
     * @return array
121
     */
122 4
    protected function getDefaultScopes()
123
    {
124 4
        return $this->defaultScopes;
125
    }
126
127
    /**
128
     * Check a provider response for errors.
129
     *
130
     * @link   https://instagram.com/developer/endpoints/
131
     * @throws IdentityProviderException
132
     * @param  ResponseInterface $response
133
     * @param  string $data Parsed response data
134
     * @return void
135
     */
136 10
    protected function checkResponse(ResponseInterface $response, $data)
137
    {
138
        // Standard error response format
139 10
        if (!empty($data['meta']['error_type'])) {
140 2
            throw InstagramIdentityProviderException::clientException($response, $data);
141
        }
142
143
        // OAuthException error response format
144 8
        if (!empty($data['error_type'])) {
145 2
            throw InstagramIdentityProviderException::oauthException($response, $data);
146
        }
147 6
    }
148
149
    /**
150
     * Generate a user object from a successful user details request.
151
     *
152
     * @param array $response
153
     * @param AccessToken $token
154
     * @return ResourceOwnerInterface
155
     */
156 2
    protected function createResourceOwner(array $response, AccessToken $token)
157
    {
158 2
        return new InstagramResourceOwner($response);
159
    }
160
161
    /**
162
     * Sets host.
163
     *
164
     * @param string $host
165
     *
166
     * @return string
167
     */
168 2
    public function setHost($host)
169
    {
170 2
        $this->host = $host;
171
172 2
        return $this;
173
    }
174
}
175