Completed
Pull Request — master (#11)
by Timothy
03:13
created

Module   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 74
rs 10
ccs 21
cts 21
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A getConfig() 0 4 1
B loadEnvironmentVariables() 0 15 5
A init() 0 6 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 4
     */
27
    public function __construct($constant = null, $variable = null, $file = null)
28 4
    {
29 1
        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
        }
32 4
33 1
        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
        }
36 4
37 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...
38 4
            $this->file = $file;
39 4
        }
40
    }
41
42
    /**
43
     * Returns configuration to merge with application configuration
44 1
     *
45
     * @return array|\Traversable
46 1
     */
47
    public function getConfig()
48 1
    {
49 1
        return require __DIR__ . '/../../config/module.config.php';
50
    }
51
52
    /**
53
     * @param ModuleManagerInterface $moduleManager
54
     */
55
    public function init(ModuleManagerInterface $moduleManager)
56
    {
57 4
        $events = $moduleManager->getEventManager();
58
59
        $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'loadEnvironmentVariables'), -1);
60 4
    }
61 1
62
    /**
63
     * @return mixed[]
64
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException
65 3
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException
66 1
     * @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException
67
     */
68
    public function loadEnvironmentVariables()
69
    {
70 2
        // Load from application-path defined by constant.
71
        if ($this->constant && defined($this->constant)) {
72
            return DotEnvLoader::loadFromConstant($this->constant, $this->file);
73
        }
74
75
        // Load from application-path defined by environment-variable.
76
        if ($this->variable && DotEnvLoader::hasEnvironmentVariable($this->variable)) {
77
            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
        return DotEnvLoader::loadFromWorkingDirectory($this->file);
82
    }
83
}
84