Environment::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.125

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
ccs 4
cts 8
cp 0.5
rs 9.4286
cc 3
eloc 6
nc 4
nop 2
crap 4.125
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