Skip to content

Commit d5dc5b3

Browse files
committed
Improved code. This is v0.8.0. ✨
1 parent 6ae5368 commit d5dc5b3

File tree

8 files changed

+93
-101
lines changed

8 files changed

+93
-101
lines changed

src/FabianBeiner/Todoist/TodoistClient.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22
/**
33
* PHP Client for Todoist
4-
* A PHP client library that provides a native interface to the official Todoist REST API (v8).
4+
* A PHP client library that provides a native interface to the official Todoist REST API.
55
*
66
* @author Fabian Beiner <[email protected]>
7-
* @author Balazs Csaba <[email protected]>
87
* @license https://opensource.org/licenses/MIT MIT
98
*
10-
* @version 0.7.2 <2018-06-04>
9+
* @version 0.8.0 <2019-07-19>
1110
*
1211
* @see https://github.com/FabianBeiner/Todoist-PHP-API-Library
1312
*/
@@ -17,6 +16,7 @@
1716
use GuzzleHttp\Client as GuzzleClient;
1817
use GuzzleHttp\Promise\PromiseInterface;
1918
use GuzzleHttp\RequestOptions;
19+
use function strlen;
2020

2121
/**
2222
* Class TodoistClient.
@@ -44,7 +44,7 @@ class TodoistClient extends GuzzleClient
4444
public function __construct(string $apiToken, array $config = [])
4545
{
4646
$apiToken = trim($apiToken);
47-
if (40 !== \strlen($apiToken)) {
47+
if (40 !== strlen($apiToken)) {
4848
throw new TodoistException('The provided API token is invalid.');
4949
}
5050

@@ -71,37 +71,16 @@ public function __construct(string $apiToken, array $config = [])
7171
* @param array $options
7272
*
7373
* @return PromiseInterface
74+
* @throws \Exception
7475
*/
7576
public function requestAsync($method, $uri = '', array $options = []): PromiseInterface
7677
{
7778
// Ensure the “X-Request-Id” header gets regenerated for every call.
78-
$options['headers']['X-Request-Id'] = $this->generateV4GUID();
79+
$options['headers']['X-Request-Id'] = bin2hex(random_bytes(16));
7980

8081
return parent::requestAsync($method, $uri, $options);
8182
}
8283

83-
/**
84-
* Generate a v4 GUID string.
85-
*
86-
* @author Pavel Volyntsev <[email protected]>
87-
*
88-
* @see http://php.net/manual/en/function.com-create-guid.php#117893
89-
*
90-
* @return string A v4 GUID.
91-
*/
92-
private function generateV4GUID(): string
93-
{
94-
if (true === \function_exists('com_create_guid')) {
95-
return trim(com_create_guid(), '{}');
96-
}
97-
98-
$data = openssl_random_pseudo_bytes(16);
99-
$data[6] = \chr(\ord($data[6]) & 0x0f | 0x40);
100-
$data[8] = \chr(\ord($data[8]) & 0x3f | 0x80);
101-
102-
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
103-
}
104-
10584
/**
10685
* Prepare Guzzle request data.
10786
*

src/FabianBeiner/Todoist/TodoistCommentsTrait.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22
/**
3-
* Todoist PHP API Library
4-
* An unofficial PHP client library for accessing the official Todoist REST API.
3+
* PHP Client for Todoist
4+
* A PHP client library that provides a native interface to the official Todoist REST API.
55
*
66
* @author Fabian Beiner <[email protected]>
7-
* @author Balazs Csaba <[email protected]>
87
* @license https://opensource.org/licenses/MIT MIT
98
*
109
* @see https://github.com/FabianBeiner/Todoist-PHP-API-Library
@@ -45,7 +44,8 @@ public function getAllComments(string $type, int $typeId)
4544
return false;
4645
}
4746

48-
$query = http_build_query([$type . '_id' => $typeId], null, '&', PHP_QUERY_RFC3986);
47+
$query = http_build_query([$type . '_id' => $typeId], null, '&', PHP_QUERY_RFC3986);
48+
/** @var object $result Result of the GET request. */
4949
$result = $this->get('comments?' . $query);
5050

5151
$status = $result->getStatusCode();
@@ -59,6 +59,15 @@ public function getAllComments(string $type, int $typeId)
5959
return false;
6060
}
6161

62+
/**
63+
* Validates an ID to be a positive integer.
64+
*
65+
* @param mixed $id
66+
*
67+
* @return bool
68+
*/
69+
abstract protected function validateId($id): bool;
70+
6271
/**
6372
* Alias for getAllComments('task', $taskId).
6473
*
@@ -101,10 +110,11 @@ public function createComment(string $type, int $typeId, string $comment)
101110
return false;
102111
}
103112

104-
$data = $this->prepareRequestData([
105-
$type . '_id' => $typeId,
106-
'content' => $comment,
107-
]);
113+
$data = $this->prepareRequestData([
114+
$type . '_id' => $typeId,
115+
'content' => $comment,
116+
]);
117+
/** @var object $result Result of the POST request. */
108118
$result = $this->post('comments', $data);
109119

110120
if (200 === $result->getStatusCode()) {
@@ -114,6 +124,15 @@ public function createComment(string $type, int $typeId, string $comment)
114124
return false;
115125
}
116126

127+
/**
128+
* Prepare Guzzle request data.
129+
*
130+
* @param array $data
131+
*
132+
* @return array
133+
*/
134+
abstract protected function prepareRequestData(array $data = []): array;
135+
117136
/**
118137
* Alias for createComment('task', $projectId, $comment).
119138
*
@@ -140,6 +159,7 @@ public function getComment(int $commentId)
140159
return false;
141160
}
142161

162+
/** @var object $result Result of the GET request. */
143163
$result = $this->get('comments/' . $commentId);
144164

