|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlfredTime; |
|
4
|
|
|
|
|
5
|
|
|
use AlfredTime\Toggl; |
|
6
|
|
|
use AlfredTime\Config; |
|
7
|
|
|
use AlfredTime\Harvest; |
|
8
|
|
|
|
|
9
|
|
|
class Timer |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var mixed |
|
13
|
|
|
*/ |
|
14
|
|
|
private $config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
private $harvest; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
private $toggl; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Config $config |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Config $config, Toggl $toggl, Harvest $harvest) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->config = $config; |
|
32
|
|
|
$this->harvest = $harvest; |
|
33
|
|
|
$this->toggl = $toggl; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $timerData |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function delete(array $timerData = []) |
|
41
|
|
|
{ |
|
42
|
|
|
$res = []; |
|
43
|
|
|
|
|
44
|
|
|
if (empty($timerData) === true) { |
|
45
|
|
|
return []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
foreach ($timerData as $service => $id) { |
|
49
|
|
|
$res[$service] = $this->deleteServiceTimer($service, $id); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $res; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $service |
|
57
|
|
|
* @param $timerId |
|
58
|
|
|
* @return boolean |
|
59
|
|
|
*/ |
|
60
|
|
|
public function deleteServiceTimer($service, $timerId) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->$service->deleteTimer($timerId) === false) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($timerId === $this->getProperty($service . '_id')) { |
|
67
|
|
|
$this->updateProperty($service . '_id', null); |
|
68
|
|
|
$this->updateProperty('is_running', false); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getDescription() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getProperty('description'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getPrimaryService() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getProperty('primary_service'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
|
|
public function isRunning() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->getProperty('is_running'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function reinitializeTimerIds() |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($this->config->activatedServices() as $service) { |
|
101
|
|
|
$this->updateProperty($service . '_id', null); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $description |
|
107
|
|
|
* @param array $projectData |
|
108
|
|
|
* @param array $tagData |
|
109
|
|
|
* @param $specificService |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function start($description = '', array $projectData = [], array $tagData = [], array $services = []) |
|
113
|
|
|
{ |
|
114
|
|
|
$res = []; |
|
115
|
|
|
$oneServiceStarted = false; |
|
116
|
|
|
|
|
117
|
|
|
if (empty($services) === true) { |
|
118
|
|
|
return []; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->reinitializeTimerIds(); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($services as $service) { |
|
124
|
|
|
$timerId = $this->$service->startTimer( |
|
125
|
|
|
$description, |
|
126
|
|
|
$projectData[$service], |
|
127
|
|
|
$tagData[$service] |
|
128
|
|
|
); |
|
129
|
|
|
$res[$service] = $timerId; |
|
130
|
|
|
$this->updateProperty($service . '_id', $timerId); |
|
131
|
|
|
$oneServiceStarted = $oneServiceStarted || ($timerId !== null); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($oneServiceStarted === true) { |
|
135
|
|
|
$this->updateProperty('description', $description); |
|
136
|
|
|
$this->updateProperty('is_running', true); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $res; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
|
|
public function stop() |
|
146
|
|
|
{ |
|
147
|
|
|
$res = []; |
|
148
|
|
|
$oneServiceStopped = false; |
|
149
|
|
|
|
|
150
|
|
|
foreach ($this->config->runningServices() as $service) { |
|
151
|
|
|
$timerId = $this->getProperty($service . '_id'); |
|
152
|
|
|
$res[$service] = $this->$service->stopTimer($timerId); |
|
153
|
|
|
$oneServiceStopped = $oneServiceStopped || $res[$service]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if ($oneServiceStopped === true) { |
|
157
|
|
|
$this->updateProperty('is_running', false); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $res; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function undo() |
|
167
|
|
|
{ |
|
168
|
|
|
$timerData = []; |
|
169
|
|
|
|
|
170
|
|
|
foreach ($this->config->runningServices() as $service) { |
|
171
|
|
|
$timerData[$service] = $this->getProperty($service . '_id'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $this->delete($timerData); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param $name |
|
179
|
|
|
* @param $value |
|
180
|
|
|
*/ |
|
181
|
|
|
public function updateProperty($name, $value) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->config->update('timer', $name, $value); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param $name |
|
188
|
|
|
* @return mixed |
|
189
|
|
|
*/ |
|
190
|
|
|
private function getProperty($name) |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->config->get('timer', $name); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|