Completed
Push — master ( 16d393...b8b112 )
by Sebastian
04:40
created

RuleBook::addRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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;
11
12
use SebastianFeldmann\CaptainHook\Exception\ActionFailed;
13
use SebastianFeldmann\Git\CommitMessage;
14
15
/**
16
 * Class RuleBook
17
 *
18
 * @package CaptainHook
19
 * @author  Sebastian Feldmann <[email protected]>
20
 * @link    https://github.com/sebastianfeldmann/captainhook
21
 * @since   Class available since Release 0.9.0
22
 */
23
class RuleBook
24
{
25
    /**
26
     * List of rules to check
27
     *
28
     * @var \SebastianFeldmann\CaptainHook\Hook\Message\Rule[]
29
     */
30
    private $rules = [];
31
32
    /**
33
     * Set rules to check.
34
     *
35
     * @param  \SebastianFeldmann\CaptainHook\Hook\Message\Rule[] $rules
36
     * @return \SebastianFeldmann\CaptainHook\Hook\Message\Rulebook
37
     */
38 6
    public function setRules(array $rules)
39
    {
40 6
        $this->rules = $rules;
41 6
        return $this;
42
    }
43
44
    /**
45
     * Add a rule to the list.
46
     *
47
     * @param  \SebastianFeldmann\CaptainHook\Hook\Message\Rule $rule
48
     * @return \SebastianFeldmann\CaptainHook\Hook\Message\Rulebook
49
     */
50 2
    public function addRule(Rule $rule)
51
    {
52 2
        $this->rules[] = $rule;
53 2
        return $this;
54
    }
55
56
    /**
57
     * Validates all rules.
58
     *
59
     * @param  \SebastianFeldmann\Git\CommitMessage $msg
60
     * @throws \SebastianFeldmann\CaptainHook\Exception\ActionFailed
61
     */
62 9
    public function validate(CommitMessage $msg)
63
    {
64 9
        $problems = [];
65
        // collect problems for all rules
66 9
        foreach ($this->rules as $rule) {
67 7
            if (!$rule->pass($msg)) {
68 7
                $problems[] = $rule->getHint();
69
            }
70
        }
71
72
        // any problems found?
73 9
        if (count($problems) > 0) {
74 4
            throw ActionFailed::withMessage($this->getOutput($problems));
75
        }
76 5
    }
77
78
    /**
79
     * Format the error output.
80
     *
81
     * @param  array $problems
82
     * @return string
83
     */
84 4
    private function getOutput(array $problems) : string
85
    {
86 4
        $output = 'RULEBOOK' . PHP_EOL .
87 4
                  '---------------------------------------------------------------------------' . PHP_EOL .
88 4
                  'CAPTAINHOOK FOUND ' . count($problems) . ' PROBLEMS(S) WITH YOUR COMMIT MESSAGE' . PHP_EOL .
89 4
                  '---------------------------------------------------------------------------' . PHP_EOL;
90
91 4
        foreach ($problems as $problem) {
92 4
            $output .= '  ' . $this->formatProblem($problem) . PHP_EOL;
93
        }
94
95 4
        $output .= '---------------------------------------------------------------------------' . PHP_EOL;
96
97 4
        return $output;
98
    }
99
100
    /**
101
     * Indent multi line problems so the lines after the first one are indented for better readability.
102
     *
103
     * @param  string $problem
104
     * @return string
105
     */
106 4
    private function formatProblem(string $problem) : string
107
    {
108 4
        $lines  = explode(PHP_EOL, $problem);
109 4
        $amount = count($lines);
110
111 4
        for ($i = 1; $i < $amount; $i++) {
112 1
            $lines[$i] = '    ' . $lines[$i];
113
        }
114
115 4
        return implode(PHP_EOL, $lines);
116
117
118
    }
119
}
120