Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

Config   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 140
rs 10
wmc 14

10 Methods

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