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 $services = [ |
19
|
|
|
'toggl', |
20
|
|
|
'harvest', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $filename |
25
|
|
|
*/ |
26
|
|
|
public function __construct($filename = null) |
27
|
|
|
{ |
28
|
|
|
if ($filename !== null) { |
29
|
|
|
$this->load($filename); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function activatedServices() |
37
|
|
|
{ |
38
|
|
|
return array_filter($this->services, function ($service) { |
39
|
|
|
return $this->get($service, 'is_active') === true; |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function generateDefaultConfigurationFile() |
44
|
|
|
{ |
45
|
|
|
$this->config = [ |
46
|
|
|
'timer' => [ |
47
|
|
|
'primary_service' => 'toggl', |
48
|
|
|
'is_running' => false, |
49
|
|
|
'toggl_id' => null, |
50
|
|
|
'harvest_id' => null, |
51
|
|
|
'description' => '', |
52
|
|
|
], |
53
|
|
|
'toggl' => [ |
54
|
|
|
'is_active' => true, |
55
|
|
|
'api_token' => '', |
56
|
|
|
], |
57
|
|
|
'harvest' => [ |
58
|
|
|
'is_active' => true, |
59
|
|
|
'domain' => '', |
60
|
|
|
'api_token' => '', |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$this->save(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $section |
69
|
|
|
* @param null $param |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function get($section = null, $param = null) |
73
|
|
|
{ |
74
|
|
|
if ($section === null) { |
75
|
|
|
return $this->config; |
76
|
|
|
} elseif ($param === null) { |
77
|
|
|
return $this->config[$section]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->config[$section][$param]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
public function isConfigured() |
87
|
|
|
{ |
88
|
|
|
return empty($this->config) === false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function runningServices() |
95
|
|
|
{ |
96
|
|
|
return array_filter($this->activatedServices(), function ($service) { |
97
|
|
|
return $this->get('timer', $service . '_id') !== null; |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $section |
103
|
|
|
* @param $param |
104
|
|
|
* @param $value |
105
|
|
|
*/ |
106
|
|
|
public function update($section, $param, $value) |
107
|
|
|
{ |
108
|
|
|
$this->config[$section][$param] = $value; |
109
|
|
|
$this->save(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
private function load($filename) |
116
|
|
|
{ |
117
|
|
|
if (file_exists($filename)) { |
118
|
|
|
$this->config = json_decode(file_get_contents($filename), true); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function save() |
123
|
|
|
{ |
124
|
|
|
$workflowDir = getenv('alfred_workflow_data'); |
125
|
|
|
$configFile = $workflowDir . '/config.json'; |
126
|
|
|
|
127
|
|
|
if (file_exists($workflowDir) === false) { |
128
|
|
|
mkdir($workflowDir); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
file_put_contents($configFile, json_encode($this->config, JSON_PRETTY_PRINT)); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|