Environment   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 150
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDev() 0 6 1
A isDevEnv() 0 13 3
A hasDevEnv() 0 10 2
A createSettingsFile() 0 7 1
A getSettings() 0 10 2
A getConfig() 0 10 1
A setEnvironment() 0 10 1
1
<?php
2
3
namespace makehappen\autominifier;
4
5
/**
6
 * Class Environment
7
 * @package Makehappen\AutoMinifier
8
 */
9
class Environment
10
{
11
    /**
12
     * Environment file
13
     */
14
    const ENV_FILE = 'env.json';
15
16
    /**
17
     * Configuration file
18
     */
19
    const CONFIG_FILE = 'config.json';
20
21
    /**
22
     * Signature file for cache bust
23
     */
24
    const SIGNATURE_FILE = 'signature.txt';
25
26
    /**
27
     * Is Development
28
     * @var
29
     */
30
    protected $blnIsDev;
31
32
    /**
33
     * Environment Settings
34
     * @var
35
     */
36
    protected $objEnvironmentSettings;
37
38
    /**
39
     * Environment constructor.
40
     */
41 3
    public function __construct()
42
    {
43 3
    }
44
45
    /**
46
     * Set env to Dev
47
     *
48
     * @param bool $bln
49
     * @return $this
50
     */
51 3
    public function setDev($bln = true)
52
    {
53
        // set to provided value
54 3
        $this->blnIsDev = $bln;
55 3
        return $this;
56
    }
57
58
    /**
59
     * Determine if we are in development environment
60
     *
61
     * @return bool
62
     */
63 3
    public function isDevEnv()
64
    {
65
        // if set to dev stop here
66 3
        if ($this->blnIsDev) {
67 3
            return true;
68
        }
69
70
        if ($this->hasDevEnv()) {
71
            return true;
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * Determine if it has develompent env set
79
     *
80
     * @return bool
81
     */
82
    public function hasDevEnv()
83
    {
84
        // if not set abort
85
        if (empty($this->objEnvironmentSettings->environment)) {
86
            return false;
87
        }
88
89
        // determine if it's development
90
        return 'development' == $this->objEnvironmentSettings->environment;
91
    }
92
93
    /**
94
     * Create a settings file
95
     *
96
     * @param $strFilePath
97
     * @param $arrSettings
98
     * @return object
99
     */
100 3
    public function createSettingsFile($strFilePath, $arrSettings)
101
    {
102
        // create file
103 3
        file_put_contents($strFilePath, json_encode($arrSettings, JSON_PRETTY_PRINT));
104
105 3
        return (object) $arrSettings;
106
    }
107
108
    /**
109
     * Get settings
110
     *
111
     * @param null $strFile
112
     * @param array $arrSettings
113
     * @return mixed|object
114
     */
115 3
    public function getSettings($strFile = null, $arrSettings = [])
116
    {
117
        // crete and retur if does not exists
118 3
        if (!file_exists($strFile)) {
119 3
            return $this->createSettingsFile($strFile, $arrSettings);
120
        }
121
122
        // return contents
123
        return json_decode(file_get_contents($strFile));
124
    }
125
126
    /**
127
     * Get Config
128
     *
129
     * @param $strFilesFolder
130
     * @return mixed|object
131
     */
132 3
    public function getConfig($strFilesFolder)
133
    {
134
        // default config
135
        $arrDefaultConfig = [
136 3
            'files' => []
137 3
        ];
138
139
        // return config
140 3
        return $this->getSettings($strFilesFolder . '/' . Environment::CONFIG_FILE, $arrDefaultConfig);
141
    }
142
143
    /**
144
     * Set environment
145
     *
146
     * @param null $strFilesFolder
147
     */
148 3
    public function setEnvironment($strFilesFolder = null)
149
    {
150
        // default environment
151
        $arrDefaultEnv = [
152
            'environment' => 'production'
153 3
        ];
154
155
        // get env file settings
156 3
        $this->objEnvironmentSettings = $this->getSettings($strFilesFolder  . '/' . self::ENV_FILE, $arrDefaultEnv);
157 3
    }
158
}
159