Config::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace Fyuze\Config;
3
4
use SplFileInfo;
5
use GlobIterator;
6
use InvalidArgumentException;
7
use Fyuze\Config\Parsers\PHP;
8
9
class Config
10
{
11
    /**
12
     * The configuration path
13
     *
14
     * @var string
15
     */
16
    protected $path;
17
18
    /**
19
     * Application env
20
     *
21
     * @var string
22
     */
23
    protected $env;
24
25
    /**
26
     * Loaded configurations
27
     *
28
     * @var array
29
     */
30
    protected $configs;
31
32
    /**
33
     * @var array
34
     */
35
    protected $types = [
36
        PHP::class
37
    ];
38
39
    /**
40
     * @param string $path
41
     * @param string $env
42
     */
43 12
    public function __construct($path, $env)
44
    {
45 12
        $this->path = $path;
46 12
        $this->env = $env;
47 12
        $this->load();
48
49
    }
50
51
    /**
52
     * @param $key
53
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
54
     * @return mixed
55
     * @throws InvalidArgumentException
56
     */
57 10
    public function get($key, $default = null)
58
    {
59 10
        if (array_key_exists($key, $this->configs)) {
60 8
            return $this->configs[$key];
61
        }
62
63 7
        $configs = $this->configs;
64
65 7
        foreach (explode('.', $key) as $value) {
66
67 7
            $configs = array_key_exists($value, $configs) ? $configs[$value] : $default;
68
        }
69
70 7
        return $configs;
71
    }
72
73
    /**
74
     * @param $key
75
     * @param $value
76
     * @return mixed
77
     */
78 3
    public function set($key, $value)
79
    {
80 3
        $configs = &$this->configs;
81
82 3
        foreach (explode('.', $key) as $segment) {
83 3
            $configs = &$configs[$segment];
84
        }
85
86 3
        return $this->configs[$key] = $configs = $value;
87
    }
88
89
    /**
90
     *
91
     */
92 12
    protected function load()
93
    {
94 12
        if (!is_dir($this->path)) {
95 1
            throw new InvalidArgumentException(sprintf('The path you defined is not a valid directory: %s', $this->path));
96
        }
97
98 12
        foreach (new GlobIterator($this->path . '/*.*') as $file) {
99
100 12
            if($config = $this->getType($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type string; however, parameter $file of Fyuze\Config\Config::getType() does only seem to accept SplFileInfo, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
            if($config = $this->getType(/** @scrutinizer ignore-type */ $file)) {
Loading history...
101 12
                $this->configs[$config[0]] = $config[1];
102
            }
103
        }
104
    }
105
106
    /**
107
     * @param SplFileInfo $file
108
     * @return array
109
     */
110 12
    protected function getType(SplFileInfo $file)
111
    {
112 12
        $extension = $file->getExtension();
113 12
        $name = $file->getBasename(".$extension");
114
115 12
        $type = array_filter($this->types, function ($n) use ($extension) {
116 12
            return in_array($extension, $n::$extensions);
117 12
        });
118
119 12
        if (!$class = reset($type)) {
120 12
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
121
        }
122
123 12
        return [$name, (new $class)->parse($file)];
124
    }
125
}
126