Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

Config::isLoadedFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App;
13
14
use InvalidArgumentException;
15
use SebastianFeldmann\Camino\Check;
16
17
/**
18
 * Class Config
19
 *
20
 * @package CaptainHook
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/captainhookphp/captainhook
23
 * @since   Class available since Release 0.9.0
24
 */
25
class Config
26
{
27
    public const SETTING_BOOTSTRAP      = 'bootstrap';
28
    public const SETTING_COLORS         = 'ansi-colors';
29
    public const SETTING_GIT_DIR        = 'git-directory';
30
    public const SETTING_INCLUDES       = 'includes';
31
    public const SETTING_INCLUDES_LEVEL = 'includes-level';
32
    public const SETTING_RUN_EXEC       = 'run-exec';
33
    public const SETTING_RUN_MODE       = 'run-mode';
34
    public const SETTING_RUN_PATH       = 'run-path';
35
    public const SETTING_VERBOSITY      = 'verbosity';
36
37
    /**
38
     * Path to the config file
39
     *
40
     * @var string
41
     */
42
    private $path;
43
44
    /**
45
     * Does the config file exist
46
     *
47
     * @var bool
48
     */
49
    private $fileExists;
50
51
    /**
52
     * CaptainHook settings
53
     *
54
     * @var array<string, string>
55
     */
56
    private $settings;
57
58
    /**
59
     * List of hook configs
60
     *
61
     * @var array<string, \CaptainHook\App\Config\Hook>
62
     */
63
    private $hooks = [];
64
65
    /**
66
     * Config constructor
67
     *
68
     * @param string $path
69 69
     * @param bool   $fileExists
70
     * @param array  $settings
71 69
     */
72 69
    public function __construct(string $path, bool $fileExists = false, array $settings = [])
73 69
    {
74
        $this->path       = $path;
75 69
        $this->fileExists = $fileExists;
76 69
        $this->settings   = $settings;
77
78 69
        foreach (Hooks::getValidHooks() as $hook => $value) {
79
            $this->hooks[$hook] = new Config\Hook($hook);
80
        }
81
    }
82
83
    /**
84
     * Is configuration loaded from file
85 23
     *
86
     * @return bool
87 23
     */
88
    public function isLoadedFromFile(): bool
89
    {
90
        return $this->fileExists;
91
    }
92
93
    /**
94
     * Path getter
95 29
     *
96
     * @return string
97 29
     */
98
    public function getPath(): string
99
    {
100
        return $this->path;
101
    }
102
103
    /**
104
     * Return git directory path if configured, CWD/.git if not
105 8
     *
106
     * @return string
107 8
     */
108 2
    public function getGitDirectory(): string
109
    {
110
        if (empty($this->settings[self::SETTING_GIT_DIR])) {
111
            return getcwd() . '/.git';
112 6
        }
113 3
114 6
        // if repo path is absolute use it otherwise create an absolute path relative to the configuration file
115
        return Check::isAbsolutePath($this->settings[self::SETTING_GIT_DIR])
116
            ? $this->settings[self::SETTING_GIT_DIR]
117
            : \dirname($this->path) . '/' . $this->settings[self::SETTING_GIT_DIR];
118
    }
119
120
    /**
121
     * Return bootstrap file if configured, CWD/vendor/autoload.php by default
122 5
     *
123
     * @return string
124 5
     */
125 1
    public function getBootstrap(): string
126 5
    {
127
        return !empty($this->settings[self::SETTING_BOOTSTRAP])
128
            ? $this->settings[self::SETTING_BOOTSTRAP]
129
            : 'vendor/autoload.php';
130
    }
131
132
    /**
133
     * Return the configured verbosity
134 8
     *
135
     * @return string
136 8
     */
137 1
    public function getVerbosity(): string
138 8
    {
139
        return !empty($this->settings[self::SETTING_VERBOSITY])
140
            ? $this->settings[self::SETTING_VERBOSITY]
141
            : 'normal';
142
    }
143
144
    /**
145
     * Should the output use ansi colors
146 10
     *
147
     * @return bool
148 10
     */
149
    public function useAnsiColors(): bool
150
    {
151
        return (bool) ($this->settings[self::SETTING_COLORS] ?? true);
152
    }
153
154
    /**
155
     * Get configured run-mode
156 6
     *
157
     * @return string
158 6
     */
159
    public function getRunMode(): string
160
    {
161
        return (string) ($this->settings[self::SETTING_RUN_MODE] ?? 'local');
162
    }
163
164
    /**
165
     * Get configured run-exec
166 3
     *
167
     * @return string
168 3
     */
169
    public function getRunExec(): string
170
    {
171
        return (string) ($this->settings[self::SETTING_RUN_EXEC] ?? '');
172
    }
173
174
    /**
175
     * Get configured run-path
176
     *
177
     * @return string
178 28
     */
179
    public function getRunPath(): string
180 28
    {
181 1
        return (string) ($this->settings[self::SETTING_RUN_PATH] ?? '');
182
    }
183 27
184
    /**
185
     * Return config for given hook
186
     *
187
     * @param  string $hook
188
     * @return \CaptainHook\App\Config\Hook
189
     * @throws \InvalidArgumentException
190
     */
191 8
    public function getHookConfig(string $hook): Config\Hook
192
    {
193 8
        if (!Hook\Util::isValid($hook)) {
194
            throw new InvalidArgumentException('Invalid hook name: ' . $hook);
195 8
        }
196 1
        return $this->hooks[$hook];
197
    }
198
199 8
    /**
200 8
     * Return config array to write to disc
201
     *
202
     * @return array
203 8
     */
204
    public function getJsonData(): array
205
    {
206
        $data = [];
207
        // only append config settings if at least one setting is present
208
        if (!empty($this->settings)) {
209
            $data['config'] = $this->settings;
210
        }
211
        // append all configured hooks
212
        foreach (Hooks::getValidHooks() as $hook => $value) {
213
            $data[$hook] = $this->hooks[$hook]->getJsonData();
214
        }
215
216
        return $data;
217
    }
218
}
219