|
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
|
|
View Code Duplication |
public function activatedServices() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$activatedServices = []; |
|
39
|
|
|
|
|
40
|
|
|
foreach ($this->services as $service) { |
|
41
|
|
|
if ($this->get($service, 'is_active') === true) { |
|
42
|
|
|
array_push($activatedServices, $service); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $activatedServices; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function generateDefaultConfigurationFile() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->config = [ |
|
52
|
|
|
'timer' => [ |
|
53
|
|
|
'primary_service' => 'toggl', |
|
54
|
|
|
'is_running' => false, |
|
55
|
|
|
'toggl_id' => null, |
|
56
|
|
|
'harvest_id' => null, |
|
57
|
|
|
'description' => '', |
|
58
|
|
|
], |
|
59
|
|
|
'toggl' => [ |
|
60
|
|
|
'is_active' => true, |
|
61
|
|
|
'api_token' => '', |
|
62
|
|
|
], |
|
63
|
|
|
'harvest' => [ |
|
64
|
|
|
|
|
65
|
|
|
'is_active' => true, |
|
66
|
|
|
'domain' => '', |
|
67
|
|
|
'api_token' => '', |
|
68
|
|
|
], |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
$this->save(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $section |
|
76
|
|
|
* @param null $param |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function get($section = null, $param = null) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($section === null) { |
|
82
|
|
|
return $this->config; |
|
83
|
|
|
} elseif ($param === null) { |
|
84
|
|
|
return $this->config[$section]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->config[$section][$param]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return boolean |
|
92
|
|
|
*/ |
|
93
|
|
|
public function isConfigured() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->config !== null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function runningServices() |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$services = []; |
|
104
|
|
|
|
|
105
|
|
|
foreach ($this->activatedServices() as $service) { |
|
106
|
|
|
if ($this->get('timer', $service . '_id') !== null) { |
|
107
|
|
|
array_push($services, $service); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $services; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param $section |
|
116
|
|
|
* @param $param |
|
117
|
|
|
* @param $value |
|
118
|
|
|
*/ |
|
119
|
|
|
public function update($section, $param, $value) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->config[$section][$param] = $value; |
|
122
|
|
|
$this->save(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
private function load($filename) |
|
129
|
|
|
{ |
|
130
|
|
|
if (file_exists($filename)) { |
|
131
|
|
|
$this->config = json_decode(file_get_contents($filename), true); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function save() |
|
136
|
|
|
{ |
|
137
|
|
|
$workflowDir = getenv('alfred_workflow_data'); |
|
138
|
|
|
$configFile = $workflowDir . '/config.json'; |
|
139
|
|
|
|
|
140
|
|
|
if (file_exists($workflowDir) === false) { |
|
141
|
|
|
mkdir($workflowDir); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
file_put_contents($configFile, json_encode($this->config, JSON_PRETTY_PRINT)); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.