Completed
Push — master ( 016444...91d1a7 )
by Sebastian
02:32
created

Rules   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 45
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 9 2
A createRule() 0 16 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\Action;
11
12
use sebastianfeldmann\CaptainHook\Config;
13
use sebastianfeldmann\CaptainHook\Console\IO;
14
use sebastianfeldmann\CaptainHook\Git\Repository;
15
use sebastianfeldmann\CaptainHook\Hook\Message\Rule;
16
use sebastianfeldmann\CaptainHook\Hook\Message\RuleBook;
17
18
/**
19
 * Class Rules
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/sebastianfeldmann/captainhook
24
 * @since   Class available since Release 0.9.0
25
 */
26
class Rules extends Book
27
{
28
    /**
29
     * Execute the configured action.
30
     *
31
     * @param  \sebastianfeldmann\CaptainHook\Config         $config
32
     * @param  \sebastianfeldmann\CaptainHook\Console\IO     $io
33
     * @param  \sebastianfeldmann\CaptainHook\Git\Repository $repository
34
     * @param  \sebastianfeldmann\CaptainHook\Config\Action  $action
35
     * @throws \Exception
36
     */
37 4
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
38
    {
39 4
        $rules = $action->getOptions()->getAll();
40 4
        $book  = new RuleBook();
41 4
        foreach ($rules as $class) {
42 3
            $book->addRule($this->createRule($class));
43
        }
44 2
        $this->validate($book, $repository);
45 2
    }
46
47
    /**
48
     * Create a new rule.
49
     *
50
     * @param  string $class
51
     * @return \sebastianfeldmann\CaptainHook\Hook\Message\Rule
52
     * @throws \Exception
53
     */
54 3
    protected function createRule($class)
55
    {
56
        // make sure the class is available
57 3
        if (!class_exists($class)) {
58 2
            throw new \Exception('Unknown rule: ' . $class);
59
        }
60
61 1
        $rule = new $class();
62
63
        // make sure the class implements the Rule interface
64 1
        if (!$rule instanceof Rule) {
65
            throw new \Exception('Class \'' . $class . '\' must implement the Rule interface');
66
        }
67
68 1
        return $rule;
69
    }
70
}
71