Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

Rules   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 18
c 0
b 0
f 0
dl 0
loc 64
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createRuleFromConfig() 0 6 3
A execute() 0 12 3
A createRule() 0 15 4
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Message\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Hook\Message\Rule;
17
use CaptainHook\App\Hook\Message\RuleBook;
18
use Exception;
19
use RuntimeException;
20
use SebastianFeldmann\Git\Repository;
21
22
/**
23
 * Class Rules
24
 *
25
 * @package CaptainHook
26
 * @author  Sebastian Feldmann <[email protected]>
27
 * @link    https://github.com/captainhookphp/captainhook
28
 * @since   Class available since Release 0.9.0
29
 */
30
class Rules extends Book
31
{
32
    /**
33
     * Execute the configured action
34
     *
35
     * @param  \CaptainHook\App\Config           $config
36
     * @param  \CaptainHook\App\Console\IO       $io
37
     * @param  \SebastianFeldmann\Git\Repository $repository
38 6
     * @param  \CaptainHook\App\Config\Action    $action
39
     * @return void
40 6
     * @throws \Exception
41 6
     */
42 6
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
43 4
    {
44
        $rules = $action->getOptions()->getAll();
45 3
        $book  = new RuleBook();
46 3
        foreach ($rules as $rule) {
47
            if (is_string($rule)) {
48
                $book->addRule($this->createRule($rule));
49
                continue;
50
            }
51
            $book->addRule($this->createRuleFromConfig($rule));
52
        }
53
        $this->validate($book, $repository, $io);
54
    }
55 4
56
    /**
57
     * Create a new rule
58 4
     *
59 2
     * @param  string $class
60
     * @param  array  $args
61
     * @return \CaptainHook\App\Hook\Message\Rule
62 2
     * @throws \Exception
63
     */
64
    protected function createRule(string $class, array $args = []): Rule
65 2
    {
66 1
        // make sure the class is available
67
        if (!class_exists($class)) {
68
            throw new Exception('Unknown rule: ' . $class);
69 1
        }
70
71
        $rule = empty($args) ? new $class() : new $class(...$args);
72
73
        // make sure the class implements the Rule interface
74
        if (!$rule instanceof Rule) {
75
            throw new Exception('Class \'' . $class . '\' must implement the Rule interface');
76
        }
77
78
        return $rule;
79
    }
80
81
    /**
82
     * Create a rule from a argument containing configuration
83
     *
84
     * @param  array $config
85
     * @return \CaptainHook\App\Hook\Message\Rule
86
     * @throws \Exception
87
     */
88
    private function createRuleFromConfig(array $config): Rule
89
    {
90
        if (!is_string($config[0]) || !is_array($config[1])) {
91
            throw new RuntimeException('Invalid rule configuration');
92
        }
93
        return $this->createRule($config[0], $config[1]);
94
    }
95
}
96