Completed
Push — master ( fdd703...40dd8e )
by Sebastian
03:16
created

File::getOptionValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
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\Cli;
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       http://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
     * File constructor.
39
     *
40
     * @param string $file
41
     */
42 37
    public function __construct($file)
43
    {
44 37
        $this->filename = $file;
45 37
    }
46
47
    /**
48
     * Returns the phpbu Configuration.
49
     *
50
     * @param  \phpbu\App\Factory $factory
51
     * @return \phpbu\App\Configuration
52
     */
53 32
    public function getConfiguration(AppFactory $factory)
54
    {
55
        // create configuration first so the working directory is available for all adapters
56 32
        $configuration = new Configuration();
57 32
        $configuration->setFilename($this->filename);
58
59 32
        $this->setupAdapters($factory);
60
61 28
        $this->setAppSettings($configuration);
62 28
        $this->setLoggers($configuration);
63 26
        $this->setBackups($configuration);
64
65 12
        return $configuration;
66
    }
67
68
    /**
69
     * Load all available config adapters.
70
     *
71
     * @param \phpbu\App\Factory $factory
72
     */
73 32
    protected function setupAdapters(AppFactory $factory)
74
    {
75 32
        foreach ($this->getAdapterConfigs() as $config) {
76 3
            $this->adapters[$config->name] = $factory->createAdapter($config->type, $config->options);
77
        }
78 28
    }
79
80
    /**
81
     * Return a registered adapter.
82
     *
83
     * @param  string $name
84
     * @return \phpbu\App\Adapter
85
     * @throws \phpbu\App\Exception
86
     */
87 3
    protected function getAdapter($name)
88
    {
89 3
        if (!isset($this->adapters[$name])) {
90 1
            throw new Exception('no adapter registered with name: ' . $name);
91
        }
92 2
        return $this->adapters[$name];
93
    }
94
95
    /**
96
     * Return list of adapter configs.
97
     *
98
     * @return array
99
     */
100
    abstract protected function getAdapterConfigs();
101
102
    /**
103
     * Set the phpbu application settings.
104
     *
105
     * @param  \phpbu\App\Configuration $configuration
106
     */
107
    abstract public function setAppSettings(Configuration $configuration);
108
109
    /**
110
     * Set the log configuration.
111
     *
112
     * @param  \phpbu\App\Configuration $configuration
113
     * @throws \phpbu\App\Exception
114
     */
115
    abstract public function setLoggers(Configuration $configuration);
116
117
    /**
118
     * Set the backup configurations.
119
     *
120
     * @param  \phpbu\App\Configuration $configuration
121
     * @throws \phpbu\App\Exception
122
     */
123
    abstract public function setBackups(Configuration $configuration);
124
125
    /**
126
     * Converts a path to an absolute one if necessary.
127
     *
128
     * @param  string  $path
129
     * @param  boolean $useIncludePath
130
     * @return string
131
     */
132 27
    protected function toAbsolutePath($path, $useIncludePath = false)
133
    {
134 27
        return Cli::toAbsolutePath($path, dirname($this->filename), $useIncludePath);
135
    }
136
137
    /**
138
     * Return option value.
139
     * Checks if the value should be fetched from an Adapter, if not it just returns the value.
140
     *
141
     * @param  string $value
142
     * @return string
143
     */
144 28
    protected function getAdapterizedValue($value)
145
    {
146 28
        foreach (Util::getAdapterReplacements($value) as $replacement) {
147 3
            $search  = $replacement['search'];
148 3
            $replace = $this->getAdapter($replacement['adapter'])->getValue($replacement['path']);
149 2
            $value   = str_replace($search, $replace, $value);
150
        }
151 28
        return $value;
152
    }
153
154
    /**
155
     * Load the file.
156
     *
157
     * @param  string $filename
158
     * @throws \phpbu\App\Exception
159
     * @return \stdClass
160
     */
161 37
    protected function loadFile($filename)
162
    {
163 37
        $reporting = error_reporting(0);
164 37
        $contents  = file_get_contents($filename);
165 37
        error_reporting($reporting);
166
167 37
        if ($contents === false) {
168 2
            throw new Exception(sprintf('Could not read "%s".', $filename));
169
        }
170 35
        return $contents;
171
    }
172
}
173