Passed
Push — master ( b34030...56c38f )
by Sebastian
03:50
created

Factory::configureHook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
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;
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 12
    public static function create($path = null)
33
    {
34 12
        $factory = new static();
35
36 12
        return $factory->createConfig($path);
37
    }
38
39
    /**
40
     * Create a CaptainHook configuration.
41
     *
42
     * @param  string $path
43
     * @return \sebastianfeldmann\CaptainHook\Config
44
     */
45 12
    public function createConfig($path = null)
46
    {
47 12
        $path       = $path ?: getcwd() . DIRECTORY_SEPARATOR . 'captainhook.json';
48 12
        $json       = new Json($path);
49 12
        $fileExists = $json->exists();
50 11
        $config     = new Config($path, $fileExists);
51
52 11
        if ($fileExists) {
53 8
            $this->configure($config, $json->readAssoc());
1 ignored issue
show
Bug introduced by
It seems like $json->readAssoc() targeting sebastianfeldmann\Captai...\File\Json::readAssoc() can also be of type object<stdClass>; however, sebastianfeldmann\Captai...ig\Factory::configure() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
        }
55
56 11
        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 8
    protected function configure(Config $config, array $json)
66
    {
67 8
        foreach ($json as $hook => $data) {
68 8
            $this->configureHook($config->getHookConfig($hook), $data);
69
        }
70 8
    }
71
72
    /**
73
     * Setup a hook configuration by json data.
74
     *
75
     * @param \sebastianfeldmann\CaptainHook\Config\Hook $config
76
     * @param array                 $json
77
     */
78 8
    protected function configureHook(Config\Hook $config, array $json)
79
    {
80 8
        $config->setEnabled($json['enabled']);
81 8
        foreach ($json['actions'] as $actionJson) {
82 6
            $type = Util::getActionType($actionJson['action']);
83 6
            $config->addAction(new Config\Action($type, $actionJson['action'], $actionJson['options']));
84
        }
85 8
    }
86
}
87