Completed
Push — master ( 6a6e14...e9047f )
by Julián
02:21
created

Environment::isActive()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 6
eloc 7
nc 4
nop 0
1
<?php
2
/**
3
 * Effortless maintenance management (http://juliangut.com/janitor)
4
 *
5
 * @link https://github.com/juliangut/janitor for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/janitor/blob/master/LICENSE
8
 */
9
10
namespace Janitor\Watcher;
11
12
use Janitor\Watcher as WatcherInterface;
13
14
/**
15
 * Environment variable check for maintenance status watcher.
16
 */
17
class Environment implements WatcherInterface
18
{
19
    /**
20
     * Environment variable.
21
     *
22
     * @var array
23
     */
24
    protected $vars;
25
26
    /**
27
     * @param array|string $vars
28
     * @param mixed|null   $value
29
     */
30
    public function __construct($vars, $value = null)
31
    {
32
        if (!is_array($vars)) {
33
            $vars = [$vars => $value];
34
        }
35
36
        foreach ($vars as $varName => $varValue) {
37
            $this->addVariable($varName, $varValue);
38
        }
39
    }
40
41
    /**
42
     * Set environment variable.
43
     *
44
     * @param string $var
45
     * @param mixed  $value
46
     *
47
     * @return $this
48
     */
49
    public function addVariable($var, $value = null)
50
    {
51
        if (trim($var) !== '') {
52
            $this->vars[trim($var)] = $value;
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function isActive()
62
    {
63
        foreach ($this->vars as $var => $value) {
64
            if ($value === null && getenv($var) !== false) {
65
                return true;
66
            } elseif ($value !== null && getenv($var) === $value) {
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
}
74