1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\BlobCommand; |
6
|
|
|
use ApiClients\Client\Github\Resource\TreeInterface; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
8
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
9
|
|
|
use Clue\React\Buzz\Message\ReadableBodyStream; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\Promise\PromiseInterface; |
12
|
|
|
use React\Stream\ThroughStream; |
13
|
|
|
use RingCentral\Psr7\Request; |
14
|
|
|
use WyriHaximus\React\Stream\Base64\ReadableStreamBase64Encode; |
15
|
|
|
use WyriHaximus\React\Stream\Json\JsonStream; |
16
|
|
|
|
17
|
|
|
final class BlobHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var RequestService |
21
|
|
|
*/ |
22
|
|
|
private $requestService; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Hydrator |
26
|
|
|
*/ |
27
|
|
|
private $hydrator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var LoopInterface |
31
|
|
|
*/ |
32
|
|
|
private $loop; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param RequestService $requestService |
36
|
|
|
* @param Hydrator $hydrator |
37
|
|
|
* @param LoopInterface $loop |
38
|
|
|
*/ |
39
|
|
|
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop) |
40
|
|
|
{ |
41
|
|
|
$this->requestService = $requestService; |
42
|
|
|
$this->hydrator = $hydrator; |
43
|
|
|
$this->loop = $loop; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param BlobCommand $command |
48
|
|
|
* @return PromiseInterface |
49
|
|
|
*/ |
50
|
|
|
public function handle(BlobCommand $command): PromiseInterface |
51
|
|
|
{ |
52
|
|
|
$stream = new JsonStream(); |
53
|
|
|
$this->loop->futureTick(function () use ($stream, $command) { |
54
|
|
|
$contentsStream = new ThroughStream(); |
55
|
|
|
$stream->write('encoding', 'base64'); |
56
|
|
|
$stream->write('content', new ReadableStreamBase64Encode($contentsStream)); |
57
|
|
|
$stream->end(); |
58
|
|
|
|
59
|
|
|
$this->loop->futureTick(function () use ($contentsStream, $command) { |
60
|
|
|
$command->getContents()->pipe($contentsStream); |
61
|
|
|
}); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
return $this->requestService->request( |
65
|
|
|
new Request( |
66
|
|
|
'POST', |
67
|
|
|
'repos/' . $command->getRepository() . '/git/blobs', |
68
|
|
|
[], |
69
|
|
|
new ReadableBodyStream($stream) |
70
|
|
|
) |
71
|
|
|
)->then(function ($tree) { |
72
|
|
|
return $this->hydrator->hydrate(TreeInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents()); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|