1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\Git\Contract\Command; |
4
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandBus; |
6
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface; |
7
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandInterface; |
8
|
|
|
use Bruli\EventBusBundle\QueryBus\QueryBus; |
9
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinder; |
10
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse; |
11
|
|
|
use PhpGitHooks\Module\Git\Contract\Exception\InvalidPushException; |
12
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse; |
13
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse; |
14
|
|
|
use PhpGitHooks\Module\Git\Model\PrePushOriginalExecutorInterface; |
15
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageCommand; |
16
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand; |
17
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class PrePushToolHandler implements CommandHandlerInterface |
21
|
|
|
{ |
22
|
|
|
const PRE_PUSH_HOOK = '<comment>Pre-push hook</comment>'; |
23
|
|
|
/** |
24
|
|
|
* @var QueryBus |
25
|
|
|
*/ |
26
|
|
|
private $queryBus; |
27
|
|
|
/** |
28
|
|
|
* @var PrePushOriginalExecutorInterface |
29
|
|
|
*/ |
30
|
|
|
private $prePushOriginalExecutor; |
31
|
|
|
/** |
32
|
|
|
* @var OutputInterface |
33
|
|
|
*/ |
34
|
|
|
private $output; |
35
|
|
|
/** |
36
|
|
|
* @var CommandBus |
37
|
|
|
*/ |
38
|
|
|
private $commandBus; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* PrePushTool constructor. |
42
|
|
|
* |
43
|
|
|
* @param QueryBus $queryBus |
44
|
|
|
* @param PrePushOriginalExecutorInterface $prePushOriginalExecutor |
45
|
|
|
* @param OutputInterface $output |
46
|
|
|
* @param CommandBus $commandBus |
47
|
|
|
*/ |
48
|
3 |
|
public function __construct( |
49
|
|
|
QueryBus $queryBus, |
50
|
|
|
PrePushOriginalExecutorInterface $prePushOriginalExecutor, |
51
|
|
|
OutputInterface $output, |
52
|
|
|
CommandBus $commandBus |
53
|
|
|
) { |
54
|
3 |
|
$this->queryBus = $queryBus; |
55
|
3 |
|
$this->prePushOriginalExecutor = $prePushOriginalExecutor; |
56
|
3 |
|
$this->output = $output; |
57
|
3 |
|
$this->commandBus = $commandBus; |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $remote |
62
|
|
|
* @param string $url |
63
|
|
|
* |
64
|
|
|
* @throws InvalidPushException |
65
|
|
|
*/ |
66
|
3 |
|
private function execute($remote, $url) |
67
|
|
|
{ |
68
|
|
|
/** @var ConfigurationDataResponse $configurationData */ |
69
|
3 |
|
$configurationData = $this->queryBus->handle(new ConfigurationDataFinder()); |
70
|
3 |
|
$prePushResponse = $configurationData->getPrePush(); |
71
|
|
|
|
72
|
3 |
|
if (true === $prePushResponse->isPrePush()) { |
73
|
2 |
|
$this->output->writeln(self::PRE_PUSH_HOOK); |
74
|
2 |
|
$this->executeOriginalHook($remote, $url, $prePushResponse->getErrorMessage()); |
75
|
|
|
|
76
|
1 |
|
$phpunitResponse = $prePushResponse->getPhpUnit(); |
77
|
|
|
|
78
|
1 |
View Code Duplication |
if (true === $phpunitResponse->isPhpunit()) { |
|
|
|
|
79
|
1 |
|
$this->commandBus->handle( |
80
|
1 |
|
new PhpUnitToolCommand( |
81
|
1 |
|
$phpunitResponse->isPhpunitRandomMode(), |
82
|
1 |
|
$phpunitResponse->getPhpunitOptions(), |
83
|
1 |
|
$prePushResponse->getErrorMessage() |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
1 |
|
$phpunitStrictCoverageResponse = $prePushResponse->getPhpUnitStrictCoverage(); |
88
|
|
|
|
89
|
1 |
|
if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) { |
90
|
1 |
|
$this->commandBus->handle( |
91
|
1 |
|
new StrictCoverageCommand( |
92
|
1 |
|
$phpunitStrictCoverageResponse->getMinimum(), |
93
|
1 |
|
$prePushResponse->getErrorMessage() |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$phpunitGuardCoverageResponse = $prePushResponse->getPhpUnitGuardCoverage(); |
99
|
|
|
|
100
|
1 |
|
if (true === $phpunitGuardCoverageResponse->isEnabled()) { |
101
|
1 |
|
$this->commandBus->handle( |
102
|
1 |
|
new GuardCoverageCommand($phpunitGuardCoverageResponse->getWarningMessage()) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$this->output->writeln(GoodJobLogoResponse::paint($prePushResponse->getRightMessage())); |
108
|
|
|
} |
109
|
2 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $remote |
113
|
|
|
* @param string $url |
114
|
|
|
* @param string $errorMessage |
115
|
|
|
* |
116
|
|
|
* @throws InvalidPushException |
117
|
|
|
*/ |
118
|
2 |
|
private function executeOriginalHook($remote, $url, $errorMessage) |
119
|
|
|
{ |
120
|
2 |
|
$response = $this->prePushOriginalExecutor->execute($remote, $url); |
121
|
|
|
|
122
|
2 |
|
if (null != $response) { |
|
|
|
|
123
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
124
|
|
|
|
125
|
1 |
|
throw new InvalidPushException(); |
126
|
|
|
} |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param CommandInterface|PrePushTool $command |
131
|
|
|
*/ |
132
|
3 |
|
public function handle(CommandInterface $command) |
133
|
|
|
{ |
134
|
3 |
|
$this->execute($command->getRemote(), $command->getUrl()); |
|
|
|
|
135
|
2 |
|
} |
136
|
|
|
} |
137
|
|
|
|
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.