Total Complexity | 51 |
Total Lines | 498 |
Duplicated Lines | 0 % |
Changes | 17 | ||
Bugs | 1 | Features | 2 |
Complex classes like Api often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Api, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Api { |
||
26 | |||
27 | /** |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | protected $log; |
||
31 | |||
32 | /** |
||
33 | * @var User |
||
34 | */ |
||
35 | protected $me; |
||
36 | |||
37 | /** |
||
38 | * @var Pool |
||
39 | */ |
||
40 | protected $pool; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $token; |
||
46 | |||
47 | /** |
||
48 | * @param string $token |
||
49 | * @param Pool|null $pool |
||
50 | */ |
||
51 | public function __construct (string $token, Pool $pool = null) { |
||
52 | $this->token = $token; |
||
53 | $this->pool = $pool ?? new Pool(); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * cURL transport. |
||
58 | * |
||
59 | * @param string $method |
||
60 | * @param string $path |
||
61 | * @param array $curlOpts |
||
62 | * @return null|array |
||
63 | * @internal |
||
64 | */ |
||
65 | protected function _exec (string $method, string $path, array $curlOpts = []) { |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * `HTTP DELETE` |
||
112 | * |
||
113 | * @param string $path |
||
114 | */ |
||
115 | public function delete (string $path): void { |
||
116 | $this->_exec('DELETE', $path); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * The central point of object creation. |
||
121 | * |
||
122 | * This can be overridden to return custom extensions. |
||
123 | * |
||
124 | * @param Api|Data $caller |
||
125 | * @param string $class |
||
126 | * @param array $data |
||
127 | * @return mixed|Data|AbstractEntity |
||
128 | */ |
||
129 | public function factory ($caller, string $class, array $data = []) { |
||
130 | return new $class($caller, $data); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * `HTTP GET` |
||
135 | * |
||
136 | * @param string $path |
||
137 | * @param array $query |
||
138 | * @param array $opt |
||
139 | * @return null|array |
||
140 | */ |
||
141 | public function get (string $path, array $query = [], array $opt = []) { |
||
142 | foreach ($opt as $name => $value) { |
||
143 | $query["opt_{$name}"] = $value; |
||
144 | } |
||
145 | $response = $this->_exec('GET', $path . '?' . http_build_query($query)); |
||
146 | return $response['data'] ?? null; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Loads and returns an {@link Attachment}. |
||
151 | * |
||
152 | * @param string $gid |
||
153 | * @return null|Attachment |
||
154 | */ |
||
155 | public function getAttachment (string $gid) { |
||
156 | return $this->load($this, Attachment::class, "attachments/{$gid}"); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Loads and returns a {@link CustomField}. |
||
161 | * |
||
162 | * @param string $gid |
||
163 | * @return null|CustomField |
||
164 | */ |
||
165 | public function getCustomField (string $gid) { |
||
166 | return $this->load($this, CustomField::class, "custom_fields/{$gid}"); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Returns the first known workspace of the authorized user. |
||
171 | * You should only rely on this if the authorized user is in exactly one workspace. |
||
172 | * |
||
173 | * @return Workspace |
||
174 | */ |
||
175 | public function getDefaultWorkspace () { |
||
176 | return $this->getMe()->getDefaultWorkspace(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return LoggerInterface |
||
181 | */ |
||
182 | public function getLog () { |
||
183 | return $this->log ?? $this->log = new NullLogger(); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns the authorized user. |
||
188 | * |
||
189 | * @return User |
||
190 | */ |
||
191 | public function getMe () { |
||
192 | return $this->me ?? $this->me = $this->getUser('me'); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return Pool |
||
197 | */ |
||
198 | final public function getPool () { |
||
199 | return $this->pool; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Loads and returns a {@link Portfolio}. |
||
204 | * |
||
205 | * @param string $gid |
||
206 | * @return null|Portfolio |
||
207 | */ |
||
208 | public function getPortfolio (string $gid) { |
||
209 | return $this->load($this, Portfolio::class, "portfolios/{$gid}"); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Loads and returns a {@link Project}. |
||
214 | * |
||
215 | * @param string $gid |
||
216 | * @return null|Project |
||
217 | */ |
||
218 | public function getProject (string $gid) { |
||
219 | return $this->load($this, Project::class, "projects/{$gid}"); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Loads and returns a {@link Section}. |
||
224 | * |
||
225 | * @param string $gid |
||
226 | * @return null|Section |
||
227 | */ |
||
228 | public function getSection (string $gid) { |
||
229 | return $this->load($this, Section::class, "sections/{$gid}"); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Loads and returns a {@link Story}. |
||
234 | * |
||
235 | * @param string $gid |
||
236 | * @return null|Story |
||
237 | */ |
||
238 | public function getStory (string $gid) { |
||
239 | return $this->load($this, Story::class, "stories/{$gid}"); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Loads and returns a {@link Tag}. |
||
244 | * |
||
245 | * @param string $gid |
||
246 | * @return null|Tag |
||
247 | */ |
||
248 | public function getTag (string $gid) { |
||
249 | return $this->load($this, Tag::class, "tags/{$gid}"); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Loads and returns a {@link Task}. |
||
254 | * |
||
255 | * @param string $gid |
||
256 | * @return null|Task |
||
257 | */ |
||
258 | public function getTask (string $gid) { |
||
259 | return $this->load($this, Task::class, "tasks/{$gid}"); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Loads and returns a {@link TaskList}. |
||
264 | * |
||
265 | * @param string $gid |
||
266 | * @return null|TaskList |
||
267 | */ |
||
268 | public function getTaskList (string $gid) { |
||
269 | return $this->load($this, TaskList::class, "user_task_lists/{$gid}"); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Loads and returns a {@link Team}. |
||
274 | * |
||
275 | * @param string $gid |
||
276 | * @return null|Team |
||
277 | */ |
||
278 | public function getTeam (string $gid) { |
||
279 | return $this->load($this, Team::class, "teams/{$gid}"); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Loads and returns a {@link User}. |
||
284 | * |
||
285 | * @param string $gid |
||
286 | * @return null|User |
||
287 | */ |
||
288 | public function getUser (string $gid) { |
||
289 | return $this->load($this, User::class, "users/{$gid}"); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param string $gid |
||
294 | * @return ProjectWebhook|TaskWebhook |
||
295 | */ |
||
296 | public function getWebhook (string $gid) { |
||
297 | return $this->pool->get($gid, $this, function() use ($gid) { |
||
298 | static $classes = [ |
||
299 | Project::TYPE => ProjectWebhook::class, |
||
300 | Task::TYPE => TaskWebhook::class |
||
301 | ]; |
||
302 | if ($remote = $this->get("webhooks/{$gid}", [], ['expand' => 'this'])) { |
||
303 | return $this->factory($this, $classes[$remote['resource_type']], $remote); |
||
304 | } |
||
305 | return null; |
||
306 | }); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Expands received webhook data as a full event object. |
||
311 | * |
||
312 | * @see https://developers.asana.com/docs/event |
||
313 | * |
||
314 | * @param array $data |
||
315 | * @return Event |
||
316 | */ |
||
317 | public function getWebhookEvent (array $data) { |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Loads and returns a {@link Workspace}. |
||
323 | * |
||
324 | * @param string $gid |
||
325 | * @return null|Workspace |
||
326 | */ |
||
327 | public function getWorkspace (string $gid) { |
||
328 | return $this->load($this, Workspace::class, "workspaces/{$gid}"); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Loads and returns a {@link Workspace} by name. |
||
333 | * |
||
334 | * @param string $name |
||
335 | * @return null|Workspace |
||
336 | */ |
||
337 | public function getWorkspaceByName (string $name) { |
||
338 | foreach ($this->getWorkspaces() as $workspace) { |
||
339 | if ($workspace->getName() === $name) { |
||
340 | return $workspace; |
||
341 | } |
||
342 | } |
||
343 | return null; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * All workspaces visible to the authorized user. |
||
348 | * |
||
349 | * @return Workspace[] |
||
350 | */ |
||
351 | public function getWorkspaces () { |
||
352 | return $this->getMe()->getWorkspaces(); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Loads the entity found at the given path + query. |
||
357 | * |
||
358 | * @param Api|Data $caller |
||
359 | * @param string $class |
||
360 | * @param string $path |
||
361 | * @param array $query |
||
362 | * @return null|mixed|AbstractEntity |
||
363 | */ |
||
364 | public function load ($caller, string $class, string $path, array $query = []) { |
||
365 | $key = rtrim($path . '?' . http_build_query($query), '?'); |
||
366 | return $this->pool->get($key, $caller, function($caller) use ($class, $path, $query) { |
||
367 | if ($data = $this->get($path, $query, ['expand' => 'this'])) { |
||
368 | return $this->factory($caller, $class, $data); |
||
369 | } |
||
370 | return null; |
||
371 | }); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Returns all results from {@link loadEach()} |
||
376 | * |
||
377 | * @param Api|Data $caller |
||
378 | * @param string $class |
||
379 | * @param string $path |
||
380 | * @param array $query |
||
381 | * @return array|AbstractEntity[] |
||
382 | */ |
||
383 | public function loadAll ($caller, string $class, string $path, array $query = []) { |
||
384 | return iterator_to_array($this->loadEach(...func_get_args())); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Loads and yields each entity found at the given path + query. |
||
389 | * |
||
390 | * The result-set is not pooled, but individual entities are. |
||
391 | * |
||
392 | * @param Api|Data $caller |
||
393 | * @param string $class |
||
394 | * @param string $path |
||
395 | * @param array $query `limit` can exceed `100` here. |
||
396 | * @return Generator|AbstractEntity[] |
||
397 | */ |
||
398 | public function loadEach ($caller, string $class, string $path, array $query = []) { |
||
399 | $query['opt_expand'] = 'this'; |
||
400 | $remain = $query['limit'] ?? PHP_INT_MAX; |
||
401 | do { |
||
402 | $query['limit'] = min($remain, 100); |
||
403 | $page = $this->_exec('GET', $path . '?' . http_build_query($query)); |
||
404 | foreach ($page['data'] as $each) { |
||
405 | yield $this->pool->get($each['gid'], $caller, function($caller) use ($class, $each) { |
||
406 | return $this->factory($caller, $class, $each); |
||
407 | }); |
||
408 | $remain--; |
||
409 | } |
||
410 | $query['offset'] = $page['next_page']['offset'] ?? null; |
||
411 | } while ($remain and $query['offset']); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * `HTTP POST` |
||
416 | * |
||
417 | * @param string $path |
||
418 | * @param array $data |
||
419 | * @param array $opt |
||
420 | * @return null|array |
||
421 | */ |
||
422 | public function post (string $path, array $data = [], array $opt = []) { |
||
423 | $response = $this->_exec('POST', $path, [ |
||
424 | CURLOPT_HTTPHEADER => ['Content-Type: application/json'], |
||
425 | CURLOPT_POSTFIELDS => json_encode([ |
||
426 | 'options' => $opt, |
||
427 | 'data' => $data |
||
428 | ], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) |
||
429 | ]); |
||
430 | return $response['data'] ?? null; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * `HTTP PUT` |
||
435 | * |
||
436 | * @param string $path |
||
437 | * @param array $data |
||
438 | * @param array $opt |
||
439 | * @return null|array |
||
440 | */ |
||
441 | public function put (string $path, array $data = [], array $opt = []) { |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @param LoggerInterface $log |
||
454 | */ |
||
455 | public function setLog (LoggerInterface $log) { |
||
456 | $this->log = $log; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @param Pool $pool |
||
461 | */ |
||
462 | public function setPool (Pool $pool) { |
||
463 | $this->pool = $pool; |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Polls for new events. |
||
468 | * |
||
469 | * If the given sync token expired, a `412` is thrown. |
||
470 | * |
||
471 | * If the given token is `null`, this returns an empty array. |
||
472 | * |
||
473 | * @see https://developers.asana.com/docs/get-events-on-a-resource |
||
474 | * |
||
475 | * @param string $gid A project or task GID. |
||
476 | * @param null|string $token Updated to the new token. |
||
477 | * @return Event[] |
||
478 | */ |
||
479 | public function sync (string $gid, ?string &$token) { |
||
480 | try { |
||
481 | /** @var array $remote Asana throws 400 for missing entities here. */ |
||
482 | $remote = $this->_exec('GET', 'events?' . http_build_query([ |
||
483 | 'resource' => $gid, |
||
484 | 'sync' => $token, |
||
485 | 'opt_expand' => 'this' |
||
486 | ])); |
||
487 | } |
||
488 | catch (AsanaError $error) { |
||
489 | if ($error->getCode() === 412) { |
||
490 | $remote = json_decode($error->getMessage(), true, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR); |
||
491 | if (!isset($token)) { |
||
492 | // API docs say: "The response will be the same as for an expired sync token." |
||
493 | // The caller knowingly gave a null token, so we don't need to rethrow. |
||
494 | $token = $remote['sync']; |
||
495 | return []; |
||
496 | } |
||
497 | // Token expired. Update and rethrow. |
||
498 | $token = $remote['sync']; |
||
499 | } |
||
500 | throw $error; |
||
501 | } |
||
502 | $token = $remote['sync']; |
||
503 | $events = array_map(function(array $each) { |
||
504 | return $this->factory($this, Event::class, $each); |
||
505 | }, $remote['data']); |
||
506 | usort($events, function(Event $a, Event $b) { |
||
507 | return $a->getCreatedAt() <=> $b->getCreatedAt(); |
||
508 | }); |
||
509 | return $events; |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * `HTTP POST` (multipart/form-data) |
||
514 | * |
||
515 | * @param string $file |
||
516 | * @param string $to |
||
517 | * @return array |
||
518 | */ |
||
519 | public function upload (string $file, string $to) { |
||
523 | } |
||
524 | } |