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

Vkontakte::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A Vkontakte::getAuthorizationEndpoint() 0 4 1
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 Vkontakte extends AbstractService
10
{
11
    /**
12
     * Defined scopes
13
     *
14
     * @link http://vk.com/dev/permissions
15
     */
16
    const SCOPE_EMAIL         = 'email';
17
    const SCOPE_NOTIFY        = 'notify';
18
    const SCOPE_FRIENDS       = 'friends';
19
    const SCOPE_PHOTOS        = 'photos';
20
    const SCOPE_AUDIO         = 'audio';
21
    const SCOPE_VIDEO         = 'video';
22
    const SCOPE_DOCS          = 'docs';
23
    const SCOPE_NOTES         = 'notes';
24
    const SCOPE_PAGES         = 'pages';
25
    const SCOPE_APP_LINK      = '';
26
    const SCOPE_STATUS        = 'status';
27
    const SCOPE_OFFERS        = 'offers';
28
    const SCOPE_QUESTIONS     = 'questions';
29
    const SCOPE_WALL          = 'wall';
30
    const SCOPE_GROUPS        = 'groups';
31
    const SCOPE_MESSAGES      = 'messages';
32
    const SCOPE_NOTIFICATIONS = 'notifications';
33
    const SCOPE_STATS         = 'stats';
34
    const SCOPE_ADS           = 'ads';
35
    const SCOPE_OFFLINE       = 'offline';
36
    const SCOPE_NOHTTPS       = 'nohttps';
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function init()
42
    {
43
        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...
44
            $this->baseApiUri = new Uri('https://api.vk.com/method/');
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getAuthorizationEndpoint()
52
    {
53
        return new Uri('https://oauth.vk.com/authorize');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getAccessTokenEndpoint()
60
    {
61
        return new Uri('https://oauth.vk.com/access_token');
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function parseAccessTokenResponse($responseBody)
68
    {
69
        $data = json_decode($responseBody, true);
70
71
        if (null === $data || !is_array($data)) {
72
            throw new TokenResponseException('Unable to parse response.');
73
        } elseif (isset($data['error'])) {
74
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
75
        }
76
77
        $token = new StdOAuth2Token();
78
        $token->setAccessToken($data['access_token']);
79
        $token->setLifeTime($data['expires_in']);
80
81
        if (isset($data['refresh_token'])) {
82
            $token->setRefreshToken($data['refresh_token']);
83
            unset($data['refresh_token']);
84
        }
85
86
        unset($data['access_token']);
87
        unset($data['expires_in']);
88
89
        $token->setExtraParams($data);
90
91
        return $token;
92
    }
93
    
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function getAuthorizationMethod()
98
    {
99
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
100
    }
101
}
102