1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Travis\Service; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Travis\CommandBus\Command\BroadcastsCommand; |
6
|
|
|
use ApiClients\Client\Travis\Resource\BroadcastInterface; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
8
|
|
|
use ApiClients\Foundation\Service\ServiceInterface; |
9
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use React\Promise\CancellablePromiseInterface; |
12
|
|
|
use React\Promise\PromiseInterface; |
13
|
|
|
use RingCentral\Psr7\Request; |
14
|
|
|
use Rx\Observable; |
15
|
|
|
use Rx\React\Promise; |
16
|
|
|
use function React\Promise\resolve; |
17
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
18
|
|
|
|
19
|
|
|
final class FetchAndIterateService implements ServiceInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var RequestService |
23
|
|
|
*/ |
24
|
|
|
private $requestService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Hydrator |
28
|
|
|
*/ |
29
|
|
|
private $hydrator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param RequestService $requestService |
33
|
|
|
* @param Hydrator $hydrator |
34
|
|
|
*/ |
35
|
1 |
|
public function __construct(RequestService $requestService, Hydrator $hydrator) |
36
|
|
|
{ |
37
|
1 |
|
$this->requestService = $requestService; |
38
|
1 |
|
$this->hydrator = $hydrator; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string|null $path |
43
|
|
|
* @param string|null $index |
44
|
|
|
* @param string|null $hydrateClass |
45
|
|
|
* @return CancellablePromiseInterface |
46
|
|
|
*/ |
47
|
1 |
|
public function handle( |
48
|
|
|
string $path = null, |
49
|
|
|
string $index = null, |
50
|
|
|
string $hydrateClass = null |
51
|
|
|
): CancellablePromiseInterface { |
52
|
1 |
|
return resolve( |
53
|
1 |
|
Promise::toObservable( |
54
|
1 |
|
$this->requestService->handle( |
55
|
1 |
|
new Request('GET', $path) |
56
|
|
|
) |
57
|
|
|
)->flatMap(function ($response) use ($index) { |
58
|
1 |
|
return Observable::fromArray($response->getBody()->getJson()[$index]); |
59
|
1 |
|
})->map(function ($json) use ($hydrateClass) { |
60
|
1 |
|
return $this->hydrator->hydrate( |
61
|
|
|
$hydrateClass, |
62
|
|
|
$json |
63
|
|
|
); |
64
|
1 |
|
}) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|