|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Godbout\Alfred\Time\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Carbon\CarbonInterval; |
|
7
|
|
|
use Required\Harvest\Client; |
|
8
|
|
|
use Required\Harvest\Exception\AuthenticationException; |
|
9
|
|
|
use Required\Harvest\Exception\NotFoundException; |
|
10
|
|
|
use Required\Harvest\Exception\ValidationFailedException; |
|
11
|
|
|
|
|
12
|
|
|
class Harvest extends TimerService |
|
13
|
|
|
{ |
|
14
|
|
|
public $allowsEmptyProject = false; |
|
15
|
|
|
|
|
16
|
|
|
public $allowsEmptyTag = false; |
|
17
|
|
|
|
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
54 |
|
public function __construct($accountId, $apiToken) |
|
22
|
|
|
{ |
|
23
|
54 |
|
$this->client = new Client(); |
|
24
|
|
|
|
|
25
|
54 |
|
$this->client->authenticate($accountId, $apiToken); |
|
26
|
54 |
|
} |
|
27
|
|
|
|
|
28
|
14 |
|
public function projects() |
|
29
|
|
|
{ |
|
30
|
|
|
try { |
|
31
|
14 |
|
return array_column($this->client->projects()->all(['']), 'name', 'id'); |
|
32
|
12 |
|
} catch (AuthenticationException $e) { |
|
33
|
12 |
|
return []; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
16 |
|
public function tags() |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
16 |
|
$taskAssignments = $this->client->projects()->taskAssignments()->all( |
|
41
|
16 |
|
(int) getenv('timer_project_id'), |
|
42
|
16 |
|
['is_active' => true] |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
4 |
|
array_walk($taskAssignments, function ($taskAssignment) use (&$tags) { |
|
|
|
|
|
|
46
|
4 |
|
$tags[$taskAssignment['task']['id']] = $taskAssignment['task']['name']; |
|
47
|
4 |
|
}); |
|
48
|
|
|
|
|
49
|
4 |
|
return $tags; |
|
50
|
12 |
|
} catch (AuthenticationException $e) { |
|
51
|
12 |
|
return []; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function pastTimers() |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
2 |
|
$harvestTimers = $this->client->timeEntries()->all([ |
|
59
|
2 |
|
'from' => Carbon::today()->subDays(30), |
|
60
|
2 |
|
'to' => Carbon::today() |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
2 |
|
return $this->convertToPastTimers($harvestTimers); |
|
64
|
|
|
} catch (AuthenticationException $e) { |
|
65
|
|
|
return []; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
12 |
|
public function startTimer() |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
12 |
|
$timer = $this->client->timeEntries()->create([ |
|
73
|
12 |
|
'notes' => getenv('timer_description'), |
|
74
|
12 |
|
'project_id' => (int) getenv('timer_project_id'), |
|
75
|
12 |
|
'task_id' => (int) getenv('timer_tag_id'), |
|
76
|
12 |
|
'spent_date' => date('Y-m-d') |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
12 |
|
if (! isset($timer['id'])) { |
|
80
|
12 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} catch (ValidationFailedException $e) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
12 |
|
return $timer['id']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
10 |
|
public function stopCurrentTimer() |
|
90
|
|
|
{ |
|
91
|
10 |
|
if ($timerId = $this->runningTimer()) { |
|
92
|
10 |
|
$timer = $this->client->timeEntries()->stop($timerId); |
|
93
|
|
|
|
|
94
|
10 |
|
if (! isset($timer['id'])) { |
|
95
|
|
|
throw new \Exception("Can't stop current running timer.", 1); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
10 |
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
42 |
|
public function runningTimer() |
|
105
|
|
|
{ |
|
106
|
|
|
try { |
|
107
|
42 |
|
$timer = $this->client->timeEntries()->all(['is_running' => true]); |
|
108
|
|
|
|
|
109
|
18 |
|
return $timer[0]['id'] ?? false; |
|
110
|
24 |
|
} catch (\Exception $e) { |
|
111
|
24 |
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
public function continueTimer($timerId) |
|
116
|
|
|
{ |
|
117
|
2 |
|
$timer = $this->client->timeEntries()->restart($timerId); |
|
118
|
|
|
|
|
119
|
2 |
|
return $timer['id'] ?? false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
12 |
|
public function deleteTimer($timerId) |
|
123
|
|
|
{ |
|
124
|
|
|
try { |
|
125
|
12 |
|
$this->client->timeEntries()->remove($timerId); |
|
126
|
|
|
} catch (NotFoundException $e) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
12 |
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
protected function convertToPastTimers($harvestTimers) |
|
134
|
|
|
{ |
|
135
|
2 |
|
return array_map(function ($harvestTimer) { |
|
136
|
2 |
|
return $this->buildPastTimerObject($harvestTimer); |
|
137
|
2 |
|
}, $harvestTimers); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
protected function buildPastTimerObject($harvestTimer) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$pastTimer['id'] = $harvestTimer['id']; |
|
|
|
|
|
|
143
|
2 |
|
$pastTimer['description'] = $harvestTimer['notes']; |
|
144
|
2 |
|
$pastTimer['project_id'] = $harvestTimer['project']['id']; |
|
145
|
2 |
|
$pastTimer['project_name'] = $harvestTimer['project']['name']; |
|
146
|
2 |
|
$pastTimer['tag_id'] = $harvestTimer['task']['id']; |
|
147
|
2 |
|
$pastTimer['tags'] = $harvestTimer['task']['name']; |
|
148
|
2 |
|
$pastTimer['duration'] = CarbonInterval::seconds( |
|
149
|
2 |
|
floor($harvestTimer['hours'] * 3600) |
|
150
|
2 |
|
)->cascade()->format('%H:%I:%S'); |
|
151
|
|
|
|
|
152
|
2 |
|
return (object) $pastTimer; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|