1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\ContentsCommand; |
6
|
|
|
use ApiClients\Client\Github\CommandBus\Command\UserCommand; |
7
|
|
|
use ApiClients\Client\Github\Resource\Contents\DirectoryInterface; |
8
|
|
|
use ApiClients\Client\Github\Resource\Contents\FileInterface; |
9
|
|
|
use ApiClients\Client\Github\Resource\UserInterface; |
10
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
11
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
12
|
|
|
use ApiClients\Tools\Services\Client\FetchAndHydrateService; |
13
|
|
|
use React\Promise\PromiseInterface; |
14
|
|
|
use function React\Promise\resolve; |
15
|
|
|
use RingCentral\Psr7\Request; |
16
|
|
|
use Rx\Observable; |
17
|
|
|
use Rx\React\Promise; |
18
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
19
|
|
|
|
20
|
|
|
final class ContentsHandler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RequestService |
24
|
|
|
*/ |
25
|
|
|
private $requestService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Hydrator |
29
|
|
|
*/ |
30
|
|
|
private $hydrator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ContentsHandler constructor. |
34
|
|
|
* @param RequestService $requestService |
35
|
|
|
* @param Hydrator $hydrator |
36
|
|
|
*/ |
37
|
|
|
public function __construct(RequestService $requestService, Hydrator $hydrator) |
38
|
|
|
{ |
39
|
|
|
$this->requestService = $requestService; |
40
|
|
|
$this->hydrator = $hydrator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function handle(ContentsCommand $command) |
44
|
|
|
{ |
45
|
|
|
return resolve(Promise::toObservable($this->requestService->handle( |
46
|
|
|
new Request('GET', 'repos/' . $command->getFullname() . '/contents/') |
47
|
|
|
))->flatMap(function ($contents) { |
48
|
|
|
return Observable::fromArray($contents->getBody()->getJson()); |
49
|
|
|
})->map(function ($content) { |
50
|
|
|
if ($content['type'] === 'file') { |
51
|
|
|
return $this->hydrator->hydrate( |
52
|
|
|
FileInterface::HYDRATE_CLASS, |
53
|
|
|
$content |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->hydrator->hydrate( |
58
|
|
|
DirectoryInterface::HYDRATE_CLASS, |
59
|
|
|
$content |
60
|
|
|
); |
61
|
|
|
})); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|