Completed
Push — master ( ca4c8c...687844 )
by Sebastian
03:10
created

Util::validateConditionsConfig()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6
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 CaptainHook\App\Config;
11
12
use CaptainHook\App\Hook\Util as HookUtil;
13
use CaptainHook\App\Config;
14
use CaptainHook\App\Storage\File\Json;
15
16
/**
17
 * Class Util
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/captainhookphp/captainhook
22
 * @since   Class available since Release 1.0.3
23
 */
24
abstract class Util
25
{
26
    /**
27
     * Validate a configuration
28
     *
29
     * @param  array $json
30
     * @return void
31
     * @throws \RuntimeException
32
     */
33 28
    public static function validateJsonConfiguration(array $json) : void
34
    {
35 28
        foreach (HookUtil::getValidHooks() as $hook => $class) {
36 28
            if (isset($json[$hook])) {
37 28
                self::validateHookConfig($json[$hook]);
38
            }
39
        }
40 21
    }
41
42
    /**
43
     * Validate a hook configuration
44
     *
45
     * @param  array $json
46
     * @return void
47
     * @throws \RuntimeException
48
     */
49 27
    public static function validateHookConfig(array $json) : void
50
    {
51 27
        if (!self::keysExist(['enabled', 'actions'], $json)) {
52 2
            throw new \RuntimeException('Config error: invalid hook configuration');
53
        }
54 25
        if (!is_array($json['actions'])) {
55 1
            throw new \RuntimeException('Config error: \'actions\' must be an array');
56
        }
57 24
        self::validateActionsConfig($json['actions']);
58 20
    }
59
60
    /**
61
     * Validate a list of action configurations
62
     *
63
     * @param  array $json
64
     * @return void
65
     * @throws \RuntimeException
66
     */
67 24
    public static function validateActionsConfig(array $json) : void
68
    {
69 24
        foreach ($json as $action) {
70 18
            if (!self::keysExist(['action'], $action)) {
71 1
                throw new \RuntimeException('Config error: \'action\' missing');
72
            }
73 17
            if (empty($action['action'])) {
74 1
                throw new \RuntimeException('Config error: \'action\' can\'t be empty');
75
            }
76 16
            if (!empty($action['conditions'])) {
77 16
                self::validateConditionsConfig($action['conditions']);
78
            }
79
        }
80 20
    }
81
82
    /**
83
     * Validate a list of condition configurations
84
     *
85
     * @param  array $json
86
     * @throws \RuntimeException
87
     */
88 4
    public static function validateConditionsConfig(array $json) : void
89
    {
90 4
        foreach ($json as $condition) {
91 4
            if (!self::keysExist(['exec'], $condition) || empty($condition['exec'])) {
92 1
                throw new \RuntimeException('Config error: \'exec\' is required for conditions');
93
            }
94 3
            if (!empty($condition['args']) && !is_array($condition['args'])) {
95 3
                throw new \RuntimeException('Config error: invalid \'args\' configuration');
96
            }
97
        }
98 2
    }
99
100
    /**
101
     * Write the config to disk
102
     *
103
     * @param  \CaptainHook\App\Config $config
104
     * @return void
105
     */
106 6
    public static function writeToDisk(Config $config) : void
107
    {
108 6
        $filePath = $config->getPath();
109 6
        $file     = new Json($filePath);
110 6
        $file->write($config->getJsonData());
111 6
    }
112
113
    /**
114
     * Does an array have the expected keys
115
     *
116
     * @param  array $keys
117
     * @param  array $subject
118
     * @return bool
119
     */
120 27
    private static function keysExist(array $keys, array $subject) : bool
121
    {
122 27
        foreach ($keys as $key) {
123 27
            if (!isset($subject[$key])) {
124 27
                return false;
125
            }
126
        }
127 25
        return true;
128
    }
129
}
130