|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\RepositoriesCommand; |
|
6
|
|
|
use ApiClients\Client\Github\Resource\RepositoryInterface; |
|
7
|
|
|
use ApiClients\Client\Github\Service\IteratePagesService; |
|
8
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
|
9
|
|
|
use React\Promise\PromiseInterface; |
|
10
|
|
|
use Rx\Observable; |
|
11
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
|
12
|
|
|
use function React\Promise\resolve; |
|
13
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
|
14
|
|
|
|
|
15
|
|
|
final class RepositoriesHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var IteratePagesService |
|
19
|
|
|
*/ |
|
20
|
|
|
private $iteratePagesService; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Hydrator |
|
24
|
|
|
*/ |
|
25
|
|
|
private $hydrator; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param IteratePagesService $iteratePagesService |
|
29
|
|
|
* @param Hydrator $hydrator |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(IteratePagesService $iteratePagesService, Hydrator $hydrator) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->iteratePagesService = $iteratePagesService; |
|
34
|
|
|
$this->hydrator = $hydrator; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Fetch the given repository and hydrate it |
|
39
|
|
|
* |
|
40
|
|
|
* @param RepositoriesCommand $command |
|
41
|
|
|
* @return PromiseInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handle(RepositoriesCommand $command): PromiseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return resolve(unwrapObservableFromPromise( |
|
46
|
|
|
$this->iteratePagesService->handle('users/' . $command->getLogin() . '/repos') |
|
47
|
|
|
)->flatMap(function ($repositories) { |
|
48
|
|
|
return Observable::fromArray($repositories); |
|
49
|
|
|
})->map(function ($repository) { |
|
50
|
|
|
return $this->hydrator->hydrate(RepositoryInterface::HYDRATE_CLASS, $repository); |
|
51
|
|
|
})); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|