Detector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 6 2
A detect() 0 8 2
1
<?php
2
3
namespace PassGenerator;
4
5
/**
6
 * Detector class which should detect a environment where the script is running.
7
 * 
8
 * @property string $env detected environment
9
 * 
10
 * @author Lachezar Mihaylov <[email protected]>
11
 * @license https://github.com/lmihaylov2512/pass-generator/blob/master/LICENSE.md MIT License
12
 */
13
class Detector
14
{
15
    /**
16
     * @var array data like environment and console arguments
17
     */
18
    protected $data;
19
    /**
20
     * @var array list with all allowed environments
21
     */
22
    private $_envs = [
23
        'console' => 'cli',
24
        'browser' => 'server',
25
    ];
26
    
27
    /**
28
     * Initialize current environment and set some useful data.
29
     */
30
    public function __construct()
31
    {
32
        $this->detect();
33
    }
34
    
35
    /**
36
     * Overloading method for getting element value from data array.
37
     * 
38
     * @param string $name data storage key name
39
     * @return mixed|null the value of data key
40
     */
41
    public function __get($name)
42
    {
43
        if (array_key_exists($name, $this->data)) {
44
            return $this->data[$name];
45
        }
46
    }
47
    
48
    /**
49
     * Detect an environment in which is running the script.
50
     * 
51
     * @return void
52
     */
53
    public function detect()
54
    {
55
        if (php_sapi_name() === $this->_envs['console']) {
56
            $this->data['env'] = $this->_envs['console'];
57
        } else {
58
            $this->data['env'] = $this->_envs['browser'];
59
        }
60
    }
61
}
62