Test Failed
Push — master ( 55c49a...565016 )
by Julien
05:04
created

Env   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Test Coverage

Coverage 47.19%

Importance

Changes 0
Metric Value
wmc 39
eloc 78
c 0
b 0
f 0
dl 0
loc 289
ccs 42
cts 89
cp 0.4719
rs 9.28

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 3 1
A setFileEncoding() 0 3 1
A getNames() 0 3 1
A __callStatic() 0 3 1
B get() 0 28 8
A __call() 0 3 1
A initialize() 0 14 2
A setType() 0 4 2
A getDotenv() 0 3 1
A setShortCircuit() 0 3 1
A call() 0 23 5
A setPaths() 0 3 1
A set() 0 4 1
A getShortCircuit() 0 3 1
A __get() 0 3 1
A getFileEncoding() 0 3 1
A __construct() 0 3 1
A getType() 0 3 1
B getPaths() 0 18 7
A setNames() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Support;
13
14
use Dotenv\Dotenv;
15
use Phalcon\Di\DiInterface;
16
17
/**
18
 * Allow to access environment variable easily
19
 *
20
 * Example usage:
21
 * ```php
22
 * $this->SET_APPLICATION_ENV('production'); // SET APPLICATION_ENV to 'production'
23
 * self::SET_APPLICATION_ENV('production'); // SET APPLICATION_ENV to 'production'
24
 * $this->GET_APPLICATION_ENV('default'); // GET APPLICATION_ENV value or 'default'
25
 * self::GET_APPLICATION_ENV('default'); // GET APPLICATION_ENV value or 'default'
26
 * ```
27
 * @todo fix dotenv mandatory file
28
 */
