Completed
Pull Request — master (#11)
by Timothy
06:33 queued 03:39
created

Module::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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