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

Config::getHookConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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;
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 23
    public function __construct($path, $fileExists = false)
50
    {
51 23
        $this->path                = $path;
52 23
        $this->fileExists          = $fileExists;
53 23
        $this->hooks['commit-msg'] = new Config\Hook();
54 23
        $this->hooks['pre-commit'] = new Config\Hook();
55 23
        $this->hooks['pre-push']   = new Config\Hook();
56 23
    }
57
58
    /**
59
     * Is configuration loaded from file.
60
     *
61
     * @return bool
62
     */
63 12
    public function isLoadedFromFile()
64
    {
65 12
        return $this->fileExists;
66
    }
67
68
    /**
69
     * Path getter.
70
     *
71
     * @return string
72
     */
73 3
    public function getPath()
74
    {
75 3
        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 12
    public function getHookConfig($hook)
86
    {
87 12
        if (!Hook\Util::isValid($hook)) {
88 1
            throw new \InvalidArgumentException('Invalid hook name: ' . $hook);
89
        }
90 11
        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()
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