29
class Env
30
{
31
    public static bool $initialized = false;
32
    
33
    public static Dotenv $dotenv;
34
    public static array $vars = [];
35
    
36
    public static ?array $paths = null;
37
    public static array $names = ['.env'];
38
    public static string $type = 'mutable';
39
    public static bool $shortCircuit = true;
40
    public static ?string $fileEncoding = null;
41
    
42
    public function __construct(DiInterface $di)
43
    {
44
        self::initialize();
45
    }
46
    
47
    /**
48
     *
49
     * @param $paths
50
     * @param $names
51
     * @param bool $shortCircuit
52
     * @param string|null $fileEncoding
53
     * @param $type
54
     * @return Dotenv
55
     */
56 1
    public static function initialize($paths = null, $names = null, bool $shortCircuit = null, string $fileEncoding = null, $type = null): Dotenv
57
    {
58 1
        if (!self::$initialized) {
59 1
            $type ??= self::getType();
60 1
            $paths ??= self::getPaths();
61 1
            $names ??= self::getNames();
62 1
            $shortCircuit ??= self::getShortCircuit();
63 1
            $fileEncoding ??= self::getFileEncoding();
64 1
            self::$dotenv = Dotenv::{'create' . $type}($paths, $names, $shortCircuit, $fileEncoding);
65 1
            self::$vars = self::$dotenv->load();
66 1
            self::$initialized = true;
67
        }
68
        
69 1
        return self::$dotenv;
70
    }
71
    
72
    /**
73
     * Get .env directories
74
     * @return array
75
     */
76 1
    public static function getPaths()
77
    {
78 1
        if (is_null(self::$paths)) {
79 1
            self::$paths = [];
80 1
            foreach (['ENV_PATH', 'ROOT_PATH', 'APP_PATH'] as $constant) {
81 1
                if (defined($constant)) {
82
                    $path = constant($constant);
83
                    if (!is_null($path)) {
84
                        self::$paths [] = $constant === 'APP_PATH' ? dirname($path) : $path;
85
                        break;
86
                    }
87
                }
88
            }
89 1
            if (empty(self::$paths)) {
90 1
                self::$paths [] = getcwd();
91
            }
92
        }
93 1
        return self::$paths;
94
    }
95
    
96
    /**
97
     * Set .env directories
98
     * @param $paths
99
     * @return void
100
     */
101
    public static function setPaths(array $paths = null): void
102
    {
103
        self::$paths = $paths;
104
    }
105
    
106
    /**
107
     * Get .env file names
108
     * @return string|string[]
109
     */
110 1
    public static function getNames()
111
    {
112 1
        return self::$names;
113
    }
114
    
115
    /**
116
     * Set .env file names
117
     * @param $names
118
     * @return void
119
     */
120
    public static function setNames($names = null): void
121
    {
122
        self::$names = $names;
123
    }
124
    
125
    /**
126
     * Get Dotenv type
127
     * @return string
128
     */
129 1
    public static function getType()
130
    {
131 1
        return ucfirst(Helper::camelize(strtolower(self::$type ?? 'mutable')));
0 ignored issues
show
Bug introduced by
The method camelize() does not exist on Zemit\Support\Helper. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

131
        return ucfirst(Helper::/** @scrutinizer ignore-call */ camelize(strtolower(self::$type ?? 'mutable')));
Loading history...
132
    }
133
    
134
    /**
135
     * Set Dotenv type
136
     * @param string|null $type
137
     * @return void
138
     */
139
    public static function setType(?string $type = null): void
140
    {
141
        $domain = ['mutable', 'immutable', 'unsafe-mutable', 'unsafe-immutable'];
142
        self::$type = (!in_array(strtolower($type), $domain, true)) ? strtolower($type) : 'mutable';
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $string of strtolower() does only seem to accept string, 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

142
        self::$type = (!in_array(strtolower(/** @scrutinizer ignore-type */ $type), $domain, true)) ? strtolower($type) : 'mutable';
Loading history...
143
    }
144
    
145
    /**
146
     * @return boolean
147
     */
148 1
    public static function getShortCircuit(): bool
149
    {
150 1
        return self::$shortCircuit;
151
    }
152
    
153
    /**
154
     * @param bool $shortCircuit
155
     * @return void
156
     */
157
    public static function setShortCircuit(bool $shortCircuit = true): void
158
    {
159
        self::$shortCircuit = (bool)$shortCircuit;
160
    }
161
    
162
    /**
163
     * @return ?string
164
     */
165 1
    public static function getFileEncoding(): ?string
166
    {
167 1
        return self::$fileEncoding;
168
    }
169
    
170
    /**
171
     * @param ?string $fileEncoding
172
     * @return void
173
     */
174
    public static function setFileEncoding(?string $fileEncoding = null): void
175
    {
176
        self::$fileEncoding = $fileEncoding;
177
    }
178
    
179
    
180
    /**
181
     * @return Dotenv
182
     */
183 8
    public static function getDotenv()
184
    {
185 8
        return self::$dotenv ?? self::initialize();
186
    }
187
    
188
    /**
189
     * Get or set the environment variable
190
     * Ex. (SET): $this->SET_APPLICATION_ENV('production');
191
     * Ex. (SET): self::SET_APPLICATION_ENV('production');
192
     * Ex. (SET): Env::SET_APPLICATION_ENV('production');
193
     * Ex. (GET): $this->GET_APPLICATION_ENV('development');
194
     * Ex. (GET): self::GET_APPLICATION_ENV('development');
195
     * Ex. (GET): Env::GET_APPLICATION_ENV('development');
196
     * @param string $name key to get/set
197
     * @param mixed $arguments Default fallback value for get, or value to set for set
198
     * @return mixed Return void for set, and return the environment variable value, or default value for get
199
     */
200
    public static function call($name, $arguments)
201
    {
202
        $getSet = 'set';
203
        
204
        if (strpos($name, 'SET_') === 0) {
205
            $name = substr($name, 0, 4);
206
        }
207
        
208
        elseif (strpos($name, 'set') === 0) {
209
            $name = substr($name, 0, 3);
210
        }
211
        
212
        elseif (strpos($name, 'GET_') === 0) {
213
            $getSet = 'get';
214
            $name = substr($name, 0, 4);
215
        }
216
        
217
        elseif (strpos($name, 'get') === 0) {
218
            $getSet = 'get';
219
            $name = substr($name, 0, 3);
220
        }
221
        
222
        return self::$getSet($name, array_pop($arguments));
223
    }
224
    
225
    /**
226
     * Gets the value of an environment variable. Pass the $default for fallback value.
227
     * @param string $key Key to get
228
     * @param mixed $default Value to fallback if the key can't be found
229
     * @return mixed Return the environment variable value or the default value
230
     */
231 8
    public static function get(string $key, $default = null)
232
    {
233 8
        self::getDotenv();
234
        
235 8
        $ret = self::$vars[$key] ?? null;
236 8
        $ret ??= !is_string($default) && is_callable($default)? $default() : $default;
237
        
238 8
        if (is_string($ret)) {
239 8
            switch (strtolower($ret)) {
240 8
                case 'true':
241 8
                    $ret = true;
242 8
                    break;
243
                    
244 8
                case 'false':
245
                    $ret = false;
246
                    break;
247
                    
248 8
                case 'empty':
249
                    $ret = '';
250
                    break;
251
                    
252 8
                case 'null':
253
                    $ret = null;
254
                    break;
255
            }
256
        }
257
        
258 8
        return $ret;
259
    }
260
    
261
    /**
262
     * Set an environment variable
263
     * @param string $key Key to set
264
     * @param mixed $value Value to set
265
     */
266
    public static function set(string $key, $value)
267
    {
268
        self::$vars[$key] = $value;
269
        $_ENV[$key] = $value;
270
    }
271
    
272
    /**
273
     * Return the environment variable
274
     * @param string $name Env key to fetch
275
     * @return mixed Env value
276
     */
277
    public function __get(string $name)
278
    {
279
        return self::get($name);
280
    }
281
    
282
    /**
283
     * Set the environment variable
284
     * @param string $name Env key to set
285
     * @param mixed $value Value to set
286
     */
287
    public function __set(string $name, $value)
288
    {
289
        self::set($name, $value);
290
    }
291
    
292
    /**
293
     * Get or set the environment variable
294
     * Ex. (SET): self::SET_APPLICATION_ENV('production');
295
     * Ex. (SET): Env::SET_APPLICATION_ENV('production');
296
     * Ex. (GET): self::GET_APPLICATION_ENV('development');
297
     * Ex. (GET): Env::GET_APPLICATION_ENV('development');
298
     * @param $name string Key to get/set
299
     * @param $arguments array Default fallback value for get, or value to set for set
300
     * @return mixed Return void for set, and return the environment variable value, or default value for get
301
     */
302
    public static function __callStatic(string $name, array $arguments)
303
    {
304
        return self::call($name, $arguments);
305
    }
306
    
307
    /**
308
     * Get or set the environment variable
309
     * Ex. (SET): $this->SET_APPLICATION_ENV('production');
310
     * Ex. (GET): $this->GET_APPLICATION_ENV('development');
311
     * @param $name string Key to get/set
312
     * @param $arguments array Default fallback value for get, or value to set for set
313
     * @return mixed Return void for set, and return the environment variable value, or default value for get
314
     */
315
    public function __call(string $name, array $arguments)
316
    {
317
        return self::call($name, $arguments);
318
    }
319
}
320