Completed
Pull Request — master (#9)
by
unknown
15:39
created

Module::loadEnvironmentVariables()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 3
nop 0
crap 30
1
<?php
2
3
namespace Abacaphiliac\ZendPhpDotEnv;
4
5
use Zend\ModuleManager\ModuleEvent;
6
use Zend\ModuleManager\ModuleManager;
7
8
class Module
9
{
10
    /** @var  string */
11
    private $constant = 'APPLICATION_PATH';
12
13
    /** @var  string */
14
    private $variable = 'APPLICATION_PATH';
15
    
16
    /** @var  string */
17
    private $file = '.env';
18
19
    /**
20
     * Module constructor.
21
     * @param string $constant
22
     * @param string $variable
23
     * @param string $file
24
     */
25
    public function __construct($constant = null, $variable = null, $file = null)
26
    {
27
        if ($constant) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $constant of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $this->constant = $constant;
29
        }
30
31
        if ($variable) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $variable of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32
            $this->variable = $variable;
33
        }
34
35
        if ($file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
36
            $this->file = $file;
37
        }
38
    }
39
40
    /**
41
     * @param ModuleManager $moduleManager
42
     */
43
    public function init(ModuleManager $moduleManager)
44
    {
45
        $events = $moduleManager->getEventManager();
46
47
        $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'loadEnvironmentVariables'), -1);
48
    }
49
50
    /**
51
     * @return mixed[]
52
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException
53
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException
54
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException
55
     */
56
    public function loadEnvironmentVariables()
57
    {
58
        // Load from application-path defined by constant.
59
        if ($this->constant && defined($this->constant)) {
60
            return DotEnvLoader::loadFromConstant($this->constant, $this->file);
61
        }
62
63
        // Load from application-path defined by environment-variable.
64
        if ($this->variable && DotEnvLoader::hasEnvironmentVariable($this->variable)) {
65
            return DotEnvLoader::loadFromEnvironmentVariable($this->variable, $this->file);
66
        }
67
        
68
        // Load from working directory. ZF2 applications change the working directory to the application root.
69
        return DotEnvLoader::loadFromWorkingDirectory($this->file);
70
    }
71
}
72