Env::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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