Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Instagram::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class Instagram extends AbstractService
10
{
11
    /**
12
     * Defined scopes
13
     * @link http://instagram.com/developer/authentication/#scope
14
     */
15
    const SCOPE_BASIC         = 'basic';
16
    const SCOPE_PUBLIC_CONTENT = 'public_content';
17
    const SCOPE_COMMENTS      = 'comments';
18
    const SCOPE_RELATIONSHIPS = 'relationships';
19
    const SCOPE_LIKES         = 'likes';
20
    const SCOPE_FOLLOWER_LIST = 'follower_list';
21
    
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function init()
26
    {
27
        $this->stateParameterInAuthUrl = true;
28
29
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
30
            $this->baseApiUri = new Uri('https://api.instagram.com/v1/');
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getAuthorizationEndpoint()
38
    {
39
        return new Uri('https://api.instagram.com/oauth/authorize/');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAccessTokenEndpoint()
46
    {
47
        return new Uri('https://api.instagram.com/oauth/access_token');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function getAuthorizationMethod()
54
    {
55
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function parseAccessTokenResponse($responseBody)
62
    {
63
        $data = json_decode($responseBody, true);
64
65
        if (null === $data || !is_array($data)) {
66
            throw new TokenResponseException('Unable to parse response.');
67
        } elseif (isset($data['error'])) {
68
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
69
        }
70
71
        $token = new StdOAuth2Token();
72
        $token->setAccessToken($data['access_token']);
73
        // Instagram tokens evidently never expire...
74
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
75
        unset($data['access_token']);
76
77
        $token->setExtraParams($data);
78
79
        return $token;
80
    }
81
}
82