Passed
Push — 0.7.0 ( 33914c...86b65d )
by Alexander
03:01
created

Environment::getRepositoryCreator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 10
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Support;
24
25
use Syscodes\Dotenv\Repository\RepositoryCreator;
26
use Syscodes\Dotenv\Repository\Adapters\PutenvAdapter;
27
28
/**
29
 * Gets the adapter environment and value of an environment variable.
30
 * 
31
 * @author Alexander Campo <[email protected]>
32
 */
33
class Environment
34
{
35
    /**
36
     * Indicates if the putenv adapter is enabled.
37
     * 
38
     * @var bool $enabledPutenv
39
     */
40
    protected static $enabledPutenv = true;
41
42
    /**
43
     * The environment repository instance.
44
     * 
45
     * @var Syscodes\Dotenv\Repository\RepositoryCreator|null $repository
0 ignored issues
show
Bug introduced by
The type Syscodes\Support\Syscode...itory\RepositoryCreator was not found. Did you mean Syscodes\Dotenv\Repository\RepositoryCreator? If so, make sure to prefix the type with \.
Loading history...
46
     */
47
    protected static $repository;
48
49
    /**
50
     * Gets the value of an environment variable.
51
     * 
52
     * @param  string  $key
53
     * @param  mixed  $default
54
     * 
55
     * @return mixed
56
     */
57
    public static function get($key, $default = null)
58
    {
59
        $value = static::getRepositoryCreator()->get($key);
60
61
        if ($value === false)
62
        {
63
            return value($default);
64
        }
65
66
        // Handle any boolean values
67
        switch (strtolower($value))
68
        {
69
            case 'true':
70
            case '(true)':
71
                return true;
72
            case 'false':
73
            case '(false)':
74
                return false;
75
            case 'empty':
76
            case '(empty)':
77
                return '';
78
            case 'null':
79
            case '(null)':
80
                return null;
81
        }
82
        
83
        return $value;
84
    }
85
86
    /**
87
     * Get the environment repository instance.
88
     * 
89
     * @return  Syscodes\Dotenv\Repository\RepositoryCreator
90
     */
91
    public static function getRepositoryCreator()
92
    {
93
        if (null === static::$repository) {
94
            $repository = RepositoryCreator::createDefaultAdapters();
95
96
            if (static::$enabledPutenv) {
97
                $repository = $repository->addAdapter(PutenvAdapter::class);
98
            }
99
100
            static::$repository = $repository->make();
0 ignored issues
show
Documentation Bug introduced by
It seems like $repository->make() of type Syscodes\Dotenv\Repository\AdapterRepository is incompatible with the declared type Syscodes\Support\Syscode...\RepositoryCreator|null of property $repository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101
        }
102
103
        return static::$repository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::repository also could return the type Syscodes\Dotenv\Repository\AdapterRepository which is incompatible with the documented return type Syscodes\Support\Syscode...itory\RepositoryCreator.
Loading history...
104
    }
105
}