Completed
Push — master ( 7da0d2...cca0f8 )
by Sebastian
03:03
created

Util   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateJsonConfiguration() 0 10 3
A validateHookConfig() 0 10 3
A validateActionsConfig() 0 11 4
A keysExist() 0 9 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\Config;
11
12
use sebastianfeldmann\CaptainHook\Hook\Util as HookUtil;
13
14
/**
15
 * Class Util
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/captainhook
20
 * @since   Class available since Release 1.0.3
21
 */
22
abstract class Util
23
{
24
    /**
25
     * Validate a configuration.
26
     *
27
     * @param  array $json
28
     * @throws \RuntimeException
29
     */
30
    public static function validateJsonConfiguration(array $json)
31
    {
32
        foreach($json as $hook => $config) {
33
            // check hook name
34
            if (!HookUtil::isValid($hook)) {
35
                throw new \RuntimeException('Config error: invalid hook \'' . $hook . '\'');
36
            }
37
            self::validateHookConfig($config);
38
        }
39
    }
40
41
    /**
42
     * Validate a hook configuration.
43
     *
44
     * @param  array $json
45
     * @throws \RuntimeException
46
     */
47
    public static function validateHookConfig(array $json)
48
    {
49
        if (!self::keysExist(['enabled', 'actions'], $json)) {
50
            throw new \RuntimeException('Config error: invalid hook configuration');
51
        }
52
        if (!is_array($json['actions'])) {
53
            throw new \RuntimeException('Config error: \'actions\' must be an array');
54
        }
55
        self::validateActionsConfig($json['actions']);
56
    }
57
58
    /**
59
     * Validate a list of action configurations.
60
     *
61
     * @param  array $json
62
     * @throws \RuntimeException
63
     */
64
    public static function validateActionsConfig(array $json)
65
    {
66
        foreach ($json as $action) {
67
            if (!self::keysExist(['action'], $action)) {
68
                throw new \RuntimeException('Config error: \'action\' missing');
69
            }
70
            if (empty($action['action'])) {
71
                throw new \RuntimeException('Config error: \'action\' can\'t be empty');
72
            }
73
        }
74
    }
75
76
    /**
77
     * Does an array have the expected keys.
78
     *
79
     * @param  array $keys
80
     * @param  array $subject
81
     * @return bool
82
     */
83
    private function keysExist(array $keys, array $subject)
84
    {
85
        foreach ($keys as $key) {
86
            if (!isset($subject[$key])) {
87
                return false;
88
            }
89
        }
90
        return true;
91
    }
92
}
93