Completed
Pull Request — master (#467)
by
unknown
04:30
created

Todoist::getScopesDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
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 Todoist extends AbstractService
14
{
15
    /*
16
     * Defined scopes, see https://developer.todoist.com/#oauth for definitions.
17
     */
18
19
    /**
20
     * Grants permission to add tasks to the Inbox project (The application cannot read tasks data).
21
     */
22
    const SCOPE_TASK_ADD = 'task:add';
23
24
    /**
25
     * Grants read-only access to application data, including tasks, projects, labels, and filters.
26
     */
27
    const SCOPE_DATA_READ = 'data:read';
28
29
    /**
30
     * Grants read and write access to application data, including tasks, projects, labels, and filters.
31
     *
32
     * This scope includes task:add and data:read scopes.
33
     */
34
    const SCOPE_DATA_READ_WRITE = 'data:read_write';
35
36
    /**
37
     * Grants permission to delete application data, including tasks, labels, and filters.
38
     */
39
    const SCOPE_DATA_DELETE = 'data:delete';
40
41
    /**
42
     * Grants permission to delete projects.
43
     */
44
    const SCOPE_PROJECT_DELETE = 'project:delete';
45
46
47
    public function __construct(
48
        CredentialsInterface $credentials,
49
        ClientInterface $httpClient,
50
        TokenStorageInterface $storage,
51
        $scopes = array(),
52
        UriInterface $baseApiUri = null
53
    )
54
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
55
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
56
57
        if (null === $baseApiUri) {
58
            $this->baseApiUri = new Uri('https://todoist.com/API/v7/');
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getAuthorizationEndpoint()
66
    {
67
        return new Uri('https://todoist.com/oauth/authorize');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getAccessTokenEndpoint()
74
    {
75
        return new Uri('https://todoist.com/oauth/access_token');
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected function getAuthorizationMethod()
82
    {
83
        return static::AUTHORIZATION_METHOD_QUERY_STRING_V5;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function parseAccessTokenResponse($responseBody)
90
    {
91
        $data = json_decode($responseBody, true);
92
93
        if (null === $data || !is_array($data)) {
94
            throw new TokenResponseException('Unable to parse response.');
95
        } elseif (isset($data['error'])) {
96
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
97
        }
98
99
        $token = new StdOAuth2Token();
100
        $token->setAccessToken($data['access_token']);
101
        // Todoist tokens evidently never expire...
102
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
103
        unset($data['access_token']);
104
105
        $token->setExtraParams($data);
106
107
        return $token;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected function getScopesDelimiter()
114
    {
115
        return ',';
116
    }
117
}
118