Passed
Push — master ( b34030...56c38f )
by Sebastian
03:50
created

Rulebook::createRule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 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\Config;
13
use sebastianfeldmann\CaptainHook\Console\IO;
14
use sebastianfeldmann\CaptainHook\Git\Repository;
15
16
/**
17
 * Class Rulebook
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/captainhook
22
 * @since   Class available since Release 0.9.0
23
 */
24
class Rulebook extends Base
25
{
26
    /**
27
     * Execute the configured action.
28
     *
29
     * @param  \sebastianfeldmann\CaptainHook\Config         $config
30
     * @param  \sebastianfeldmann\CaptainHook\Console\IO     $io
31
     * @param  \sebastianfeldmann\CaptainHook\Git\Repository $repository
32
     * @param  \sebastianfeldmann\CaptainHook\Config\Action  $action
33
     * @throws \sebastianfeldmann\CaptainHook\Exception\ActionExecution
34
     */
35 3
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
36
    {
37 3
        $rules     = $action->getOptions();
38 3
        $validator = new Validator();
39 3
        foreach ($rules as $class) {
40 3
            $validator->addRule($this->createRule($class));
41
        }
42 1
        $this->executeValidator($validator, $repository);
43 1
    }
44
45
    /**
46
     * Create a new rule.
47
     *
48
     * @param  string $class
49
     * @return \sebastianfeldmann\CaptainHook\Hook\Message\Validator\Rule
50
     * @throws \Exception
51
     */
52 3
    protected function createRule($class)
53
    {
54
        // make sure the class is available
55 3
        if (!class_exists($class)) {
56 1
            throw new \Exception('Unknown rule: ' . $class);
57
        }
58
59 2
        $rule = new $class();
60
61
        // make sure the class implements the Rule interface
62 2
        if (!$rule instanceof Validator\Rule) {
63 1
            throw new \Exception('Class \'' . $class . '\' must implement the Rule interface');
64
        }
65
66 1
        return $rule;
67
    }
68
}
69