Passed
Push — master ( 70b7f8...c79008 )
by Sebastian
01:40
created

Config::getGitDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
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 CaptainHook\App;
11
12
use InvalidArgumentException;
13
14
/**
15
 * Class Config
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/captainhookphp/captainhook
20
 * @since   Class available since Release 0.9.0
21
 * @internal
22
 */
23
class Config
24
{
25
    /**
26
     * Path to the config file
27
     *
28
     * @var string
29
     */
30
    private $path;
31
32
    /**
33
     * Does the config file exist
34
     *
35
     * @var bool
36
     */
37
    private $fileExists;
38
39
    /**
40
     * CaptainHook settings
41
     *
42
     * @var array
43
     */
44
    private $settings;
45
46
    /**
47
     * List of hook configs
48
     *
49
     * @var \CaptainHook\App\Config\Hook[]
50
     */
51
    private $hooks = [];
52
53
    /**
54
     * Config constructor
55
     *
56
     * @param string $path
57
     * @param bool   $fileExists
58
     * @param array  $settings
59
     */
60 59
    public function __construct(string $path, bool $fileExists = false, array $settings = [])
61
    {
62 59
        $this->path       = $path;
63 59
        $this->fileExists = $fileExists;
64 59
        $this->settings   = $settings;
65
66 59
        foreach (Hooks::getValidHooks() as $hook => $value) {
67 59
            $this->hooks[$hook] = new Config\Hook($hook);
68
        }
69 59
    }
70
71
    /**
72
     * Is configuration loaded from file
73
     *
74
     * @return bool
75
     */
76 23
    public function isLoadedFromFile() : bool
77
    {
78 23
        return $this->fileExists;
79
    }
80
81
    /**
82
     * Path getter
83
     *
84
     * @return string
85
     */
86 27
    public function getPath() : string
87
    {
88 27
        return $this->path;
89
    }
90
91
    /**
92
     * Return git directory path if configured, CWD/.git if not
93
     *
94
     * @return string
95
     */
96 1
    public function getGitDirectory() : string
97
    {
98 1
        return !empty($this->settings['git-directory'])
99 1
            ? dirname($this->path) . DIRECTORY_SEPARATOR . $this->settings['git-directory']
100 1
            : getcwd() . DIRECTORY_SEPARATOR . '.git';
101
    }
102
103
    /**
104
     * Return config for given hook
105
     *
106
     * @param  string $hook
107
     * @return \CaptainHook\App\Config\Hook
108
     * @throws \InvalidArgumentException
109
     */
110 27
    public function getHookConfig(string $hook) : Config\Hook
111
    {
112 27
        if (!Hook\Util::isValid($hook)) {
113 1
            throw new InvalidArgumentException('Invalid hook name: ' . $hook);
114
        }
115 26
        return $this->hooks[$hook];
116
    }
117
118
    /**
119
     * Return config array to write to disc
120
     *
121
     * @return array
122
     */
123 6
    public function getJsonData() : array
124
    {
125 6
        $return = [];
126
127 6
        foreach (Hooks::getValidHooks() as $hook => $value) {
128 6
            $return[$hook] = $this->hooks[$hook]->getJsonData();
129
        }
130
131 6
        return $return;
132
    }
133
}
134