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

Environment   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 11
c 5
b 1
f 0
lcom 1
cbo 0
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A addVariable() 0 8 2
B isActive() 0 12 6
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