Completed
Pull Request — master (#478)
by
unknown
02:35
created

Pocket   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 0
loc 113
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationUri() 0 17 2
A requestRequestToken() 0 14 1
A parseRequestTokenResponse() 0 11 4
A requestAccessToken() 0 17 1
A parseAccessTokenResponse() 0 19 4
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\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Client\ClientInterface;
12
13
class Pocket extends AbstractService
14
{
15
    public function __construct(
16
        CredentialsInterface $credentials,
17
        ClientInterface $httpClient,
18
        TokenStorageInterface $storage,
19
        $scopes = array(),
20
        UriInterface $baseApiUri = null
21
    ) {
22
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
23
        if ($baseApiUri === null) {
24
            $this->baseApiUri = new Uri('https://getpocket.com/v3/');
25
        }
26
    }
27
    
28
    public function getRequestTokenEndpoint()
29
    {
30
        return new Uri('https://getpocket.com/v3/oauth/request');
31
    }
32
    
33
    public function getAuthorizationEndpoint()
34
    {
35
        return new Uri('https://getpocket.com/auth/authorize');
36
    }
37
    
38
    public function getAccessTokenEndpoint()
39
    {
40
        return new Uri('https://getpocket.com/v3/oauth/authorize');
41
    }
42
    
43
    public function getAuthorizationUri(array $additionalParameters = array())
44
    {
45
        $parameters = array_merge(
46
            $additionalParameters,
47
            array(
48
                'redirect_uri' => $this->credentials->getCallbackUrl(),
49
            )
50
        );
51
        
52
        // Build the url
53
        $url = clone $this->getAuthorizationEndpoint();
54
        foreach ($parameters as $key => $val) {
55
            $url->addToQuery($key, $val);
56
        }
57
58
        return $url;
59
    }
60
    
61
    public function requestRequestToken()
62
    {
63
        $responseBody = $this->httpClient->retrieveResponse(
64
            $this->getRequestTokenEndpoint(),
65
            array(
66
                'consumer_key' => $this->credentials->getConsumerId(),
67
                'redirect_uri' => $this->credentials->getCallbackUrl(),
68
            )
69
        );
70
        
71
        $code = $this->parseRequestTokenResponse($responseBody);
72
73
        return $code;
74
    }
75
    
76
    protected function parseRequestTokenResponse($responseBody)
77
    {
78
        parse_str($responseBody, $data);
79
        
80
        if (null === $data || !is_array($data)) {
81
            throw new TokenResponseException('Unable to parse response.');
82
        } elseif (!isset($data['code'])) {
83
            throw new TokenResponseException('Error in retrieving code.');
84
        }
85
        return $data['code'];
86
    }
87
    
88
    public function requestAccessToken($code)
89
    {
90
        $bodyParams = array(
91
            'consumer_key'     => $this->credentials->getConsumerId(),
92
            'code'             => $code,
93
        );
94
95
        $responseBody = $this->httpClient->retrieveResponse(
96
            $this->getAccessTokenEndpoint(),
97
            $bodyParams,
98
            $this->getExtraOAuthHeaders()
99
        );
100
        $token = $this->parseAccessTokenResponse($responseBody);
101
        $this->storage->storeAccessToken($this->service(), $token);
102
103
        return $token;
104
    }
105
    
106
    protected function parseAccessTokenResponse($responseBody)
107
    {
108
        parse_str($responseBody, $data);
109
        
110
        if ($data === null || !is_array($data)) {
111
            throw new TokenResponseException('Unable to parse response.');
112
        } elseif (isset($data['error'])) {
113
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
114
        }
115
        
116
        $token = new StdOAuth2Token();
117
        #$token->setRequestToken($data['access_token']);
118
        $token->setAccessToken($data['access_token']);
119
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
120
        unset($data['access_token']);
121
        $token->setExtraParams($data);
122
        
123
        return $token;
124
    }
125
}
126