|
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 function ApiClients\Tools\Rx\observableFromArray; |
|
11
|
|
|
use function React\Promise\resolve; |
|
12
|
|
|
use RingCentral\Psr7\Request; |
|
13
|
|
|
use Rx\React\Promise; |
|
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
|
3 |
|
public function __construct(RequestService $requestService, Hydrator $hydrator) |
|
33
|
|
|
{ |
|
34
|
3 |
|
$this->requestService = $requestService; |
|
35
|
3 |
|
$this->hydrator = $hydrator; |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
public function handle(ContentsCommand $command) |
|
39
|
|
|
{ |
|
40
|
3 |
|
$path = \ltrim($command->getPath(), '/'); |
|
41
|
3 |
|
$uri = 'repos/' . $command->getFullname() . '/contents/' . $path; |
|
42
|
|
|
|
|
43
|
3 |
|
return resolve( |
|
44
|
3 |
|
Promise::toObservable( |
|
45
|
3 |
|
$this->requestService->request( |
|
46
|
3 |
|
new Request('GET', $uri) |
|
47
|
|
|
) |
|
48
|
3 |
|
)->flatMap(function ($contents) { |
|
49
|
3 |
|
$parsedContents = $contents->getBody()->getParsedContents(); |
|
50
|
|
|
if (isset($parsedContents['type'])) { |
|
51
|
3 |
|
return observableFromArray([$parsedContents]); |
|
52
|
3 |
|
} |
|
53
|
3 |
|
|
|
54
|
3 |
|
return observableFromArray($contents->getBody()->getParsedContents()); |
|
55
|
3 |
|
})->map(function ($content) use ($command) { |
|
56
|
|
|
$content['repository_fullname'] = $command->getFullname(); |
|
57
|
|
|
if ($content['type'] === 'file') { |
|
58
|
|
|
return $this->hydrator->hydrate( |
|
59
|
3 |
|
FileInterface::HYDRATE_CLASS, |
|
60
|
3 |
|
$content |
|
61
|
3 |
|
); |
|
62
|
|
|
} |
|
63
|
3 |
|
|
|
64
|
|
|
return $this->hydrator->hydrate( |
|
65
|
|
|
DirectoryInterface::HYDRATE_CLASS, |
|
66
|
|
|
$content |
|
67
|
|
|
); |
|
68
|
|
|
}) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|