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