Passed
Push — master ( a6662b...0e2829 )
by Sebastian
03:17
created

Config::getRunPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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