|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\Git\Service; |
|
4
|
|
|
|
|
5
|
|
|
use CommandBus\CommandBus\CommandBus; |
|
6
|
|
|
use CommandBus\QueryBus\QueryBus; |
|
7
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinderQuery; |
|
8
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse; |
|
9
|
|
|
use PhpGitHooks\Module\Git\Contract\Exception\InvalidPushException; |
|
10
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse; |
|
11
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse; |
|
12
|
|
|
use PhpGitHooks\Module\Git\Model\PrePushOriginalExecutorInterface; |
|
13
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
|
|
16
|
|
|
class PrePushTool |
|
17
|
|
|
{ |
|
18
|
|
|
const PRE_PUSH_HOOK = '<comment>Pre-push hook</comment>'; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var QueryBus |
|
21
|
|
|
*/ |
|
22
|
|
|
private $queryBus; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PrePushOriginalExecutorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $prePushOriginalExecutor; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var OutputInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $output; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var CommandBus |
|
33
|
|
|
*/ |
|
34
|
|
|
private $commandBus; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* PrePushTool constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param QueryBus $queryBus |
|
40
|
|
|
* @param PrePushOriginalExecutorInterface $prePushOriginalExecutor |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
* @param CommandBus $commandBus |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function __construct( |
|
45
|
|
|
QueryBus $queryBus, |
|
46
|
|
|
PrePushOriginalExecutorInterface $prePushOriginalExecutor, |
|
47
|
|
|
OutputInterface $output, |
|
48
|
|
|
CommandBus $commandBus |
|
49
|
|
|
) { |
|
50
|
3 |
|
$this->queryBus = $queryBus; |
|
51
|
3 |
|
$this->prePushOriginalExecutor = $prePushOriginalExecutor; |
|
52
|
3 |
|
$this->output = $output; |
|
53
|
3 |
|
$this->commandBus = $commandBus; |
|
54
|
3 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $remote |
|
58
|
|
|
* @param string $url |
|
59
|
|
|
* |
|
60
|
|
|
* @throws InvalidPushException |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function execute($remote, $url) |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var ConfigurationDataResponse $configurationData */ |
|
65
|
3 |
|
$configurationData = $this->queryBus->handle(new ConfigurationDataFinderQuery()); |
|
66
|
|
|
|
|
67
|
3 |
|
if (true === $configurationData->isPrePush()) { |
|
68
|
2 |
|
$this->output->writeln(self::PRE_PUSH_HOOK); |
|
69
|
2 |
|
$this->executeOriginalHook($remote, $url, $configurationData->getPrePushErrorMessage()); |
|
70
|
|
|
|
|
71
|
1 |
|
if (true === $configurationData->isPrePushPhpUnit()) { |
|
72
|
1 |
|
$this->commandBus->handle( |
|
73
|
1 |
|
new PhpUnitToolCommand( |
|
74
|
1 |
|
$configurationData->isPrePushPhpUnitRandom(), |
|
75
|
1 |
|
$configurationData->getPrePushPhpUnitOptions(), |
|
76
|
1 |
|
$configurationData->getPrePushErrorMessage() |
|
77
|
|
|
) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$this->output->writeln(GoodJobLogoResponse::paint($configurationData->getPrePushRightMessage())); |
|
82
|
|
|
} |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $remote |
|
87
|
|
|
* @param string $url |
|
88
|
|
|
* @param string $errorMessage |
|
89
|
|
|
* |
|
90
|
|
|
* @throws InvalidPushException |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private function executeOriginalHook($remote, $url, $errorMessage) |
|
93
|
|
|
{ |
|
94
|
2 |
|
$response = $this->prePushOriginalExecutor->execute($remote, $url); |
|
95
|
|
|
|
|
96
|
2 |
|
if (null != $response) { |
|
|
|
|
|
|
97
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
|
98
|
|
|
|
|
99
|
1 |
|
throw new InvalidPushException(); |
|
100
|
|
|
} |
|
101
|
1 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|