145165
if (200 === $result->getStatusCode()) {
@@ -163,7 +183,8 @@ public function updateComment(int $commentId, string $content): bool
163183
return false;
164184
}
165185

166-
$data = $this->prepareRequestData(['content' => $content]);
186+
$data = $this->prepareRequestData(['content' => $content]);
187+
/** @var object $result Result of the POST request. */
167188
$result = $this->post('comments/' . $commentId, $data);
168189

169190
return 204 === $result->getStatusCode();
@@ -182,26 +203,9 @@ public function deleteComment(int $commentId): bool
182203
return false;
183204
}
184205

206+
/** @var object $result Result of the DELETE request. */
185207
$result = $this->delete('comments/' . $commentId);
186208

187209
return 204 === $result->getStatusCode();
188210
}
189-
190-
/**
191-
* Prepare Guzzle request data.
192-
*
193-
* @param array $data
194-
*
195-
* @return array
196-
*/
197-
abstract protected function prepareRequestData(array $data = []): array;
198-
199-
/**
200-
* Validates an ID to be a positive integer.
201-
*
202-
* @param mixed $id
203-
*
204-
* @return bool
205-
*/
206-
abstract protected function validateId($id): bool;
207211
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?php
22
/**
33
* PHP Client for Todoist
4-
* A PHP client library that provides a native interface to the official Todoist REST API (v8).
4+
* A PHP client library that provides a native interface to the official Todoist REST API.
55
*
66
* @author Fabian Beiner <[email protected]>
7-
* @author Balazs Csaba <[email protected]>
87
* @license https://opensource.org/licenses/MIT MIT
98
*
109
* @see https://github.com/FabianBeiner/Todoist-PHP-API-Library
1110
*/
1211

1312
namespace FabianBeiner\Todoist;
1413

14+
use Exception;
15+
1516
/**
1617
* Class TodoistException.
1718
*/
18-
class TodoistException extends \Exception
19+
class TodoistException extends Exception
1920
{
2021
}

src/FabianBeiner/Todoist/TodoistLabelsTrait.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22
/**
33
* PHP Client for Todoist
4-
* A PHP client library that provides a native interface to the official Todoist REST API (v8).
4+
* A PHP client library that provides a native interface to the official Todoist REST API.
55
*
66
* @author Fabian Beiner <[email protected]>
7-
* @author Balazs Csaba <[email protected]>
87
* @license https://opensource.org/licenses/MIT MIT
98
*
109
* @see https://github.com/FabianBeiner/Todoist-PHP-API-Library
@@ -24,6 +23,7 @@ trait TodoistLabelsTrait
2423
*/
2524
public function getAllLabels()
2625
{
26+
/** @var object $result Result of the GET request. */
2727
$result = $this->get('labels');
2828

2929
$status = $result->getStatusCode();
@@ -51,6 +51,7 @@ public function createLabel(string $name)
5151
}
5252

5353
$data = $this->prepareRequestData(['name' => $name]);
54+
/** @var object $result Result of the POST request. */
5455
$result = $this->post('labels', $data);
5556

5657
if (200 === $result->getStatusCode()) {
@@ -60,6 +61,15 @@ public function createLabel(string $name)
6061
return false;
6162
}
6263

64+
/**
65+
* Prepare Guzzle request data.
66+
*
67+
* @param array $data
68+
*
69+
* @return array
70+
*/
71+
abstract protected function prepareRequestData(array $data = []): array;
72+
6373
/**
6474
* Get a label.
6575
*
@@ -73,6 +83,7 @@ public function getLabel(int $labelId)
7383
return false;
7484
}
7585

86+
/** @var object $result Result of the GET request. */
7687
$result = $this->get('labels/' . $labelId);
7788

7889
if (200 === $result->getStatusCode()) {
@@ -82,6 +93,15 @@ public function getLabel(int $labelId)
8293
return false;
8394
}
8495

96+
/**
97+
* Validates an ID to be a positive integer.
98+
*
99+
* @param mixed $id
100+
*
101+
* @return bool
102+
*/
103+
abstract protected function validateId($id): bool;
104+
85105
/**
86106
* Alias for updateLabel().
87107
*
@@ -110,6 +130,7 @@ public function updateLabel(int $labelId, string $name): bool
110130
}
111131

112132
$data = $this->prepareRequestData(['name' => $name]);
133+
/** @var object $result Result of the POST request. */
113134
$result = $this->post('labels/' . $labelId, $data);
114135

115136
return 204 === $result->getStatusCode();
@@ -128,26 +149,9 @@ public function deleteLabel(int $labelId): bool
128149
return false;
129150
}
130151

152+
/** @var object $result Result of the DELETE request. */
131153
$result = $this->delete('labels/' . $labelId);
132154

133155
return 204 === $result->getStatusCode();
134156
}
135-
136-
/**
137-
* Prepare Guzzle request data.
138-
*
139-
* @param array $data
140-
*
141-
* @return array
142-
*/
143-
abstract protected function prepareRequestData(array $data = []): array;
144-
145-
/**
146-
* Validates an ID to be a positive integer.
147-
*
148-
* @param mixed $id
149-
*
150-
* @return bool
151-
*/
152-
abstract protected function validateId($id): bool;
153157
}

0 commit comments

Comments
 (0)