Reddit   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 37
dl 0
loc 99
c 1
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessTokenEndpoint() 0 3 1
A getAuthorizationMethod() 0 3 1
A __construct() 0 11 2
A parseAccessTokenResponse() 0 24 5
A getExtraOAuthHeaders() 0 5 1
A getAuthorizationEndpoint() 0 3 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
class Reddit extends AbstractService
14
{
15
    /**
16
     * Defined scopes.
17
     *
18
     * @see http://www.reddit.com/dev/api/oauth
19
     */
20
    // User scopes
21
    const SCOPE_EDIT = 'edit';
22
    const SCOPE_HISTORY = 'history';
23
    const SCOPE_IDENTITY = 'identity';
24
    const SCOPE_MYSUBREDDITS = 'mysubreddits';
25
    const SCOPE_PRIVATEMESSAGES = 'privatemessages';
26
    const SCOPE_READ = 'read';
27
    const SCOPE_SAVE = 'save';
28
    const SCOPE_SUBMIT = 'submit';
29
    const SCOPE_SUBSCRIBE = 'subscribe';
30
    const SCOPE_VOTE = 'vote';
31
    // Mod Scopes
32
    const SCOPE_MODCONFIG = 'modconfig';
33
    const SCOPE_MODFLAIR = 'modflair';
34
    const SCOPE_MODLOG = 'modlog';
35
    const SCOPE_MODPOST = 'modpost';
36
37
    public function __construct(
38
        CredentialsInterface $credentials,
39
        ClientInterface $httpClient,
40
        TokenStorageInterface $storage,
41
        $scopes = [],
42
        ?UriInterface $baseApiUri = null
43
    ) {
44
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
45
46
        if (null === $baseApiUri) {
47
            $this->baseApiUri = new Uri('https://oauth.reddit.com');
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getAuthorizationEndpoint()
55
    {
56
        return new Uri('https://ssl.reddit.com/api/v1/authorize');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getAccessTokenEndpoint()
63
    {
64
        return new Uri('https://ssl.reddit.com/api/v1/access_token');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function getAuthorizationMethod()
71
    {
72
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function parseAccessTokenResponse($responseBody)
79
    {
80
        $data = json_decode($responseBody, true);
81
82
        if (null === $data || !is_array($data)) {
83
            throw new TokenResponseException('Unable to parse response.');
84
        } elseif (isset($data['error'])) {
85
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
86
        }
87
88
        $token = new StdOAuth2Token();
89
        $token->setAccessToken($data['access_token']);
90
        $token->setLifeTime($data['expires_in']);
91
92
        if (isset($data['refresh_token'])) {
93
            $token->setRefreshToken($data['refresh_token']);
94
            unset($data['refresh_token']);
95
        }
96
97
        unset($data['access_token'], $data['expires_in']);
98
99
        $token->setExtraParams($data);
100
101
        return $token;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function getExtraOAuthHeaders()
108
    {
109
        // Reddit uses a Basic OAuth header
110
        return ['Authorization' => 'Basic ' .
111
            base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()), ];
112
    }
113
}
114