1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\TreeCommand; |
6
|
|
|
use ApiClients\Client\Github\Resource\Git\TreeInterface; |
7
|
|
|
use ApiClients\Client\Github\VO\NamedBlob; |
8
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
9
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
10
|
|
|
use Clue\React\Buzz\Message\ReadableBodyStream; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use React\Promise\PromiseInterface; |
13
|
|
|
use RingCentral\Psr7\Request; |
14
|
|
|
use WyriHaximus\React\Stream\Json\JsonStream; |
15
|
|
|
|
16
|
|
|
final class TreeHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RequestService |
20
|
|
|
*/ |
21
|
|
|
private $requestService; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Hydrator |
25
|
|
|
*/ |
26
|
|
|
private $hydrator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var LoopInterface |
30
|
|
|
*/ |
31
|
|
|
private $loop; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RequestService $requestService |
35
|
|
|
* @param Hydrator $hydrator |
36
|
|
|
* @param LoopInterface $loop |
37
|
|
|
*/ |
38
|
|
|
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop) |
39
|
|
|
{ |
40
|
|
|
$this->requestService = $requestService; |
41
|
|
|
$this->hydrator = $hydrator; |
42
|
|
|
$this->loop = $loop; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param TreeCommand $command |
47
|
|
|
* @return PromiseInterface |
48
|
|
|
*/ |
49
|
|
|
public function handle(TreeCommand $command): PromiseInterface |
50
|
|
|
{ |
51
|
|
|
$stream = new JsonStream(); |
52
|
|
|
$this->loop->futureTick(function () use ($stream, $command) { |
53
|
|
|
if ($command->getBaseTree() !== null) { |
54
|
|
|
$stream->write('base_tree', $command->getBaseTree()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$trees = JsonStream::createArray(); |
58
|
|
|
$stream->write('tree', $trees); |
59
|
|
|
$stream->end(); |
60
|
|
|
|
61
|
|
|
$this->loop->futureTick(function () use ($trees, $command) { |
62
|
|
|
/** @var NamedBlob $blob */ |
63
|
|
|
foreach ($command->getBlobs() as $blob) { |
64
|
|
|
$node = [ |
65
|
|
|
'path' => $blob->getPath(), |
66
|
|
|
'mode' => $blob->getMode(), |
67
|
|
|
'type' => $blob->getType(), |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
if ($blob->getSha() !== null) { |
71
|
|
|
$node['sha'] = $blob->getSha(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($blob->getContent() !== null) { |
75
|
|
|
$node['content'] = $blob->getContent(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$trees->writeValue($node); |
79
|
|
|
} |
80
|
|
|
$trees->close(); |
81
|
|
|
}); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
return $this->requestService->request( |
85
|
|
|
new Request( |
86
|
|
|
'POST', |
87
|
|
|
'repos/' . $command->getRepository() . '/git/trees', |
88
|
|
|
[], |
89
|
|
|
new ReadableBodyStream($stream) |
90
|
|
|
) |
91
|
|
|
)->then(function ($tree) { |
92
|
|
|
return $this->hydrator->hydrate(TreeInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents()); |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|