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

Pocket::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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