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\FileDeleteCommand; |
6
|
|
|
use ApiClients\Client\Github\Resource\Contents\FileOperationInterface; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
8
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
9
|
|
|
use ApiClients\Middleware\Json\JsonStream; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\Promise\PromiseInterface; |
12
|
|
|
use RingCentral\Psr7\Request; |
13
|
|
|
|
14
|
|
|
final class FileDeleteHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var RequestService |
18
|
|
|
*/ |
19
|
|
|
private $requestService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Hydrator |
23
|
|
|
*/ |
24
|
|
|
private $hydrator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var LoopInterface |
28
|
|
|
*/ |
29
|
|
|
private $loop; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param RequestService $requestService |
33
|
|
|
* @param Hydrator $hydrator |
34
|
|
|
* @param LoopInterface $loop |
35
|
|
|
*/ |
36
|
3 |
|
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop) |
37
|
|
|
{ |
38
|
3 |
|
$this->requestService = $requestService; |
39
|
3 |
|
$this->hydrator = $hydrator; |
40
|
3 |
|
$this->loop = $loop; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param FileDeleteCommand $command |
45
|
|
|
* @return PromiseInterface |
46
|
|
|
*/ |
47
|
3 |
|
public function handle(FileDeleteCommand $command): PromiseInterface |
48
|
|
|
{ |
49
|
|
|
$json = [ |
50
|
3 |
|
'message' => $command->getCommitMessage(), |
51
|
3 |
|
'sha' => $command->getSha(), |
52
|
|
|
]; |
53
|
|
|
|
54
|
3 |
|
if ($command->getBranch() !== '') { |
55
|
1 |
|
$json['branch'] = $command->getBranch(); |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
return $this->requestService->request( |
59
|
3 |
|
new Request( |
60
|
3 |
|
'DELETE', |
61
|
3 |
|
$command->getUrl(), |
62
|
3 |
|
[], |
63
|
3 |
|
new JsonStream($json) |
64
|
|
|
) |
65
|
3 |
|
)->then(function ($operation) { |
66
|
3 |
|
return $this->hydrator->hydrate( |
67
|
3 |
|
FileOperationInterface::HYDRATE_CLASS, |
68
|
3 |
|
$operation->getBody()->getParsedContents() |
69
|
|
|
); |
70
|
3 |
|
}); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|