Completed
Push — master ( d573c8...0f6a93 )
by Florin
13:34
created

Environment::createSettingsFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Makehappen\AutoMinifier;
4
5
class Environment
6
{
7
    const ENV_FILE = 'env.json';
8
9
    const CONFIG_FILE = 'config.json';
10
11
    const SIGNATURE_FILE = 'signature.txt';
12
13
    protected $blnIsDev;
14
15
    protected $objEnv;
16
17
    public function __construct()
18
    {
19
    }
20
21
    /**
22
     * Set env to Dev
23
     *
24
     * @param bool $bln
25
     * @return $this
26
     */
27
    public function setDev($bln = true)
28
    {
29
        // set to provided value
30
        $this->blnIsDev = $bln;
31
        return $this;
32
    }
33
34
    /**
35
     * Determine if we are in development environment
36
     *
37
     * @return bool
38
     */
39
    public function isDevEnv()
40
    {
41
        // if set to dev stop here
42
        if ($this->blnIsDev) {
43
            return true;
44
        }
45
46
        if ($this->hasDevEnv()) {
47
            return true;
48
        }
49
50
        return false;
51
    }
52
53
    public function hasDevEnv()
54
    {
55
        // if not set abort
56
        if (empty($this->objEnv->environment)) {
57
            return false;
58
        }
59
60
        // determine if it's development
61
        return 'development' == $this->objEnv->environment;
62
    }
63
64
    public function createSettingsFile($strFilePath, $arrSettings)
65
    {
66
        // create file
67
        file_put_contents($strFilePath, json_encode($arrSettings, JSON_PRETTY_PRINT));
68
69
        return (object) $arrSettings;
70
    }
71
72
    public function getSettings($strFile = null, $arrSettings = [])
73
    {
74
        // crete and retur if does not exists
75
        if (!file_exists($strFile)) {
76
            return $this->createSettingsFile($strFile, $arrSettings);
77
        }
78
79
        // return contents
80
        return json_decode(file_get_contents($strFile));
81
    }
82
83
    public function setEnvironment($strFilesFolder = null)
84
    {
85
        // default environment
86
        $arrDefaultEnv = [
87
            'environment' => 'production'
88
        ];
89
90
        // get env file settings
91
        $this->objEnv = $this->getSettings($strFilesFolder  . '/' . self::ENV_FILE, $arrDefaultEnv);
92
    }
93
}
94