EnsureNaming   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 74
ccs 21
cts 21
cp 1
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessage() 0 4 2
A getRegex() 0 7 2
A execute() 0 12 2
A getRestriction() 0 3 1
A getSuccessMessage() 0 4 2
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Branch\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Exception\ActionFailed;
17
use CaptainHook\App\Hook\Action;
18
use CaptainHook\App\Hook\Restriction;
19
use CaptainHook\App\Hooks;
20
use SebastianFeldmann\Git\Repository;
21
22
/**
23
 * Class EnsureNaming
24
 *
25
 * This Action makes sure you are on a branch that follows your naming conventions.
26
 *
27
 * Example configuration:
28
 * <code>
29
 * {
30
 *   "action": "CaptainHook.Branch.EnsureNaming",
31
 *   "options": {
32
 *     "regex": "#feature/issue[0-9]+-.*#i",
33
 *     "error": "Arr matey! Ye be on the wrong branch!",
34
 *     "success": "All clear, Captain! Full speed ahead!",
35
 *   }
36
 * }
37
 * </code>
38
 *
39
 * @package CaptainHook
40
 * @author  Felix Edelmann <[email protected]>
41
 * @link    https://github.com/captainhook-git/captainhook
42
 * @since   Class available since Release 5.4.0
43
 */
44
class EnsureNaming implements Action
45
{
46
    /**
47
     * Return hook restriction
48
     *
49
     * @return \CaptainHook\App\Hook\Restriction
50
     */
51 1
    public static function getRestriction(): Restriction
52
    {
53 1
        return Restriction::fromArray([Hooks::PRE_COMMIT, Hooks::PRE_PUSH, Hooks::POST_CHECKOUT]);
54
    }
55
56
    /**
57
     * Execute the configured action
58
     *
59
     * @param  \CaptainHook\App\Config           $config
60
     * @param  \CaptainHook\App\Console\IO       $io
61
     * @param  \SebastianFeldmann\Git\Repository $repository
62
     * @param  \CaptainHook\App\Config\Action    $action
63
     * @return void
64
     * @throws \Exception
65
     */
66 5
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
67
    {
68 5
        $regex      = $this->getRegex($action->getOptions());
69 4
        $errorMsg   = $this->getErrorMessage($action->getOptions());
70 4
        $successMsg = $this->getSuccessMessage($action->getOptions());
71
72 4
        $branch = $repository->getInfoOperator()->getCurrentBranch();
73 4
        if (!preg_match($regex, $branch)) {
74 2
            throw new ActionFailed(sprintf($errorMsg, $regex));
75
        }
76
77 2
        $io->write(['', '', sprintf($successMsg, $regex), ''], true, IO::VERBOSE);
78
    }
79
80
    /**
81
     * Extract regex from options array
82
     *
83
     * @param  \CaptainHook\App\Config\Options $options
84
     * @return string
85
     * @throws \CaptainHook\App\Exception\ActionFailed
86
     */
87 5
    protected function getRegex(Config\Options $options): string
88
    {
89 5
        $regex = $options->get('regex', '');
90 5
        if (empty($regex)) {
91 1
            throw new ActionFailed('No regex option');
92
        }
93 4
        return $regex;
94
    }
95
96
    /**
97
     * Determine the error message to use
98
     *
99
     * @param  \CaptainHook\App\Config\Options $options
100
     * @return string
101
     */
102 4
    protected function getErrorMessage(Config\Options $options): string
103
    {
104 4
        $msg = $options->get('error', '');
105 4
        return !empty($msg) ? $msg : '<error>FAIL</error> Branch name does not match regex: %s';
106
    }
107
108
    /**
109
     * Determine the error message to use
110
     *
111
     * @param  \CaptainHook\App\Config\Options $options
112
     * @return string
113
     */
114 4
    protected function getSuccessMessage(Config\Options $options): string
115
    {
116 4
        $msg = $options->get('success', '');
117 4
        return !empty($msg) ? $msg : '<info>OK</info> Branch name does match regex: %s';
118
    }
119
}
120