Completed
Push — master ( e73bbc...d6ad0c )
by Sebastian
03:01
created

PHPArray::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace phpbu\App\Adapter;
3
4
use Dotenv\Dotenv as DotenvLib;
5
use phpbu\App\Adapter;
6
use phpbu\App\Configuration;
7
use phpbu\App\Exception;
8
use phpbu\App\Util;
9
10
/**
11
 * PHPArray Adapter
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 4.0.1
20
 */
21
class PHPArray implements Adapter
22
{
23
    /**
24
     * Path to the config file
25
     *
26
     * @var string
27
     */
28
    private $file;
29
30
    /**
31
     * Configuration
32
     *
33
     * @var array
34
     */
35
    private $config;
36
37
    /**
38
     * Setup the adapter.
39
     *
40
     * @param  array $conf
41
     * @return void
42
     */
43
    public function setup(array $conf)
44
    {
45
        $path       = Util\Arr::getValue($conf, 'file', '.env');
46
        $this->file = Util\Cli::toAbsolutePath($path, Configuration::getWorkingDirectory());
47
        $this->load();
48
    }
49
50
    /**
51
     * Load config file to local file.
52
     *
53
     * @throws \phpbu\App\Exception
54
     */
55
    private function load()
56
    {
57
        if (!file_exists($this->file)) {
58
            throw new Exception('config file not found');
59
        }
60
        $this->config = require $this->file;
61
    }
62
63
    /**
64
     * Return a value for a given path.
65
     *
66
     * @param  string $path
67
     * @return string
68
     * @throws \phpbu\App\Exception
69
     */
70
    public function getValue($path)
71
    {
72
        $arrPath = explode('.', $path);
73
        $data    = $this->config;
74
        foreach ($arrPath as $segment) {
75
            if (!isset($data[$segment])) {
76
                throw new Exception('invalid config path');
77
            }
78
            $data = $data[$segment];
79
        }
80
        return (string) $data;
81
    }
82
}
83