Completed
Pull Request — master (#572)
by Frédéric
01:32
created

Vkontakte::requestAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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