Completed
Pull Request — master (#479)
by Andrey
03:26
created

Reddit   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 102
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
B parseAccessTokenResponse() 0 26 5
A getExtraOAuthHeaders() 0 6 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
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 Reddit extends AbstractService
14
{
15
    /**
16
     * Defined scopes
17
     *
18
     * @link 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 = array(),
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']);
98
        unset($data['expires_in']);
99
100
        $token->setExtraParams($data);
101
102
        return $token;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    protected function getExtraOAuthHeaders()
109
    {
110
        // Reddit uses a Basic OAuth header
111
        return array('Authorization' => 'Basic ' .
112
            base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()));
113
    }
114
}
115