1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TogglJira\Service; |
5
|
|
|
|
6
|
|
|
use AJT\Toggl\TogglClient; |
7
|
|
|
use Exception; |
8
|
|
|
use GuzzleHttp\Command\Guzzle\GuzzleClient; |
9
|
|
|
use Psr\Log\LoggerAwareInterface; |
10
|
|
|
use Psr\Log\LoggerAwareTrait; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use TogglJira\Entity\WorkLogEntry; |
13
|
|
|
use TogglJira\Hydrator\WorkLogHydrator; |
14
|
|
|
use TogglJira\Jira\Api; |
15
|
|
|
|
16
|
|
|
class SyncService implements LoggerAwareInterface |
17
|
|
|
{ |
18
|
|
|
use LoggerAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Api |
22
|
|
|
*/ |
23
|
|
|
private $api; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TogglClient |
27
|
|
|
*/ |
28
|
|
|
private $togglClient; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var WorkLogHydrator |
32
|
|
|
*/ |
33
|
|
|
private $workLogHydrator; |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $username; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Api $api |
41
|
|
|
* @param GuzzleClient $togglClient |
42
|
|
|
* @param WorkLogHydrator $workLogHydrator |
43
|
|
|
* @param string $username |
44
|
|
|
*/ |
45
|
6 |
|
public function __construct(Api $api, GuzzleClient $togglClient, WorkLogHydrator $workLogHydrator, string $username) |
46
|
|
|
{ |
47
|
6 |
|
$this->api = $api; |
48
|
6 |
|
$this->togglClient = $togglClient; |
49
|
6 |
|
$this->workLogHydrator = $workLogHydrator; |
50
|
6 |
|
$this->username = $username; |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \DateTimeInterface $startDate |
55
|
|
|
* @param \DateTimeInterface $endDate |
56
|
|
|
* @param bool $overwrite |
57
|
|
|
* @return void |
58
|
|
|
* @throws Exception |
59
|
|
|
*/ |
60
|
5 |
|
public function sync(\DateTimeInterface $startDate, \DateTimeInterface $endDate, bool $overwrite): void |
61
|
|
|
{ |
62
|
5 |
|
$user = $this->api->getUser($this->username); |
63
|
|
|
|
64
|
5 |
|
if (!isset($user['accountId'])) { |
65
|
1 |
|
throw new RuntimeException("User with username {$this->username} not found"); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
while ($startDate < $endDate) { |
69
|
4 |
|
$timeEntries = $this->getTimeEntries( |
70
|
4 |
|
$startDate, |
71
|
4 |
|
(new \DateTime($startDate->format('Y-m-d')))->modify('+1 day') |
72
|
|
|
); |
73
|
4 |
|
if (!$timeEntries) { |
74
|
1 |
|
$startDate = $this->createStartDateFromTomorrow($startDate); |
75
|
1 |
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
$this->addWorkLogsToApi($this->parseTimeEntries($timeEntries), $user, $overwrite); |
79
|
|
|
|
80
|
3 |
|
$startDate = $this->createStartDateFromTomorrow($startDate); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
$this->logger->info('All done for today, time to go home!'); |
84
|
4 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \DateTimeInterface $startDate |
88
|
|
|
* @param \DateTimeInterface $endDate |
89
|
|
|
* @return array|null |
90
|
|
|
*/ |
91
|
4 |
|
private function getTimeEntries(\DateTimeInterface $startDate, \DateTimeInterface $endDate): ?array |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
/** @var array $timeEntries */ |
95
|
4 |
|
return $this->togglClient->getTimeEntries( |
96
|
|
|
[ |
97
|
4 |
|
'start_date' => $startDate->format(DATE_ATOM), |
98
|
4 |
|
'end_date' => $endDate->format(DATE_ATOM), |
99
|
|
|
] |
100
|
3 |
|
)->toArray(); |
101
|
1 |
|
} catch (Exception $e) { |
102
|
1 |
|
$this->logger->error( |
103
|
1 |
|
'Failed to get time entries from Toggl', |
104
|
1 |
|
['exception' => $e] |
105
|
|
|
); |
106
|
|
|
|
107
|
1 |
|
return null; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $timeEntries |
113
|
|
|
* @return array |
114
|
|
|
* @throws Exception |
115
|
|
|
*/ |
116
|
3 |
|
private function parseTimeEntries(array $timeEntries): array |
117
|
|
|
{ |
118
|
3 |
|
$workLogEntries = []; |
119
|
|
|
|
120
|
3 |
|
foreach ($timeEntries as $timeEntry) { |
121
|
3 |
|
$workLogEntry = $this->parseTimeEntry($timeEntry); |
122
|
|
|
|
123
|
3 |
|
if (!$workLogEntry) { |
124
|
1 |
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
$existingKey = md5($workLogEntry->getIssueID() . '-' . $workLogEntry->getSpentOn()->format('Y-m-d')); |
128
|
|
|
|
129
|
2 |
|
if (isset($workLogEntries[$existingKey])) { |
130
|
2 |
|
$this->addTimeToExistingTimeEntry($workLogEntries[$existingKey], $workLogEntry); |
131
|
2 |
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
$workLogEntries[$existingKey] = $workLogEntry; |
135
|
|
|
|
136
|
2 |
|
$this->logger->info('Found time entry for issue', [ |
137
|
2 |
|
'issueID' => $workLogEntry->getIssueID(), |
138
|
2 |
|
'spentOn' => $workLogEntry->getSpentOn()->format('Y-m-d'), |
139
|
2 |
|
'timeSpent' => round($workLogEntry->getTimeSpent() / 60 / 60, 2) . ' hours', |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
return $workLogEntries; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array $timeEntry |
148
|
|
|
* @return WorkLogEntry|null |
149
|
|
|
* @throws Exception |
150
|
|
|
*/ |
151
|
3 |
|
private function parseTimeEntry(array $timeEntry): ?WorkLogEntry |
152
|
|
|
{ |
153
|
|
|
$data = [ |
154
|
3 |
|
'issueID' => explode(' ', $timeEntry['description'])[0], |
155
|
3 |
|
'timeSpent' => $timeEntry['duration'], |
156
|
3 |
|
'comment' => $timeEntry['description'], |
157
|
3 |
|
'spentOn' => $timeEntry['start'] |
158
|
|
|
]; |
159
|
|
|
|
160
|
3 |
|
if (strpos($data['issueID'], '-') === false) { |
161
|
1 |
|
$this->logger->warning('Could not parse issue string, cannot link to Jira'); |
162
|
1 |
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
if ($data['timeSpent'] < 0) { |
166
|
1 |
|
$this->logger->info('0 seconds, or timer still running, skipping', [ |
167
|
1 |
|
'issueID' => $data['issueID'] |
168
|
|
|
]); |
169
|
1 |
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
return $this->workLogHydrator->hydrate($data, new WorkLogEntry()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $existingWorkLog |
177
|
|
|
* @param $newWorkLog |
178
|
|
|
* @return WorkLogEntry |
179
|
|
|
*/ |
180
|
2 |
|
private function addTimeToExistingTimeEntry(WorkLogEntry $existingWorkLog, WorkLogEntry $newWorkLog): WorkLogEntry |
181
|
|
|
{ |
182
|
2 |
|
$timeSpent = $existingWorkLog->getTimeSpent(); |
183
|
2 |
|
$timeSpent += $newWorkLog->getTimeSpent(); |
184
|
|
|
|
185
|
2 |
|
$existingWorkLog->setTimeSpent($timeSpent); |
186
|
2 |
|
$existingWorkLog->setComment($existingWorkLog->getComment() . "\n" . $newWorkLog->getComment()); |
187
|
|
|
|
188
|
2 |
|
$this->logger->info('Added time spent for issue', [ |
189
|
2 |
|
'issueID' => $newWorkLog->getIssueID(), |
190
|
2 |
|
'spentOn' => $newWorkLog->getSpentOn()->format('Y-m-d'), |
191
|
2 |
|
'timeSpent' => round($newWorkLog->getTimeSpent() / 60 / 60, 2) . ' hours', |
192
|
|
|
]); |
193
|
|
|
|
194
|
2 |
|
return $existingWorkLog; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $workLogEntries |
199
|
|
|
* @param array $user |
200
|
|
|
* @param bool $overwrite |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
3 |
|
private function addWorkLogsToApi(array $workLogEntries, array $user, bool $overwrite): void |
204
|
|
|
{ |
205
|
|
|
/** @var WorkLogEntry $workLogEntry */ |
206
|
3 |
|
foreach ($workLogEntries as $workLogEntry) { |
207
|
|
|
try { |
208
|
2 |
|
$result = $this->api->addWorkLogEntry( |
209
|
2 |
|
$workLogEntry->getIssueID(), |
210
|
2 |
|
$workLogEntry->getTimeSpent(), |
211
|
2 |
|
$user['accountId'], |
212
|
2 |
|
$workLogEntry->getComment(), |
213
|
2 |
|
$workLogEntry->getSpentOn()->format('Y-m-d\TH:i:s.vO'), |
214
|
2 |
|
$overwrite |
215
|
|
|
); |
216
|
|
|
|
217
|
1 |
|
if (isset($result->getResult()['errorMessages']) && \count($result->getResult()['errorMessages']) > 0) { |
218
|
|
|
$this->logger->error(implode("\n", $result->getResult()['errorMessages']), [ |
219
|
|
|
'issueID' => $workLogEntry->getIssueID() |
220
|
|
|
]); |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
$this->logger->info('Saved worklog entry', [ |
224
|
1 |
|
'issueID' => $workLogEntry->getIssueID(), |
225
|
1 |
|
'spentOn' => $workLogEntry->getSpentOn()->format('Y-m-d'), |
226
|
1 |
|
'timeSpent' => round($workLogEntry->getTimeSpent() / 60 / 60, 2) . ' hours', |
227
|
|
|
]); |
228
|
1 |
|
} catch (Exception $e) { |
229
|
2 |
|
$this->logger->error('Could not add worklog entry', ['exception' => $e]); |
230
|
|
|
} |
231
|
|
|
} |
232
|
3 |
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param \DateTimeInterface $startDate |
236
|
|
|
* @return \DateTimeInterface |
237
|
|
|
*/ |
238
|
4 |
|
private function createStartDateFromTomorrow(\DateTimeInterface $startDate): \DateTimeInterface |
239
|
|
|
{ |
240
|
4 |
|
return (new \DateTime($startDate->format(DATE_ATOM)))->modify('+1 day'); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|