Completed
Push — master ( ba39f4...421481 )
by Florin
01:50
created

Environment   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDev() 0 5 1
A isDevEnv() 0 19 4
A isDevDomain() 0 5 1
A isLocalhost() 0 9 2
1
<?php
2
3
namespace Makehappen\AutoMinifier;
4
5
class Environment
6
{
7
    protected $blnIsDev;
8
9
    public function __construct()
10
    {
11
    }
12
13
    /**
14
     * Set env to Dev
15
     *
16
     * @param bool $bln
17
     * @return $this
18
     */
19
    public function setDev($bln = true)
20
    {
21
        $this->blnIsDev = $bln;
22
        return $this;
23
    }
24
25
    /**
26
     * Determine if we are in development environment
27
     *
28
     * @return bool
29
     */
30
    public function isDevEnv()
31
    {
32
        // if set to dev stop here
33
        if ($this->blnIsDev) {
34
            return true;
35
        }
36
37
        // domain extension is .dev
38
        if ($this->isDevDomain()) {
39
            return true;
40
        }
41
42
        // localhost
43
        if ($this->isLocalhost()) {
44
            return true;
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * Determine if it's .dev domain
52
     *
53
     * @return bool
54
     */
55
    public function isDevDomain()
0 ignored issues
show
Coding Style introduced by
isDevDomain uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
56
    {
57
        $arrDomain = explode('.', $_SERVER['HTTP_HOST']);
58
        return array_pop($arrDomain) == 'dev';
59
    }
60
61
    /**
62
     * Is is localhost
63
     *
64
     * @return bool
65
     */
66
    public function isLocalhost()
0 ignored issues
show
Coding Style introduced by
isLocalhost uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
67
    {
68
        // must have HTTP_HOST set
69
        if (empty($_SERVER['HTTP_HOST'])) {
70
            return false;
71
        }
72
73
        return $_SERVER['HTTP_HOST'] == 'localhost';
74
    }
75
}