Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

DotenvBootloader::loadEnvVariables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0119
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\DotEnv\Bootloader;
6
7
use Dotenv\Dotenv;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Boot\DirectoriesInterface;
10
use Spiral\Boot\EnvironmentInterface;
11
12
final class DotenvBootloader extends Bootloader
13
{
14
    private bool $init = false;
15
16 2
    public function init(
17
        DirectoriesInterface $dirs,
18
        EnvironmentInterface $env,
19
    ): void {
20 2
        $this->loadEnvVariables($dirs, $env);
21
    }
22
23 2
    public function loadEnvVariables(DirectoriesInterface $dirs, EnvironmentInterface $env): void
24
    {
25 2
        if ($this->init) {
26
            return;
27
        }
28
29 2
        $this->init = true;
30
31 2
        $dotenvPath = $env->get('DOTENV_PATH', $dirs->get('root') . '.env');
32
33 2
        if (!\file_exists($dotenvPath)) {
34 1
            return;
35
        }
36
37 1
        $path = \dirname($dotenvPath);
38 1
        $file = \basename($dotenvPath);
39
40 1
        foreach (Dotenv::createImmutable($path, $file)->load() as $key => $value) {
41 1
            $env->set($key, $value);
42
        }
43
    }
44
}
45