1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand; |
6
|
|
|
use ApiClients\Client\Github\CommandBus\Command\IteratePagesCommand; |
7
|
|
|
use ApiClients\Tools\CommandBus\CommandBus; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use React\Promise\CancellablePromiseInterface; |
10
|
|
|
use React\Promise\FulfilledPromise; |
11
|
|
|
use Rx\Disposable\CallbackDisposable; |
12
|
|
|
use Rx\Observable; |
13
|
|
|
use Rx\ObserverInterface; |
14
|
|
|
use Rx\SchedulerInterface; |
15
|
|
|
use function React\Promise\all; |
16
|
|
|
use function React\Promise\resolve; |
17
|
|
|
|
18
|
|
|
class IteratePagesHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var CommandBus |
22
|
|
|
*/ |
23
|
|
|
private $commandBus; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param CommandBus $commandBus |
27
|
|
|
*/ |
28
|
|
|
public function __construct(CommandBus $commandBus) |
29
|
|
|
{ |
30
|
|
|
$this->commandBus = $commandBus; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function handle(IteratePagesCommand $command): CancellablePromiseInterface |
34
|
|
|
{ |
35
|
|
|
return resolve(Observable::create(function ( |
36
|
|
|
ObserverInterface $observer, |
37
|
|
|
SchedulerInterface $scheduler |
|
|
|
|
38
|
|
|
) use ($command) { |
39
|
|
|
$promise = $this->commandBus-> |
40
|
|
|
handle(new SimpleRequestCommand($command->getPath()))-> |
41
|
|
|
done(function ($response) use ($observer) { |
42
|
|
|
return $this->handleResponse($response, $observer); |
43
|
|
|
}) |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
return new CallbackDisposable(function () use ($promise) { |
47
|
|
|
$promise->cancel(); |
48
|
|
|
}); |
49
|
|
|
})); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function handleResponse( |
53
|
|
|
ResponseInterface $response, |
54
|
|
|
ObserverInterface $observer |
55
|
|
|
): CancellablePromiseInterface { |
56
|
|
|
if (!$response->hasHeader('link')) { |
57
|
|
|
return $this->handleResponseContentsComplete($response, $observer); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$links = [ |
61
|
|
|
'next' => false, |
62
|
|
|
'last' => false, |
63
|
|
|
]; |
64
|
|
|
foreach (explode(', ', $response->getHeader('link')[0]) as $link) { |
65
|
|
|
list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<')); |
66
|
|
|
if (isset($links[$rel])) { |
67
|
|
|
$links[$rel] = $url; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($links['next'] === false || $links['last'] === false) { |
72
|
|
|
return $this->handleResponseContentsComplete($response, $observer); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($links['next'] == $links['last']) { |
76
|
|
|
return $this->handleResponseContentsComplete($response, $observer); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$promises = []; |
80
|
|
|
|
81
|
|
|
$promises[] = $this->commandBus-> |
82
|
|
|
handle(new SimpleRequestCommand($links['next']))-> |
83
|
|
|
then(function (ResponseInterface $response) use ($observer) { |
84
|
|
|
return $this->handleResponse($response, $observer); |
85
|
|
|
}) |
86
|
|
|
; |
87
|
|
|
|
88
|
|
|
$promises[] = $this->handleResponseContents($response, $observer); |
89
|
|
|
|
90
|
|
|
return all($promises); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function handleResponseContentsComplete( |
94
|
|
|
ResponseInterface $response, |
95
|
|
|
ObserverInterface $observer |
96
|
|
|
): CancellablePromiseInterface { |
97
|
|
|
return $this->handleResponseContents($response, $observer)->then(function () use ($observer) { |
98
|
|
|
$observer->onCompleted(); |
99
|
|
|
return new FulfilledPromise(); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function handleResponseContents( |
104
|
|
|
ResponseInterface $response, |
105
|
|
|
ObserverInterface $observer |
106
|
|
|
): CancellablePromiseInterface { |
107
|
|
|
$observer->onNext($response->getBody()->getJson()); |
|
|
|
|
108
|
|
|
return new FulfilledPromise(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.