Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

Util   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 71
c 0
b 0
f 0
ccs 25
cts 25
cp 1
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 17
    public static function validateJsonConfiguration(array $json)
31
    {
32 17
        foreach ($json as $hook => $config) {
33
            // check hook name
34 16
            if (!HookUtil::isValid($hook)) {
35 1
                throw new \RuntimeException('Config error: invalid hook \'' . $hook . '\'');
36
            }
37 15
            self::validateHookConfig($config);
38
        }
39 11
    }
40
41
    /**
42
     * Validate a hook configuration.
43
     *
44
     * @param  array $json
45
     * @throws \RuntimeException
46
     */
47 15
    public static function validateHookConfig(array $json)
48
    {
49 15
        if (!self::keysExist(['enabled', 'actions'], $json)) {
50 2
            throw new \RuntimeException('Config error: invalid hook configuration');
51
        }
52 13
        if (!is_array($json['actions'])) {
53 1
            throw new \RuntimeException('Config error: \'actions\' must be an array');
54
        }
55 12
        self::validateActionsConfig($json['actions']);
56 10
    }
57
58
    /**
59
     * Validate a list of action configurations.
60
     *
61
     * @param  array $json
62
     * @throws \RuntimeException
63
     */
64 12
    public static function validateActionsConfig(array $json)
65
    {
66 12
        foreach ($json as $action) {
67 10
            if (!self::keysExist(['action'], $action)) {
68 1
                throw new \RuntimeException('Config error: \'action\' missing');
69
            }
70 9
            if (empty($action['action'])) {
71 9
                throw new \RuntimeException('Config error: \'action\' can\'t be empty');
72
            }
73
        }
74 10
    }
75
76
    /**
77
     * Does an array have the expected keys.
78
     *
79
     * @param  array $keys
80
     * @param  array $subject
81
     * @return bool
82
     */
83 15
    private static function keysExist(array $keys, array $subject) : bool
84
    {
85 15
        foreach ($keys as $key) {
86 15
            if (!isset($subject[$key])) {
87 15
                return false;
88
            }
89
        }
90 13
        return true;
91
    }
92
}
93