Completed
Push — master ( 492e98...69b246 )
by Basil
03:16
created

BaseDevCommand::readConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace luya\dev;
4
5
use luya\console\Command;
6
use luya\helpers\FileHelper;
7
use yii\helpers\Json;
8
9
/**
10
 * BaseDevCommand Controller.
11
 * 
12
 * Provides the IO for the configuration storage.
13
 * 
14
 * @author Basil Suter <[email protected]>
15
 */
16
class BaseDevCommand extends Command
17
{
18
    public $configFile = '@devenvroot/cachestorage.json';
19
    
20
    public function readConfig()
21
    {
22
        $data = FileHelper::getFileContent($this->configFile);
23
        
24
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
            return Json::decode($data);
26
        }
27
        
28
        return false;
29
    }
30
    
31
    public function getConfig($key)
32
    {
33
        $config = $this->readConfig();
34
        
35
        return isset($config[$key]) ? $config[$key] : false;
36
    }
37
    
38
    public function saveConfig($key, $value)
39
    {
40
        $content = $this->readConfig();
41
     
42
        if (!$content) {
43
            $content = [];
44
        }
45
        
46
        $content[$key] = $value;
47
        
48
        $save = FileHelper::writeFile($this->configFile, Json::encode($content));
49
        
50
        if (!$save) {
51
            return $this->outputError("Unable to find config file " . $this->configFile. ". Please create and provide Permissions.");
52
        }
53
        
54
        return $value;
55
    }
56
}