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

Factory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 66
c 0
b 0
f 0
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A createConfig() 0 13 3
A configure() 0 8 2
A configureHook() 0 9 4
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\Config;
13
use SebastianFeldmann\CaptainHook\Hook\Util as HookUtil;
14
use SebastianFeldmann\CaptainHook\Storage\File\Json;
15
16
/**
17
 * Class Factory
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/captainhook
22
 * @since   Class available since Release 0.9.0
23
 */
24
class Factory
25
{
26
    /**
27
     * Config factory method.
28
     *
29
     * @param  string $path
30
     * @return \SebastianFeldmann\CaptainHook\Config
31
     */
32 13
    public static function create(string $path = '')
33
    {
34 13
        $factory = new static();
35
36 13
        return $factory->createConfig($path);
37
    }
38
39
    /**
40
     * Create a CaptainHook configuration.
41
     *
42
     * @param  string $path
43
     * @return \SebastianFeldmann\CaptainHook\Config
44
     */
45 13
    public function createConfig($path = '')
46
    {
47 13
        $path       = $path ?: getcwd() . DIRECTORY_SEPARATOR . 'captainhook.json';
48 13
        $json       = new Json($path);
49 13
        $fileExists = $json->exists();
50 13
        $config     = new Config($path, $fileExists);
51
52 13
        if ($fileExists) {
53 10
            $this->configure($config, $json->readAssoc());
54
        }
55
56 13
        return $config;
57
    }
58
59
    /**
60
     * Initialize the configuration with data load from config file.
61
     *
62
     * @param \SebastianFeldmann\CaptainHook\Config $config
63
     * @param array                                 $json
64
     */
65 10
    protected function configure(Config $config, array $json)
66
    {
67 10
        Util::validateJsonConfiguration($json);
68
69 10
        foreach ($json as $hook => $data) {
70 9
            $this->configureHook($config->getHookConfig($hook), $data);
71
        }
72 10
    }
73
74
    /**
75
     * Setup a hook configuration by json data.
76
     *
77
     * @param \SebastianFeldmann\CaptainHook\Config\Hook $config
78
     * @param array                                      $json
79
     */
80 9
    protected function configureHook(Config\Hook $config, array $json)
81
    {
82 9
        $config->setEnabled($json['enabled']);
83 9
        foreach ($json['actions'] as $actionJson) {
84 7
            $type    = HookUtil::getActionType($actionJson['action']);
85 7
            $options = isset($actionJson['options']) && is_array($actionJson['options']) ? $actionJson['options'] : [];
86 7
            $config->addAction(new Config\Action($type, $actionJson['action'], $options));
87
        }
88 9
    }
89
}
90