Authentication::obtainAccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
namespace ArianRashidi\PocketApi\Api;
3
4
/**
5
 * Class Authentication
6
 *
7
 * @package ArianRashidi\PocketApi\Api
8
 */
9
class Authentication extends Api
10
{
11
    /**
12
     * Obtain the access token and the username.
13
     *
14
     * @param string $requestToken
15
     *
16
     * @return mixed
17
     */
18
    public function obtainAccess(string $requestToken)
19
    {
20
        $data = [
21
            'consumer_key' => $this->pocket->getConsumerKey(),
22
            'code'         => $requestToken,
23
        ];
24
25
        $response = $this->request('/v3/oauth/authorize', $data);
26
        $responseData = json_decode($response->getBody());
27
28
        return $responseData;
29
    }
30
31
    /**
32
     * Obtain the request token.
33
     *
34
     * @param string      $redirectUrl
35
     * @param string|null $state
36
     *
37
     * @return mixed
38
     */
39
    public function obtainRequestToken(string $redirectUrl, string $state = null)
40
    {
41
        $data = ['consumer_key' => $this->pocket->getConsumerKey(), 'redirect_uri' => $redirectUrl];
42
        if (!is_null($state)) {
43
            $data = $data + ['state' => $state];
44
        }
45
46
        $response = $this->request('/v3/oauth/request', $data);
47
        $responseData = json_decode($response->getBody());
48
49
        return $responseData;
50
    }
51
52
    /**
53
     * Create the authorization url.
54
     *
55
     * @param string $requestToken
56
     * @param string $redirectUrl
57
     *
58
     * @return string
59
     */
60
    public function authorizationUrl(string $requestToken, string $redirectUrl): string
61
    {
62
        return 'https://getpocket.com/auth/authorize?request_token=' . $requestToken . '&redirect_uri=' . $redirectUrl;
63
    }
64
}
65