1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\Git\Service; |
4
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\QueryBus\QueryBus; |
6
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinderQuery; |
7
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse; |
8
|
|
|
use PhpGitHooks\Module\Git\Contract\Exception\InvalidCommitMessageException; |
9
|
|
|
use PhpGitHooks\Module\Git\Model\CommitMessageFinderInterface; |
10
|
|
|
use PhpGitHooks\Module\Git\Model\MergeValidatorInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
|
13
|
|
|
class CommitMsgTool |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var MergeValidatorInterface |
17
|
|
|
*/ |
18
|
|
|
private $mergeValidator; |
19
|
|
|
/** |
20
|
|
|
* @var QueryBus |
21
|
|
|
*/ |
22
|
|
|
private $queryBus; |
23
|
|
|
/** |
24
|
|
|
* @var CommitMessageFinderInterface |
25
|
|
|
*/ |
26
|
|
|
private $commitMessageFinder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* CommitMsgTool constructor. |
30
|
|
|
* |
31
|
|
|
* @param MergeValidatorInterface $mergeValidator |
32
|
|
|
* @param QueryBus $queryBus |
33
|
|
|
* @param CommitMessageFinderInterface $commitMessageFinder |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct( |
36
|
|
|
MergeValidatorInterface $mergeValidator, |
37
|
|
|
QueryBus $queryBus, |
38
|
|
|
CommitMessageFinderInterface $commitMessageFinder |
39
|
|
|
) { |
40
|
2 |
|
$this->mergeValidator = $mergeValidator; |
41
|
2 |
|
$this->queryBus = $queryBus; |
42
|
2 |
|
$this->commitMessageFinder = $commitMessageFinder; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param InputInterface $input |
47
|
|
|
* |
48
|
|
|
* @throws InvalidCommitMessageException |
49
|
|
|
*/ |
50
|
2 |
|
public function run(InputInterface $input) |
51
|
|
|
{ |
52
|
|
|
/** @var ConfigurationDataResponse $configurationDataResponse */ |
53
|
2 |
|
$configurationDataResponse = $this->queryBus->handle(new ConfigurationDataFinderQuery()); |
54
|
|
|
|
55
|
2 |
|
if (true === $configurationDataResponse->getCommitMsg()->isCommitMsg()) { |
56
|
1 |
|
$commitContent = $this->commitMessageFinder->find($input->getFirstArgument()); |
57
|
|
|
|
58
|
1 |
|
$validMessage = $this->isValidCommitMessage( |
59
|
1 |
|
$configurationDataResponse->getCommitMsg()->getRegularExpression(), |
60
|
|
|
$commitContent |
61
|
1 |
|
); |
62
|
|
|
|
63
|
1 |
|
if (false === $validMessage) { |
64
|
1 |
|
throw new InvalidCommitMessageException(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $regularExpression |
71
|
|
|
* @param string $commitMessage |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
1 |
|
private function isValidCommitMessage($regularExpression, $commitMessage) |
76
|
|
|
{ |
77
|
1 |
|
return $this->mergeValidator->isMerge() || preg_match(sprintf('/%s/', $regularExpression), $commitMessage); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|