Module::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 8
nop 3
crap 4
1
<?php
2
3
namespace Abacaphiliac\ZendPhpDotEnv;
4
5
use Zend\ModuleManager\Feature\InitProviderInterface;
6
use Zend\ModuleManager\ModuleEvent;
7
use Zend\ModuleManager\ModuleManagerInterface;
8
9
class Module implements InitProviderInterface
10
{
11
    /** @var  string */
12
    private $constant = 'APPLICATION_PATH';
13
14
    /** @var  string */
15
    private $variable = 'APPLICATION_PATH';
16
    
17
    /** @var  string */
18
    private $file = '.env';
19
20
    /**
21
     * Module constructor.
22
     * @param string $constant
23
     * @param string $variable
24
     * @param string $file
25
     */
26 4
    public function __construct($constant = null, $variable = null, $file = null)
27
    {
28 4
        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...
29 1
            $this->constant = $constant;
30 1
        }
31
32 4
        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...
33 1
            $this->variable = $variable;
34 1
        }
35
36 4
        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...
37 4
            $this->file = $file;
38 4
        }
39 4
    }
40
41
    /**
42
     * @param ModuleManagerInterface $moduleManager
43
     */
44 1
    public function init(ModuleManagerInterface $moduleManager)
45
    {
46 1
        $events = $moduleManager->getEventManager();
47
48 1
        $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'loadEnvironmentVariables'), -1);
49 1
    }
50
51
    /**
52
     * @return mixed[]
53
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException
54
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException
55
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException
56
     */
57 4
    public function loadEnvironmentVariables()
58
    {
59
        // Load from application-path defined by constant.
60 4
        if ($this->constant && defined($this->constant)) {
61 1
            return DotEnvLoader::loadFromConstant($this->constant, $this->file);
62
        }
63
64
        // Load from application-path defined by environment-variable.
65 3
        if ($this->variable && DotEnvLoader::hasEnvironmentVariable($this->variable)) {
66 1
            return DotEnvLoader::loadFromEnvironmentVariable($this->variable, $this->file);
67
        }
68
        
69
        // Load from working directory. ZF2 applications change the working directory to the application root.
70 2
        return DotEnvLoader::loadFromWorkingDirectory($this->file);
71
    }
72
}
73