Completed
Push — master ( 7b4301...3d29af )
by Sebastian
05:32
created

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