Completed
Push — master ( 18691f...14c33c )
by Pierre
03:58
created

Config   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 139
ccs 37
cts 37
cp 1
rs 10
wmc 14

11 Methods

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