Completed
Push — master ( 58b546...f86624 )
by Sebastian
02:38
created

File::getValidationErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace phpbu\App\Configuration\Loader;
3
4
use phpbu\App\Adapter\Util;
5
use phpbu\App\Configuration;
6
use phpbu\App\Exception;
7
use phpbu\App\Factory as AppFactory;
8
use phpbu\App\Util\Path as PathUtil;
9
10
/**
11
 * Base class for file based phpbu configuration
12
 *
13
 * @package    phpbu
14
 * @subpackage App
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       https://phpbu.de/
19
 * @since      Class available since Release 2.1.2
20
 */
21
abstract class File
22
{
23
    /**
24
     * Path to config file
25
     *
26
     * @var string
27
     */
28
    protected $filename;
29
30
    /**
31
     * Path to config file
32
     *
33
     * @var array
34
     */
35
    protected $adapters;
36
37
    /**
38
     * Handling the bootstrapping
39
     *
40
     * @var \phpbu\App\Configuration\Bootstrapper
41
     */
42
    protected $bootstrapper;
43
44
    /**
45
     * List of validation errors
46
     *
47
     * @var array
48
     */
49
    protected $errors = [];
50
51
    /**
52
     * File constructor
53
     *
54
     * @param string                                $file
55
     * @param \phpbu\App\Configuration\Bootstrapper $bootstrapper
56
     */
57 39
    public function __construct(string $file, Configuration\Bootstrapper $bootstrapper)
58
    {
59 39
        $this->filename     = $file;
60 39
        $this->bootstrapper = $bootstrapper;
61 39
    }
62
63
    /**
64
     * Is the configuration valid
65
     *
66
     * @return bool
67
     */
68
    public function hasValidationErrors(): bool
69
    {
70
        return \count($this->errors) > 0;
71
    }
72
73
    /**
74
     * Return a list of all validation errors
75
     *
76
     * @return array
77
     */
78
    public function getValidationErrors(): array
79
    {
80
        return $this->errors;
81
    }
82
83
    /**
84
     * Returns the phpbu Configuration
85
     *
86
     * @param  \phpbu\App\Factory $factory
87
     * @return \phpbu\App\Configuration
88
     * @throws \phpbu\App\Exception
89
     */
90 33
    public function getConfiguration(AppFactory $factory) : Configuration
91
    {
92
        // create configuration first so the working directory is available for all adapters
93 33
        $configuration = new Configuration();
94 33
        $configuration->setFilename($this->filename);
95
96 33
        $this->setAppSettings($configuration);
97 33
        $this->handleBootstrap($configuration);
98 33
        $this->setupAdapters($factory);
99
100 29
        $this->setLoggers($configuration);
101 27
        $this->setBackups($configuration);
102
103 13
        return $configuration;
104
    }
105
106
    /**
107
     * Load all available config adapters
108
     *
109
     * @param  \phpbu\App\Factory $factory
110
     * @throws \phpbu\App\Exception
111
     */
112 33
    protected function setupAdapters(AppFactory $factory)
113
    {
114 33
        foreach ($this->getAdapterConfigs() as $config) {
115 4
            $this->adapters[$config->name] = $factory->createAdapter($config->type, $config->options);
116
        }
117 29
    }
118
119
    /**
120
     * Return a registered adapter
121
     *
122
     * @param  string $name
123
     * @return \phpbu\App\Adapter
124
     * @throws \phpbu\App\Exception
125
     */
126 4
    protected function getAdapter($name)
127
    {
128 4
        if (!isset($this->adapters[$name])) {
129 1
            throw new Exception('no adapter registered with name: ' . $name);
130
        }
131 3
        return $this->adapters[$name];
132
    }
133
134
    /**
135
     * Return list of adapter configs
136
     *
137
     * @return array
138
     */
139
    abstract protected function getAdapterConfigs();
140
141
    /**
142
     * Set the phpbu application settings
143
     *
144
     * @param  \phpbu\App\Configuration $configuration
145
     */
146
    abstract public function setAppSettings(Configuration $configuration);
147
148
    /**
149
     * Set the log configuration
150
     *
151
     * @param  \phpbu\App\Configuration $configuration
152
     * @throws \phpbu\App\Exception
153
     */
154
    abstract public function setLoggers(Configuration $configuration);
155
156
    /**
157
     * Set the backup configurations
158
     *
159
     * @param  \phpbu\App\Configuration $configuration
160
     * @throws \phpbu\App\Exception
161
     */
162
    abstract public function setBackups(Configuration $configuration);
163
164
    /**
165
     * Handles the bootstrap file inclusion
166
     *
167
     * @param  \phpbu\App\Configuration $configuration
168
     * @throws \phpbu\App\Exception
169
     */
170 33
    protected function handleBootstrap(Configuration $configuration)
171
    {
172 33
        $this->bootstrapper->run($configuration);
173 33
    }
174
175
    /**
176
     * Converts a path to an absolute one if necessary
177
     *
178
     * @param  string  $path
179
     * @param  boolean $useIncludePath
180
     * @return string
181
     */
182 32
    protected function toAbsolutePath($path, $useIncludePath = false)
183
    {
184 32
        return PathUtil::toAbsolutePath($path, dirname($this->filename), $useIncludePath);
185
    }
186
187
    /**
188
     * Return option value
189
     * Checks if the value should be fetched from an Adapter, if not it just returns the value.
190
     *
191
     * @param  string $value
192
     * @return string
193
     * @throws \phpbu\App\Exception
194
     */
195 29
    protected function getAdapterizedValue($value)
196
    {
197 29
        if (!empty($value)) {
198 29
            foreach (Util::getAdapterReplacements($value) as $replacement) {
199 4
                $search  = $replacement['search'];
200 4
                $replace = $this->getAdapter($replacement['adapter'])->getValue($replacement['path']);
201 3
                $value   = str_replace($search, $replace, $value);
202
            }
203
        }
204 29
        return $value;
205
    }
206
207
    /**
208
     * Load the file
209
     *
210
     * @param  string $filename
211
     * @throws \phpbu\App\Exception
212
     * @return \stdClass
213
     */
214 39
    protected function loadFile($filename)
215
    {
216 39
        $reporting = error_reporting(0);
217 39
        $contents  = file_get_contents($filename);
218 39
        error_reporting($reporting);
219
220 39
        if ($contents === false) {
221 2
            throw new Exception(sprintf('Could not read "%s".', $filename));
222
        }
223 37
        return $contents;
224
    }
225
}
226