Completed
Push — master ( 700cfb...d47114 )
by Guillaume
02:09
created

Config::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * Config
5
 */
6
class Config
7
{
8
    /**
9
     * @var mixed
10
     */
11
    private $config = [];
12
13
    /**
14
     * @param $filename
15
     */
16
    public function __construct($filename = null)
17
    {
18
19
        if ($filename !== null) {
20
            $this->load($filename);
21
        }
22
23
    }
24
25
    public function generateDefaultConfigurationFile()
26
    {
27
        $this->config = [
28
            'workflow' => [
29
                'is_timer_running'  => false,
30
                'timer_toggl_id'    => null,
31
                'timer_harvest_id'  => null,
32
                'timer_description' => '',
33
            ],
34
            'toggl'    => [
35
                'is_active'          => true,
36
                'api_token'          => '',
37
                'default_project_id' => '',
38
                'default_tags'       => '',
39
            ],
40
            'harvest'  => [
41
42
                'is_active'          => true,
43
                'domain'             => '',
44
                'api_token'          => '',
45
                'default_project_id' => '',
46
                'default_task_id'    => '',
47
            ],
48
        ];
49
50
        $this->save();
51
    }
52
53
    /**
54
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     */
56
    public function get($section = null, $param = null)
57
    {
58
59
        if ($section === null) {
60
            $res = $this->config;
61
        } elseif ($param === null) {
62
            $res = $this->config[$section];
63
        } else {
64
            $res = $this->config[$section][$param];
65
        }
66
67
        return $res;
68
    }
69
70
    /**
71
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
     */
73
    public function update($section, $param, $value)
74
    {
75
        $this->config[$section][$param] = $value;
76
        $this->save();
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    private function load($filename)
83
    {
84
85
        if (file_exists($filename)) {
86
            $this->config = json_decode(file_get_contents($filename), true);
87
        }
88
89
    }
90
91
    private function save()
92
    {
93
        $workflowDir = getenv('alfred_workflow_data');
94
        $configFile = $workflowDir . '/config.json';
95
96
        if (file_exists($workflowDir) === false) {
97
            mkdir($workflowDir);
98
        }
99
100
        file_put_contents($configFile, json_encode($this->config, JSON_PRETTY_PRINT));
101
    }
102
103
}
104