Completed
Push — master ( 403335...b6b12e )
by Sebastian
03:08
created

RuleBook::setRules()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
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\ActionExecution;
13
use sebastianfeldmann\CaptainHook\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 5
    public function setRules(array $rules)
39
    {
40 5
        $this->rules = $rules;
41 5
        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\CaptainHook\Git\CommitMessage $msg
60
     * @throws \sebastianfeldmann\CaptainHook\Exception\ActionExecution
61
     */
62 8
    public function validate(CommitMessage $msg)
63
    {
64 8
        foreach ($this->rules as $rule) {
65 6
            if (!$rule->pass($msg)) {
66 6
                throw new ActionExecution($rule->getHint());
67
            }
68
        }
69 5
    }
70
}
71