|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\Service; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use React\Promise\CancellablePromiseInterface; |
|
8
|
|
|
use RingCentral\Psr7\Request; |
|
9
|
|
|
use Rx\Disposable\CallbackDisposable; |
|
10
|
|
|
use Rx\Observable; |
|
11
|
|
|
use Rx\ObserverInterface; |
|
12
|
|
|
use Rx\SchedulerInterface; |
|
13
|
|
|
use Rx\Subject\Subject; |
|
14
|
|
|
use function React\Promise\all; |
|
15
|
|
|
use function React\Promise\resolve; |
|
16
|
|
|
|
|
17
|
|
|
class IteratePagesService |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var RequestService |
|
21
|
|
|
*/ |
|
22
|
|
|
private $requestService; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param RequestService $requestService |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct(RequestService $requestService) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->requestService = $requestService; |
|
30
|
2 |
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
public function iterate(string $path): Observable |
|
33
|
|
|
{ |
|
34
|
|
|
return Observable::create(function ( |
|
35
|
|
|
ObserverInterface $observer |
|
36
|
|
|
) use ($path) { |
|
37
|
2 |
|
$subject = new Subject(); |
|
38
|
2 |
|
$subject->asObservable()->subscribe( |
|
39
|
2 |
|
[$observer, 'onNext'], |
|
40
|
2 |
|
[$observer, 'onError'], |
|
41
|
2 |
|
[$observer, 'onCompleted'] |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
2 |
|
$this->sendRequest($path, $subject); |
|
45
|
|
|
|
|
46
|
|
|
return new CallbackDisposable(function () use ($subject) { |
|
47
|
2 |
|
$subject->dispose(); |
|
48
|
2 |
|
}); |
|
49
|
2 |
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
private function sendRequest(string $path, Subject $subject) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->requestService-> |
|
55
|
2 |
|
handle(new Request('GET', $path))-> |
|
56
|
2 |
|
then( |
|
57
|
|
|
function ($response) use ($subject) { |
|
58
|
2 |
|
$this->handleResponse($response, $subject); |
|
59
|
2 |
|
}, |
|
60
|
2 |
|
function ($error) use ($subject) { |
|
61
|
|
|
$subject->onError($error); |
|
62
|
2 |
|
} |
|
63
|
|
|
) |
|
64
|
|
|
; |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
private function handleResponse( |
|
68
|
|
|
ResponseInterface $response, |
|
69
|
|
|
Subject $subject |
|
70
|
|
|
) { |
|
71
|
2 |
|
$subject->onNext($response->getBody()->getJson()); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
2 |
|
if ($subject->isDisposed() || !$subject->hasObservers()) { |
|
74
|
|
|
$subject->onCompleted(); |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
if (!$response->hasHeader('link')) { |
|
79
|
|
|
$subject->onCompleted(); |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$links = [ |
|
84
|
2 |
|
'next' => false, |
|
85
|
|
|
'last' => false, |
|
86
|
|
|
]; |
|
87
|
2 |
|
foreach (explode(', ', $response->getHeader('link')[0]) as $link) { |
|
88
|
2 |
|
list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<')); |
|
89
|
2 |
|
if (isset($links[$rel])) { |
|
90
|
2 |
|
$links[$rel] = $url; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
if ($links['next'] === false || $links['last'] === false) { |
|
95
|
1 |
|
$subject->onCompleted(); |
|
96
|
1 |
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
$this->sendRequest($links['next'], $subject); |
|
100
|
2 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: