|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository\Contents; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\Contents\FileUploadCommand; |
|
6
|
|
|
use ApiClients\Client\Github\Resource\Contents\FileOperationInterface; |
|
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\WritableStreamBase64Encode; |
|
15
|
|
|
use WyriHaximus\React\Stream\Json\JsonStream; |
|
16
|
|
|
|
|
17
|
|
|
final class FileUploadHandler |
|
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
|
3 |
|
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop) |
|
40
|
|
|
{ |
|
41
|
3 |
|
$this->requestService = $requestService; |
|
42
|
3 |
|
$this->hydrator = $hydrator; |
|
43
|
3 |
|
$this->loop = $loop; |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param FileUploadCommand $command |
|
48
|
|
|
* @return PromiseInterface |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function handle(FileUploadCommand $command): PromiseInterface |
|
51
|
|
|
{ |
|
52
|
3 |
|
$stream = new JsonStream(); |
|
53
|
|
|
|
|
54
|
3 |
|
$this->loop->futureTick(function () use ($stream, $command) { |
|
55
|
3 |
|
$fileStream = new ThroughStream(); |
|
56
|
3 |
|
$decoratedFileStream = new WritableStreamBase64Encode($fileStream); |
|
57
|
3 |
|
$stream->write('message', $command->getCommitMessage()); |
|
58
|
3 |
|
$stream->write('content', $fileStream); |
|
59
|
3 |
|
if ($command->getSha() !== '') { |
|
60
|
2 |
|
$stream->write('sha', $command->getSha()); |
|
61
|
|
|
} |
|
62
|
3 |
|
if ($command->getBranch() !== '') { |
|
63
|
2 |
|
$stream->write('branch', $command->getBranch()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
$stream->end(); |
|
67
|
|
|
|
|
68
|
3 |
|
$this->loop->futureTick(function () use ($decoratedFileStream, $command) { |
|
69
|
3 |
|
$command->getStream()->pipe($decoratedFileStream); |
|
70
|
3 |
|
}); |
|
71
|
3 |
|
}); |
|
72
|
|
|
|
|
73
|
3 |
|
return $this->requestService->request( |
|
74
|
3 |
|
new Request( |
|
75
|
3 |
|
'PUT', |
|
76
|
3 |
|
$command->getUrl(), |
|
77
|
3 |
|
[], |
|
78
|
3 |
|
new ReadableBodyStream($stream) |
|
79
|
|
|
) |
|
80
|
3 |
|
)->then(function ($operation) { |
|
81
|
3 |
|
return $this->hydrator->hydrate( |
|
82
|
3 |
|
FileOperationInterface::HYDRATE_CLASS, |
|
83
|
3 |
|
$operation->getBody()->getParsedContents() |
|
84
|
|
|
); |
|
85
|
3 |
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|