Passed
Push — master ( af1281...14bd9e )
by Sebastian
02:37
created

Env   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 54
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 4 2
B get() 0 16 5
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Helper;
13
14
use Closure;
15
16
/**
17
 * Env Helper.
18
 */
19
class Env
20
{
21
22
    /**
23
     * @var array Matches for particula values
24
     */
25
    private static $valuesMatches = [
26
        'true' => true,
27
        '(true)' => true,
28
        'false' => false,
29
        '(false)' => false,
30
        'empty' => '',
31
        '(empty)' => '',
32
        'null' => null,
33
        '(null)' => null,
34
    ];
35
    
36
    /**
37
     * Return the default value of the given value.
38
     *
39
     * @param mixed $value
40
     *
41
     * @return mixed
42
     */
43 1
    private static function value($value)
44
    {
45 1
        return $value instanceof Closure ? $value() : $value;
46
    }
47
48
    /**
49
     * Gets the value of an environment variable.
50
     *
51
     * @param string $key
52
     * @param mixed  $default
53
     *
54
     * return mixed
55
     */
56 14
    public static function get(string $key, $default = null)
57
    {
58 14
        if (($value = getenv($key)) === false) {
59 1
            return self::value($default);
60
        }
61
        
62 13
        if (array_key_exists(strtolower($value), self::$valuesMatches)) {
63 8
            return self::$valuesMatches[strtolower($value)];
64
        }
65
        
66 5
        if (strlen($value) > 1 && Str::startsEndsWith($value, ['"', '\''])) {
67 3
            return substr($value, 1, -1);
68
        }
69
        
70 2
        return $value;
71
    }
72
}
73