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

DotenvBootloader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
dl 0
loc 30
ccs 12
cts 13
cp 0.9231
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A loadEnvVariables() 0 19 4
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