1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\Service; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Service\ServiceInterface; |
6
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use React\Promise\CancellablePromiseInterface; |
9
|
|
|
use RingCentral\Psr7\Request; |
10
|
|
|
use Rx\Disposable\CallbackDisposable; |
11
|
|
|
use Rx\Observable; |
12
|
|
|
use Rx\ObserverInterface; |
13
|
|
|
use Rx\SchedulerInterface; |
14
|
|
|
use Rx\Subject\Subject; |
15
|
|
|
use function React\Promise\all; |
16
|
|
|
use function React\Promise\resolve; |
17
|
|
|
|
18
|
|
|
class IteratePagesService implements ServiceInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var RequestService |
22
|
|
|
*/ |
23
|
|
|
private $requestService; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param RequestService $requestService |
27
|
|
|
*/ |
28
|
2 |
|
public function __construct(RequestService $requestService) |
29
|
|
|
{ |
30
|
2 |
|
$this->requestService = $requestService; |
31
|
2 |
|
} |
32
|
|
|
|
33
|
2 |
|
public function handle(string $path = null): CancellablePromiseInterface |
34
|
|
|
{ |
35
|
|
|
return resolve(Observable::create(function ( |
36
|
|
|
ObserverInterface $observer, |
37
|
|
|
SchedulerInterface $scheduler |
38
|
|
|
) use ($path) { |
39
|
|
|
|
40
|
2 |
|
$subject = new Subject(); |
41
|
2 |
|
$subject->asObservable()->subscribeCallback( |
42
|
2 |
|
[$observer, 'onNext'], |
43
|
2 |
|
[$observer, 'onError'], |
44
|
2 |
|
[$observer, 'onCompleted'], |
45
|
|
|
$scheduler |
46
|
|
|
); |
47
|
|
|
|
48
|
2 |
|
$this->sendRequest($path, $subject); |
49
|
|
|
|
50
|
|
|
return new CallbackDisposable(function () use ($subject) { |
51
|
1 |
|
$subject->dispose(); |
52
|
2 |
|
}); |
53
|
2 |
|
})); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
private function sendRequest(string $path, Subject $subject) |
57
|
|
|
{ |
58
|
2 |
|
$this->requestService-> |
59
|
2 |
|
handle(new Request('GET', $path))-> |
60
|
2 |
|
then( |
61
|
|
|
function ($response) use ($subject) { |
62
|
2 |
|
$this->handleResponse($response, $subject); |
63
|
2 |
|
}, |
64
|
2 |
|
function ($error) use ($subject) { |
65
|
|
|
$subject->onError($error); |
66
|
2 |
|
} |
67
|
|
|
) |
68
|
|
|
; |
69
|
2 |
|
} |
70
|
|
|
|
71
|
2 |
|
private function handleResponse( |
72
|
|
|
ResponseInterface $response, |
73
|
|
|
Subject $subject |
74
|
|
|
) { |
75
|
2 |
|
$subject->onNext($response->getBody()->getJson()); |
|
|
|
|
76
|
|
|
|
77
|
2 |
|
if ($subject->isDisposed() || !$subject->hasObservers()) { |
78
|
|
|
$subject->onCompleted(); |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
if (!$response->hasHeader('link')) { |
83
|
|
|
$subject->onCompleted(); |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$links = [ |
88
|
2 |
|
'next' => false, |
89
|
|
|
'last' => false, |
90
|
|
|
]; |
91
|
2 |
|
foreach (explode(', ', $response->getHeader('link')[0]) as $link) { |
92
|
2 |
|
list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<')); |
93
|
2 |
|
if (isset($links[$rel])) { |
94
|
2 |
|
$links[$rel] = $url; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
if ($links['next'] === false || $links['last'] === false) { |
99
|
1 |
|
$subject->onCompleted(); |
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
$this->sendRequest($links['next'], $subject); |
104
|
2 |
|
} |
105
|
|
|
} |
106
|
|
|
|
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: