EnvironmentVariableStrategy::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 0
1
<?php
2
namespace Nubs\Sensible\Strategy;
3
4
use Habitat\Environment\Environment;
5
6
/**
7
 * Checks a given environment variable and returns its value if not null.
8
 */
9
class EnvironmentVariableStrategy implements StrategyInterface
10
{
11
    /** @type string The environment variable name. */
12
    private $_name;
13
14
    /** @type \Habitat\Environment\Environment The environment wrapper. */
15
    private $_environment;
16
17
    /**
18
     * Initialize the strategy that checks an environment variable for a
19
     * command.
20
     *
21
     * @param string $name The environment variable name to lookup.
22
     * @param \Habitat\Environment\Environment $environment The environment
23
     *     variable wrapper.  Defaults to null which just uses PHP's built-in
24
     *     getenv.
25
     */
26
    public function __construct($name, Environment $environment = null)
27
    {
28
        $this->_name = $name;
29
        $this->_environment = $environment;
30
    }
31
32
    /**
33
     * Returns the command as found in the environment variable.
34
     *
35
     * @return string|null The command, or null if the environment variable
36
     *     wasn't set.
37
     */
38
    public function get()
39
    {
40
        $result = $this->_environment ? $this->_environment->getenv($this->_name) : getenv($this->_name);
41
42
        return $result !== false ? $result : null;
43
    }
44
}
45