Completed
Push — master ( 549cd1...a8a22b )
by Pablo
04:00
created

CommitMsgTool::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
ccs 5
cts 5
cp 1
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
namespace PhpGitHooks\Module\Git\Service;
4
5
use PhpGitHooks\Infrastructure\CommandBus\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
            );
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