|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\Git\Contract\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface; |
|
6
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandInterface; |
|
7
|
|
|
use Bruli\EventBusBundle\QueryBus\QueryBus; |
|
8
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinder; |
|
9
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse; |
|
10
|
|
|
use PhpGitHooks\Module\Git\Contract\Exception\InvalidCommitMessageException; |
|
11
|
|
|
use PhpGitHooks\Module\Git\Model\CommitMessageFinderInterface; |
|
12
|
|
|
use PhpGitHooks\Module\Git\Model\MergeValidatorInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
|
|
15
|
|
|
class CommitMsgHandler implements CommandHandlerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var MergeValidatorInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $mergeValidator; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var QueryBus |
|
23
|
|
|
*/ |
|
24
|
|
|
private $queryBus; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var CommitMessageFinderInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $commitMessageFinder; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* CommitMsgTool constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param MergeValidatorInterface $mergeValidator |
|
34
|
|
|
* @param QueryBus $queryBus |
|
35
|
|
|
* @param CommitMessageFinderInterface $commitMessageFinder |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __construct( |
|
38
|
|
|
MergeValidatorInterface $mergeValidator, |
|
39
|
|
|
QueryBus $queryBus, |
|
40
|
|
|
CommitMessageFinderInterface $commitMessageFinder |
|
41
|
|
|
) { |
|
42
|
2 |
|
$this->mergeValidator = $mergeValidator; |
|
43
|
2 |
|
$this->queryBus = $queryBus; |
|
44
|
2 |
|
$this->commitMessageFinder = $commitMessageFinder; |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param InputInterface $input |
|
49
|
|
|
* |
|
50
|
|
|
* @throws InvalidCommitMessageException |
|
51
|
|
|
*/ |
|
52
|
2 |
|
private function run(InputInterface $input) |
|
53
|
|
|
{ |
|
54
|
|
|
/** @var ConfigurationDataResponse $configurationDataResponse */ |
|
55
|
2 |
|
$configurationDataResponse = $this->queryBus->handle(new ConfigurationDataFinder()); |
|
56
|
|
|
|
|
57
|
2 |
|
if (true === $configurationDataResponse->getCommitMsg()->isCommitMsg()) { |
|
58
|
1 |
|
$commitContent = $this->commitMessageFinder->find($input->getFirstArgument()); |
|
59
|
|
|
|
|
60
|
1 |
|
$regularExpression = $configurationDataResponse->getCommitMsg()->getRegularExpression(); |
|
61
|
1 |
|
$validMessage = $this->isValidCommitMessage($regularExpression, $commitContent); |
|
62
|
|
|
|
|
63
|
1 |
|
if (false === $validMessage) { |
|
64
|
1 |
|
throw new InvalidCommitMessageException( |
|
65
|
1 |
|
sprintf( |
|
66
|
1 |
|
'Invalid Commit message: commit message does not match regex %s !', |
|
67
|
1 |
|
$regularExpression |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $regularExpression |
|
76
|
|
|
* @param string $commitMessage |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
1 |
|
private function isValidCommitMessage($regularExpression, $commitMessage) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->mergeValidator->isMerge() || preg_match(sprintf('/%s/', $regularExpression), $commitMessage); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param CommandInterface|CommitMsg $command |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function handle(CommandInterface $command) |
|
89
|
|
|
{ |
|
90
|
2 |
|
$this->run($command->getInput()); |
|
|
|
|
|
|
91
|
1 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: