Completed
Push — master ( deb1d5...3324ab )
by Pablo
04:13
created

CommitMsgTool   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 67
rs 10
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A run() 0 18 3
A isValidCommitMessage() 0 4 2
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