Pocket::parseRequestTokenResponse()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 11
c 1
b 0
f 0
rs 10
cc 4
nc 3
nop 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 Pocket extends AbstractService
14
{
15
    public function __construct(
16
        CredentialsInterface $credentials,
17
        ClientInterface $httpClient,
18
        TokenStorageInterface $storage,
19
        $scopes = [],
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 = [])
44
    {
45
        $parameters = array_merge(
46
            $additionalParameters,
47
            [
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
            [
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
86
        return $data['code'];
87
    }
88
89
    public function requestAccessToken($code, $state = null)
90
    {
91
        $bodyParams = [
92
            'consumer_key' => $this->credentials->getConsumerId(),
93
            'code' => $code,
94
        ];
95
96
        $responseBody = $this->httpClient->retrieveResponse(
97
            $this->getAccessTokenEndpoint(),
98
            $bodyParams,
99
            $this->getExtraOAuthHeaders()
100
        );
101
        $token = $this->parseAccessTokenResponse($responseBody);
102
        $this->storage->storeAccessToken($this->service(), $token);
103
104
        return $token;
105
    }
106
107
    protected function parseAccessTokenResponse($responseBody)
108
    {
109
        parse_str($responseBody, $data);
110
111
        if ($data === null || !is_array($data)) {
112
            throw new TokenResponseException('Unable to parse response.');
113
        } elseif (isset($data['error'])) {
114
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
115
        }
116
117
        $token = new StdOAuth2Token();
118
        //$token->setRequestToken($data['access_token']);
119
        $token->setAccessToken($data['access_token']);
120
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
121
        unset($data['access_token']);
122
        $token->setExtraParams($data);
123
124
        return $token;
125
    }
126
}
127