1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand; |
6
|
|
|
use ApiClients\Client\Github\Resource\Git\CommitInterface; |
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
|
|
View Code Duplication |
final class CommitHandler |
|
|
|
|
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
|
|
|
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop) |
37
|
|
|
{ |
38
|
|
|
$this->requestService = $requestService; |
39
|
|
|
$this->hydrator = $hydrator; |
40
|
|
|
$this->loop = $loop; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param CommitCommand $command |
45
|
|
|
* @return PromiseInterface |
46
|
|
|
*/ |
47
|
|
|
public function handle(CommitCommand $command): PromiseInterface |
48
|
|
|
{ |
49
|
|
|
return $this->requestService->request( |
50
|
|
|
new Request( |
51
|
|
|
'POST', |
52
|
|
|
'repos/' . $command->getRepository() . '/git/commits', |
53
|
|
|
[], |
54
|
|
|
new JsonStream([ |
55
|
|
|
'message' => $command->getMessage(), |
56
|
|
|
'tree' => $command->getTree(), |
57
|
|
|
'parents' => $command->getCommit(), |
58
|
|
|
]) |
59
|
|
|
) |
60
|
|
|
)->then(function ($tree) { |
61
|
|
|
return $this->hydrator->hydrate(CommitInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents()); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.