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

RuleBook   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 48
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setRules() 0 5 1
A addRule() 0 5 1
A validate() 0 8 3
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