|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlfredTime; |
|
4
|
|
|
|
|
5
|
|
|
use AlfredTime\ServiceApiCall; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Harvest |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $message = ''; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var mixed |
|
19
|
|
|
*/ |
|
20
|
|
|
private $serviceApiCall = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param $domain |
|
24
|
|
|
* @param null $apiToken |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($domain = null, $apiToken = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->serviceApiCall = new ServiceApiCall([ |
|
29
|
|
|
'base_uri' => 'https://' . $domain . '.harvestapp.com/', |
|
30
|
|
|
'headers' => [ |
|
31
|
|
|
'Authorization' => 'Basic ' . $apiToken, |
|
32
|
|
|
], |
|
33
|
|
|
]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $timerId |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function deleteTimer($timerId = null) |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->timerAction('delete', 'daily/delete/' . $timerId); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getLastMessage() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->message; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getOnlineData() |
|
57
|
|
|
{ |
|
58
|
|
|
$data = []; |
|
59
|
|
|
$data['projects'] = $this->timerAction('get_projects', 'projects'); |
|
60
|
|
|
$data['tasks'] = $this->timerAction('get_tags', 'tasks'); |
|
61
|
|
|
|
|
62
|
|
|
return $data; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $data |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getProjects($data) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getItems('projects', $data); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $data |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getTags($data) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->getItems('tasks', $data); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $description |
|
85
|
|
|
* @param $projectId |
|
86
|
|
|
* @param $taskId |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function startTimer($description, $projectId, $taskId) |
|
90
|
|
|
{ |
|
91
|
|
|
$harvestId = null; |
|
92
|
|
|
|
|
93
|
|
|
$item = [ |
|
94
|
|
|
'notes' => $description, |
|
95
|
|
|
'project_id' => $projectId, |
|
96
|
|
|
'task_id' => $taskId, |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
$data = $this->timerAction('start', 'daily/add/', ['json' => $item]); |
|
100
|
|
|
|
|
101
|
|
|
if (isset($data['id']) === true) { |
|
102
|
|
|
$harvestId = $data['id']; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $harvestId; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param $timerId |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function stopTimer($timerId = null) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($this->isTimerRunning($timerId) === false) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->timerAction('stop', 'daily/timer/' . $timerId); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param $needle |
|
123
|
|
|
* @param array $haystack |
|
124
|
|
|
* @return mixed |
|
125
|
|
|
*/ |
|
126
|
|
|
private function getItems($needle, array $haystack = []) |
|
127
|
|
|
{ |
|
128
|
|
|
$items = []; |
|
129
|
|
|
|
|
130
|
|
|
if (isset($haystack[$needle]) === false) { |
|
131
|
|
|
return []; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
foreach ($haystack[$needle] as $item) { |
|
135
|
|
|
$items[] = [ |
|
136
|
|
|
'name' => $item[key($item)]['name'], |
|
137
|
|
|
'id' => $item[key($item)]['id'], |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $items; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param $timerId |
|
146
|
|
|
* @return boolean |
|
147
|
|
|
*/ |
|
148
|
|
|
private function isTimerRunning($timerId) |
|
149
|
|
|
{ |
|
150
|
|
|
$data = $this->timerAction('timer_running', 'daily/show/' . $timerId); |
|
151
|
|
|
|
|
152
|
|
|
return isset($data['timer_started_at']); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $action |
|
157
|
|
|
* @param string $apiUri |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
private function timerAction($action, $apiUri, array $options = []) |
|
161
|
|
|
{ |
|
162
|
|
|
$returnDataFor = [ |
|
163
|
|
|
'start', |
|
164
|
|
|
'timer_running', |
|
165
|
|
|
'get_projects', |
|
166
|
|
|
'get_tags', |
|
167
|
|
|
]; |
|
168
|
|
|
$methods = [ |
|
169
|
|
|
'start' => 'post', |
|
170
|
|
|
'stop' => 'get', |
|
171
|
|
|
'delete' => 'delete', |
|
172
|
|
|
'timer_running' => 'get', |
|
173
|
|
|
'get_projects' => 'get', |
|
174
|
|
|
'get_tags' => 'get', |
|
175
|
|
|
'', |
|
176
|
|
|
]; |
|
177
|
|
|
|
|
178
|
|
|
$method = isset($methods[$action]) ? $methods[$action] : ''; |
|
179
|
|
|
|
|
180
|
|
View Code Duplication |
if ($this->serviceApiCall->send($method, $apiUri, $options) === false) { |
|
|
|
|
|
|
181
|
|
|
$this->message = $this->serviceApiCall->getMessage(); |
|
182
|
|
|
|
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (in_array($action, $returnDataFor) === true) { |
|
187
|
|
|
return $this->serviceApiCall->getData(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $this->serviceApiCall->last('success'); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
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.