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

Reddit::getAuthorizationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 Reddit extends AbstractService
10
{
11
    /**
12
     * Defined scopes
13
     *
14
     * @link http://www.reddit.com/dev/api/oauth
15
     */
16
    // User scopes
17
    const SCOPE_EDIT                         = 'edit';
18
    const SCOPE_HISTORY                      = 'history';
19
    const SCOPE_IDENTITY                     = 'identity';
20
    const SCOPE_MYSUBREDDITS                 = 'mysubreddits';
21
    const SCOPE_PRIVATEMESSAGES              = 'privatemessages';
22
    const SCOPE_READ                         = 'read';
23
    const SCOPE_SAVE                         = 'save';
24
    const SCOPE_SUBMIT                       = 'submit';
25
    const SCOPE_SUBSCRIBE                    = 'subscribe';
26
    const SCOPE_VOTE                         = 'vote';
27
    // Mod Scopes
28
    const SCOPE_MODCONFIG                    = 'modconfig';
29
    const SCOPE_MODFLAIR                     = 'modflair';
30
    const SCOPE_MODLOG                       = 'modlog';
31
    const SCOPE_MODPOST                      = 'modpost';
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function init()
37
    {
38
        $this->stateParameterInAuthUrl = true;
39
40
        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...
41
            $this->baseApiUri = new Uri('https://oauth.reddit.com');
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getAuthorizationEndpoint()
49
    {
50
        return new Uri('https://ssl.reddit.com/api/v1/authorize');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getAccessTokenEndpoint()
57
    {
58
        return new Uri('https://ssl.reddit.com/api/v1/access_token');
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function getAuthorizationMethod()
65
    {
66
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function parseAccessTokenResponse($responseBody)
73
    {
74
        $data = json_decode($responseBody, true);
75
76
        if (null === $data || !is_array($data)) {
77
            throw new TokenResponseException('Unable to parse response.');
78
        } elseif (isset($data['error'])) {
79
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
80
        }
81
82
        $token = new StdOAuth2Token();
83
        $token->setAccessToken($data['access_token']);
84
        $token->setLifeTime($data['expires_in']);
85
86
        if (isset($data['refresh_token'])) {
87
            $token->setRefreshToken($data['refresh_token']);
88
            unset($data['refresh_token']);
89
        }
90
91
        unset($data['access_token']);
92
        unset($data['expires_in']);
93
94
        $token->setExtraParams($data);
95
96
        return $token;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function getExtraOAuthHeaders()
103
    {
104
        // Reddit uses a Basic OAuth header
105
        return array('Authorization' => 'Basic ' .
106
            base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()));
107
    }
108
}
109