Completed
Push — master ( 7efae6...e19db8 )
by Sebastian
03:17
created

Rules::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
c 0
b 0
f 0
dl 0
loc 16
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\Action;
11
12
use SebastianFeldmann\CaptainHook\Config;
13
use SebastianFeldmann\CaptainHook\Console\IO;
14
use SebastianFeldmann\CaptainHook\Hook\Message\Rule;
15
use SebastianFeldmann\CaptainHook\Hook\Message\RuleBook;
16
use SebastianFeldmann\Git\Repository;
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\Git\Repository             $repository
34
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
35
     * @throws \Exception
36
     */
37 5
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
38
    {
39 5
        $rules = $action->getOptions()->getAll();
40 5
        $book  = new RuleBook();
41 5
        foreach ($rules as $class) {
42 4
            $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 4
    protected function createRule(string $class) : Rule
55
    {
56
        // make sure the class is available
57 4
        if (!class_exists($class)) {
58 2
            throw new \Exception('Unknown rule: ' . $class);
59
        }
60
61 2
        $rule = new $class();
62
63
        // make sure the class implements the Rule interface
64 2
        if (!$rule instanceof Rule) {
65 1
            throw new \Exception('Class \'' . $class . '\' must implement the Rule interface');
66
        }
67
68 1
        return $rule;
69
    }
70
}
71