Completed
Push — master ( 6f6152...8668e6 )
by Sebastian
07:34
created

Regex   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 13 2
A getRegex() 0 8 2
A getErrorMessage() 0 4 1
A getSuccessMessage() 0 4 1
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\CaptainHook\Hook\Message\Action;
11
12
use SebastianFeldmann\CaptainHook\Config;
13
use SebastianFeldmann\CaptainHook\Console\IO;
14
use SebastianFeldmann\CaptainHook\Exception\ActionFailed;
15
use SebastianFeldmann\CaptainHook\Hook\Action;
16
use SebastianFeldmann\Git\Repository;
17
18
/**
19
 * Class Regex
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/sebastianfeldmann/captainhook
24
 * @since   Class available since Release 1.0.0
25
 */
26
class Regex implements Action
27
{
28
    /**
29
     * Execute the configured action.
30
     *
31
     * @param  \SebastianFeldmann\CaptainHook\Config         $config
32
     * @param  \SebastianFeldmann\CaptainHook\Console\IO     $io
33
     * @param  \SebastianFeldmann\Git\Repository             $repository
34
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
35
     * @throws \Exception
36
     */
37 4
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
38
    {
39 4
        $regex      = $this->getRegex($action->getOptions());
40 3
        $errorMsg   = $this->getErrorMessage($action->getOptions());
41 3
        $successMsg = $this->getSuccessMessage($action->getOptions());
42 3
        $matches    = [];
43
44 3
        if (!preg_match($regex, $repository->getCommitMsg()->getContent(), $matches)) {
45 1
            throw ActionFailed::withMessage(sprintf($errorMsg, $regex));
46
        }
47
48 2
        $io->write(sprintf($successMsg, $matches[0]));
49 2
    }
50
51
    /**
52
     * Extract regex from options array.
53
     *
54
     * @param  \SebastianFeldmann\CaptainHook\Config\Options $options
55
     * @return string
56
     * @throws \SebastianFeldmann\CaptainHook\Exception\ActionFailed
57
     */
58 4
    protected function getRegex(Config\Options $options)
59
    {
60 4
        $regex = $options->get('regex');
61 4
        if (empty($regex)) {
62 1
            throw ActionFailed::withMessage('No regex option');
63
        }
64 3
        return $regex;
65
    }
66
67
    /**
68
     * Determine the error message to use.
69
     *
70
     * @param  \SebastianFeldmann\CaptainHook\Config\Options $options
71
     * @return string
72
     */
73 3
    protected function getErrorMessage(Config\Options $options)
74
    {
75 3
        return $options->get('error') ?? 'Commit message did not match regex: %s';
76
    }
77
78
    /**
79
     * Determine the error message to use.
80
     *
81
     * @param  \SebastianFeldmann\CaptainHook\Config\Options $options
82
     * @return string
83
     */
84 3
    protected function getSuccessMessage(Config\Options $options)
85
    {
86 3
        return $options->get('success') ?? 'Found matching pattern: %s';
87
    }
88
}
89