Completed
Push — master ( 4d43de...d186d6 )
by Joram van den
04:11
created

Ajde_Environment   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 5 2
A current() 0 5 1
A environment() 0 4 1
B autoDetect() 0 16 5
1
<?php
2
3
class Ajde_Environment extends Ajde_Object_Singleton
4
{
5
    const ENV_DEFAULT = 'production';
6
7
    /**
8
     * @var string
9
     */
10
    private $environment;
11
12
    /**
13
     * Environment <-> network map.
14
     *
15
     * @author http://en.wikipedia.org/wiki/Private_network
16
     * @var array
17
     */
18
    public static $networks = [
19
        'dev' => [
20
            '/::1/',
21
            '/127\.0\.0\.1/',
22
            '/10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',
23
            '/172\.[1-3][0-9]\.[0-9]{1,3}\.[0-9]{1,3}/',
24
            '/192\.168\.[0-9]{1,3}\.[0-9]{1,3}/',
25
        ]
26
    ];
27
28
    /**
29
     * TODO
30
     */
31
    public function __construct()
32
    {
33
        $this->environment = $this->autoDetect();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->autoDetect() can also be of type integer. However, the property $environment is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
34
    }
35
36
    /**
37
     * TODO
38
     *
39
     * @return Ajde_Environment
40
     */
41
    public static function getInstance()
42
    {
43
        static $instance;
44
        return $instance === null ? $instance = new self : $instance;
45
    }
46
47
    /**
48
     * TODO
49
     *
50
     * @return string
51
     */
52
    public static function current()
53
    {
54
        $instance = self::getInstance();
55
        return $instance->environment();
56
    }
57
58
    /**
59
     * TODO
60
     *
61
     * @return string
62
     */
63
    public function environment()
64
    {
65
        return $this->environment;
66
    }
67
68
    /**
69
     * TODO
70
     *
71
     * @return string
72
     */
73
    private function autoDetect()
74
    {
75
        if (file_exists(LOCAL_ROOT . '.env')) {
76
            return trim(file_get_contents(LOCAL_ROOT . '.env'));
77
        }
78
79
        foreach (self::$networks as $environment => $ips) {
80
            foreach ($ips as $pattern) {
81
                if (preg_match($pattern, $_SERVER['SERVER_ADDR'])) {
82
                    return $environment;
83
                }
84
            }
85
        }
86
87
        return self::ENV_DEFAULT;
88
    }
89
}
90