Environment::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4286
cc 3
eloc 5
nc 4
nop 2
crap 12
1
<?php namespace GameScan\Core\Tools;
2
3
use Dotenv;
4
use GameScan\Core\Exceptions\EnvironmentException;
5
6
class Environment
7
{
8
9
    private $path = null;
10
    private $hasLoadValues = false;
11
12 2
    public function __construct($path = null, $filename = ".env")
13
    {
14 2
        if ($path === null) {
15
            $path = __DIR__ . '/../../../../..';
16
        }
17
18 2
        if (!file_exists($path . DIRECTORY_SEPARATOR . $filename)) {
19 2
            throw new EnvironmentException($path . DIRECTORY_SEPARATOR . $filename . " is not exist. Please create an environment file before using this feature");
20
        }
21
22
        $this->path = $path;
23
    }
24
25
26
    public function load()
27
    {
28
        Dotenv::load($this->path);
29
        $this->hasLoadValues = true;
30
    }
31
32
    public function get($key, $defaultValue = false)
33
    {
34
        if (!$this->hasLoadValues) {
35
            $this->load();
36
        }
37
        $value = getenv($key);
38
        return $value !== false ? $value : $defaultValue;
39
    }
40
}
41