EnvironmentLoader::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Filesystem;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Exception\InvalidPathException;
7
8
/**
9
 * Class EnvironmentLoader
10
 *
11
 * @package Tworzenieweb\SqlProvisioner\Filesystem
12
 */
13
class EnvironmentLoader implements EnvironmentLoaderInterface
14
{
15
    const MANDATORY_ENV_VARIABLES = [
16
        'DATABASE_USER',
17
        'DATABASE_PASSWORD',
18
        'DATABASE_NAME',
19
        'DATABASE_PORT',
20
        'DATABASE_HOST',
21
        'PROVISIONING_TABLE',
22
        'PROVISIONING_TABLE_CANDIDATE_NUMBER_COLUMN',
23
    ];
24
25
    /** @var bool */
26 1
    protected $skipMissing = false;
27
28 1
    /**
29 1
     * Do not rise an exception if .env file is missing on provided path
30 1
     *
31 1
     * @param bool $skipMissing
32 1
     */
33
    public function skipMissing(bool $skipMissing = true)
34
    {
35
        $this->skipMissing = $skipMissing;
36
    }
37
38
    /**
39
     * @param WorkingDirectory $currentDirectory
40
     */
41 View Code Duplication
    public function load(WorkingDirectory $currentDirectory)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $loader = new Dotenv($currentDirectory->getCurrentDirectoryAbsolute());
44
45
        try {
46
            $loader->load();
47
        } catch (InvalidPathException $e) {
48
            if ($this->skipMissing === false) {
49
                throw $e;
50
            }
51
52
            // assuming .env file is missing here and then just proceed to check global env variables
53
        }
54
55
        $loader->required(self::MANDATORY_ENV_VARIABLES)
56
               ->notEmpty();
57
    }
58
}
59