Completed
Push — master ( fac4d7...cd236f )
by Pierre
03:33
created

Config::getFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Component;
4
5
use App\Component\Interfaces\IConfig;
6
7
/**
8
 * App\Component\Config
9
 *
10
 * config manager
11
 *
12
 * @author pierrefromager
13
 */
14
15
class Config implements IConfig
16
{
17
18
    protected $path;
19
    protected $env;
20
    protected $settings;
21
22
    /**
23
     * instanciate
24
     *
25
     * @param string $env
26
     */
27
    public function __construct(string $env, string $path)
28
    {
29
        $this->path = $path;
30
        $this->setEnv($env);
31
        $this->load();
32
        return $this;
33
    }
34
35
    /**
36
     * set config environment
37
     *
38
     * @param string $env
39
     * @return IConfig
40
     */
41
    public function setEnv(string $env = self::ENV_DEV): IConfig
42
    {
43
        $this->env = $env;
44
        return $this;
45
    }
46
47
    /**
48
     * returns env
49
     *
50
     * @return string
51
     */
52
    public function getEnv(): string
53
    {
54
        return $this->env;
55
    }
56
57
    /**
58
     * set config path
59
     *
60
     * @param string $path
61
     * @return IConfig
62
     */
63
    public function setPath(string $path): IConfig
64
    {
65
        $this->path = $path;
66
        return $this;
67
    }
68
69
    /**
70
     * returns config path
71
     *
72
     * @return string
73
     */
74
    public function getPath(): string
75
    {
76
        return $this->path;
77
    }
78
79
    /**
80
     * return config array for a main entry key
81
     *
82
     * @param string $key
83
     * @return array
84
     */
85
    public function getSettings(string $key = ''): array
86
    {
87
        return ($key) ? $this->settings[$key] : $this->settings;
88
    }
89
90
    /**
91
     * return true if config main entry for a key exists
92
     *
93
     * @param string $key
94
     * @return boolean
95
     */
96
    public function hasEntry(string $key): bool
97
    {
98
        return isset($this->settings[$key]);
99
    }
100
101
    /**
102
     * load config for a given env
103
     *
104
     * @return IConfig
105
     */
106
    public function load(): IConfig
107
    {
108
        $filename = realpath($this->getFilename());
109
        if (false === $this->check($filename)) {
110
            throw new \Exception(
111
                sprintf(
112
                    self::CONFIG_ERROR_MISSING . '%s on %s',
113
                    $this->env,
114
                    $this->path
115
                )
116
            );
117
        }
118
        $this->settings = require $this->getFilename();
119
        return $this;
120
    }
121
122
    /**
123
     * getFilename
124
     *
125
     * @return string
126
     */
127
    protected function getFilename(): string
128
    {
129
        return $this->path . $this->env . '.php';
130
    }
131
132
    /**
133
     * check
134
     *
135
     * @param string $filename
136
     * @return boolean
137
     */
138
    protected function check($filename): bool
139
    {
140
        return (in_array($this->env, $this->getAllowedEnv())
141
            && file_exists($filename));
142
    }
143
144
    /**
145
     * getAllowedEnv
146
     *
147
     * @return array
148
     */
149
    protected function getAllowedEnv(): array
150
    {
151
        return [
152
            self::ENV_DEV, self::ENV_INT, self::ENV_PROD,
153
            self::ENV_TEST, self::ENV_CLI
154
        ];
155
    }
156
}
157