Completed
Push — master ( 60bcf6...cc7b46 )
by Guillaume
02:09
created

Config::generateDefaultConfigurationFile()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace AlfredTime;
4
5
/**
6
 * Config
7
 */
8
class Config
9
{
10
    /**
11
     * @var mixed
12
     */
13
    private $config = [];
14
15
    /**
16
     * @var array
17
     */
18
    private $currentImplementation = [
19
        'start'         => ['toggl'],
20
        'start_default' => ['toggl', 'harvest'],
21
        'stop'          => ['toggl', 'harvest'],
22
        'delete'        => ['toggl'],
23
        'get_projects'  => ['toggl'],
24
        'get_tags'      => ['toggl'],
25
        'get_timers'    => ['toggl'],
26
        'sync_data'     => ['toggl'],
27
    ];
28
29
    /**
30
     * @var array
31
     */
32
    private $services = [
33
        'toggl',
34
        'harvest',
35
    ];
36
37
    /**
38
     * @param $filename
39
     */
40
    public function __construct($filename = null)
41
    {
42
        if ($filename !== null) {
43
            $this->load($filename);
44
        }
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50
    public function activatedServices()
51
    {
52
        $activatedServices = [];
53
54
        foreach ($this->services as $service) {
55
            if ($this->isServiceActive($service) === true) {
56
                array_push($activatedServices, $service);
57
            }
58
        }
59
60
        return $activatedServices;
61
    }
62
63
    public function generateDefaultConfigurationFile()
64
    {
65
        $this->config = [
66
            'workflow' => [
67
                'is_timer_running'  => false,
68
                'timer_toggl_id'    => null,
69
                'timer_harvest_id'  => null,
70
                'timer_description' => '',
71
            ],
72
            'toggl'    => [
73
                'is_active'          => true,
74
                'api_token'          => '',
75
                'default_project_id' => '',
76
                'default_tags'       => '',
77
            ],
78
            'harvest'  => [
79
80
                'is_active'          => true,
81
                'domain'             => '',
82
                'api_token'          => '',
83
                'default_project_id' => '',
84
                'default_task_id'    => '',
85
            ],
86
        ];
87
88
        $this->save();
89
    }
90
91
    /**
92
     * @param  $section
93
     * @param  null       $param
94
     * @return mixed
95
     */
96
    public function get($section = null, $param = null)
97
    {
98
        if ($section === null) {
99
            $res = $this->config;
100
        } elseif ($param === null) {
101
            $res = $this->config[$section];
102
        } else {
103
            $res = $this->config[$section][$param];
104
        }
105
106
        return $res;
107
    }
108
109
    /**
110
     * @param  string  $feature
111
     * @return mixed
112
     */
113
    public function implementedServicesForFeature($feature = null)
114
    {
115
        $services = [];
116
117
        if (isset($this->currentImplementation[$feature]) === true) {
118
            $services = $this->currentImplementation[$feature];
119
        }
120
121
        return $services;
122
    }
123
124
    /**
125
     * @return boolean
126
     */
127
    public function isConfigured()
128
    {
129
        return $this->config !== null;
130
    }
131
132
    /**
133
     * @param  $service
134
     * @return mixed
135
     */
136
    public function isServiceActive($service)
137
    {
138
        return $this->get($service, 'is_active');
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144
    public function servicesToUndo()
145
    {
146
        $services = [];
147
148
        foreach ($this->activatedServices() as $service) {
149
            if ($this->get('workflow', 'timer_' . $service . '_id') !== null) {
150
                array_push($services, $service);
151
            }
152
        }
153
154
        return $services;
155
    }
156
157
    /**
158
     * @param $section
159
     * @param $param
160
     * @param $value
161
     */
162
    public function update($section, $param, $value)
163
    {
164
        $this->config[$section][$param] = $value;
165
        $this->save();
166
    }
167
168
    /**
169
     * @return mixed
170
     */
171
    private function load($filename)
172
    {
173
        if (file_exists($filename)) {
174
            $this->config = json_decode(file_get_contents($filename), true);
175
        }
176
    }
177
178
    private function save()
179
    {
180
        $workflowDir = getenv('alfred_workflow_data');
181
        $configFile = $workflowDir . '/config.json';
182
183
        if (file_exists($workflowDir) === false) {
184
            mkdir($workflowDir);
185
        }
186
187
        file_put_contents($configFile, json_encode($this->config, JSON_PRETTY_PRINT));
188
    }
189
190
    /**
191
     * @return mixed
192
     */
193
    public function getTimerDescription()
194
    {
195
        return $this->get('workflow', 'timer_description');
196
    }
197
198
        /**
199
     * @return boolean
200
     */
201
    public function hasTimerRunning()
202
    {
203
        return $this->get('workflow', 'is_timer_running') === true;
204
    }
205
}
206