1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\AppVeyor\Service; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use RingCentral\Psr7\Request; |
8
|
|
|
use Rx\AsyncSchedulerInterface; |
9
|
|
|
use Rx\Observable; |
10
|
|
|
use Rx\Scheduler; |
11
|
|
|
use Rx\Subject\Subject; |
12
|
|
|
use function igorw\get_in; |
13
|
|
|
|
14
|
|
|
class IteratePagesService |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var RequestService |
18
|
|
|
*/ |
19
|
|
|
private $requestService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var AsyncSchedulerInterface |
23
|
|
|
*/ |
24
|
|
|
private $scheduler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param RequestService $requestService |
28
|
|
|
* @param AsyncSchedulerInterface|null $scheduler |
29
|
|
|
*/ |
30
|
1 |
|
public function __construct(RequestService $requestService, AsyncSchedulerInterface $scheduler = null) |
31
|
|
|
{ |
32
|
1 |
|
$this->requestService = $requestService; |
33
|
1 |
|
$this->scheduler = $scheduler ?: Scheduler::getAsync(); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
1 |
|
public function iterate( |
37
|
|
|
string $path, |
38
|
|
|
string $collectionIndex, |
39
|
|
|
string $identifierIndex, |
40
|
|
|
string $identifierQueryKey, |
41
|
|
|
array $options = [] |
42
|
|
|
): Observable { |
43
|
1 |
|
$paths = new Subject(); |
44
|
|
|
|
45
|
1 |
|
return Observable::of($path, $this->scheduler) |
46
|
1 |
|
->merge($paths) |
47
|
1 |
|
->flatMap(function ($path) use ($options) { |
48
|
1 |
|
return Observable::fromPromise($this->requestService->request( |
49
|
1 |
|
new Request('GET', $path), |
50
|
1 |
|
$options |
51
|
|
|
)); |
52
|
1 |
|
}) |
53
|
1 |
|
->do(function (ResponseInterface $response) use ( |
54
|
1 |
|
$path, |
55
|
1 |
|
$paths, |
56
|
1 |
|
$collectionIndex, |
57
|
1 |
|
$identifierIndex, |
58
|
1 |
|
$identifierQueryKey |
59
|
|
|
) { |
60
|
1 |
|
$json = $response->getBody()->getParsedContents(); |
|
|
|
|
61
|
1 |
|
$items = $json; |
62
|
1 |
|
if ($collectionIndex !== '') { |
63
|
1 |
|
$items = get_in($json, explode('.', $collectionIndex)); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
if (count($items) === 0) { |
67
|
1 |
|
$paths->onCompleted(); |
68
|
|
|
|
69
|
1 |
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$item = array_pop($items); |
73
|
1 |
|
$identifier = get_in($item, explode('.', $identifierIndex), false); |
74
|
|
|
|
75
|
1 |
|
if ($identifier === false) { |
76
|
|
|
$paths->onCompleted(); |
77
|
|
|
|
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$this->scheduler->schedule(function () use ($path, $paths, $identifier, $identifierQueryKey) { |
82
|
1 |
|
$paths->onNext($path . '&' . $identifierQueryKey . '=' . rawurlencode((string)$identifier)); |
83
|
1 |
|
}); |
84
|
1 |
|
}) |
85
|
1 |
|
->map(function (ResponseInterface $response) { |
86
|
1 |
|
return $response->getBody()->getParsedContents(); |
|
|
|
|
87
|
1 |
|
}) |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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: