Env   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 9 2
A setEnvValue() 0 8 2
1
<?php
2
3
namespace Consolidation\Cgr;
4
5
class Env
6
{
7
    protected $env;
8
9
    public function __construct($env)
10
    {
11
        $this->env = $env;
12
    }
13
14
    /**
15
     * Apply a set of environment variables; return the original
16
     * value of any value that is set, to avoid polluting the environment.
17
     *
18
     * @return Env
19
     */
20
    public function apply()
21
    {
22
        $orig = array();
23
        foreach ($this->env as $key => $value) {
24
            $orig[$key] = getenv($key);
25
            static::setEnvValue($key, $value);
26
        }
27
        return new Env($orig);
28
    }
29
30
    /**
31
     * Set or un-set one environment variable
32
     *
33
     * @param string $key The environment variable to set or unset
34
     * @param mixed $value THe value to set the variable to, or false to unset.
35
     */
36
    protected static function setEnvValue($key, $value)
37
    {
38
        if ($value === false) {
39
            putenv($key);
40
            return;
41
        }
42
        putenv("$key=$value");
43
    }
44
}
45