1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository\Commit; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\Commit\CreateStatusCommand; |
6
|
|
|
use ApiClients\Client\Github\Resource\Repository\Commit\StatusInterface; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
8
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
9
|
|
|
use ApiClients\Middleware\Json\JsonStream; |
10
|
|
|
use React\Promise\PromiseInterface; |
11
|
|
|
use RingCentral\Psr7\Request; |
12
|
|
|
|
13
|
|
|
final class CreateStatusHandler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var RequestService |
17
|
|
|
*/ |
18
|
|
|
private $requestService; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Hydrator |
22
|
|
|
*/ |
23
|
|
|
private $hydrator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param RequestService $requestService |
27
|
|
|
* @param Hydrator $hydrator |
28
|
|
|
*/ |
29
|
|
|
public function __construct(RequestService $requestService, Hydrator $hydrator) |
30
|
|
|
{ |
31
|
|
|
$this->requestService = $requestService; |
32
|
|
|
$this->hydrator = $hydrator; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param CreateStatusCommand $command |
37
|
|
|
* @return PromiseInterface |
38
|
|
|
*/ |
39
|
|
|
public function handle(CreateStatusCommand $command): PromiseInterface |
40
|
|
|
{ |
41
|
|
|
$json = [ |
42
|
|
|
'state' => $command->getState(), |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$targetUrl = $command->getTargetUrl(); |
46
|
|
|
if ($targetUrl !== null) { |
47
|
|
|
$json['target_url'] = $targetUrl; |
48
|
|
|
} |
49
|
|
|
$description = $command->getDescription(); |
50
|
|
|
if ($description !== null) { |
51
|
|
|
$json['description'] = $description; |
52
|
|
|
} |
53
|
|
|
$context = $command->getContext(); |
54
|
|
|
if ($context !== null) { |
55
|
|
|
$json['context'] = $context; |
56
|
|
|
} |
57
|
|
|
unset($targetUrl, $description, $context); |
58
|
|
|
|
59
|
|
|
return $this->requestService->request( |
60
|
|
|
new Request( |
61
|
|
|
'POST', |
62
|
|
|
\str_replace('/commits/', '/statuses/', $command->getCommit()->url()), |
63
|
|
|
[], |
64
|
|
|
new JsonStream($json) |
65
|
|
|
) |
66
|
|
|
)->then(function ($status) { |
67
|
|
|
return $this->hydrator->hydrate(StatusInterface::HYDRATE_CLASS, $status->getBody()->getParsedContents()); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|