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

Config   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 87
c 0
b 0
f 0
ccs 19
cts 19
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A isLoadedFromFile() 0 4 1
A getPath() 0 4 1
A getHookConfig() 0 7 2
A getJsonData() 0 8 1
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;
11
12
/**
13
 * Class Config
14
 *
15
 * @package CaptainHook
16
 * @author  Sebastian Feldmann <[email protected]>
17
 * @link    https://github.com/sebastianfeldmann/captainhook
18
 * @since   Class available since Release 0.9.0
19
 */
20
class Config
21
{
22
    /**
23
     * Path to the config file
24
     *
25
     * @var string
26
     */
27
    private $path;
28
29
    /**
30
     * Does the config file exist
31
     *
32
     * @var bool
33
     */
34
    private $fileExists;
35
36
    /**
37
     * List of hook configs
38
     *
39
     * @var \SebastianFeldmann\CaptainHook\Config\Hook[]
40
     */
41
    private $hooks = [];
42
43
    /**
44
     * Config constructor.
45
     *
46
     * @param string $path
47
     * @param bool   $fileExists
48
     */
49 35
    public function __construct(string $path, bool $fileExists = false)
50
    {
51 35
        $this->path                = $path;
52 35
        $this->fileExists          = $fileExists;
53 35
        $this->hooks['commit-msg'] = new Config\Hook();
54 35
        $this->hooks['pre-commit'] = new Config\Hook();
55 35
        $this->hooks['pre-push']   = new Config\Hook();
56 35
    }
57
58
    /**
59
     * Is configuration loaded from file.
60
     *
61
     * @return bool
62
     */
63 13
    public function isLoadedFromFile() : bool
64
    {
65 13
        return $this->fileExists;
66
    }
67
68
    /**
69
     * Path getter.
70
     *
71
     * @return string
72
     */
73 6
    public function getPath() : string
74
    {
75 6
        return $this->path;
76
    }
77
78
    /**
79
     * Return config for given hook.
80
     *
81
     * @param  string $hook
82
     * @return \SebastianFeldmann\CaptainHook\Config\Hook
83
     * @throws \InvalidArgumentException
84
     */
85 13
    public function getHookConfig(string $hook) : Config\Hook
86
    {
87 13
        if (!Hook\Util::isValid($hook)) {
88 1
            throw new \InvalidArgumentException('Invalid hook name: ' . $hook);
89
        }
90 12
        return $this->hooks[$hook];
91
    }
92
93
    /**
94
     * Return config array to write to disc.
95
     *
96
     * @return array
97
     */
98 3
    public function getJsonData() : array
99
    {
100
        return [
101 3
            'commit-msg' => $this->hooks['commit-msg']->getJsonData(),
102 3
            'pre-commit' => $this->hooks['pre-commit']->getJsonData(),
103 3
            'pre-push'   => $this->hooks['pre-push']->getJsonData()
104
        ];
105
    }
106
}
107