|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Harvest |
|
11
|
|
|
{ |
|
12
|
|
|
private $message; |
|
13
|
|
|
private $domain; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
public function __construct($domain = null, $apiToken = null) |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
$this->client = new Client([ |
|
|
|
|
|
|
18
|
|
|
'base_uri' => 'https://' . $domain . '.harvestapp.com/daily/', |
|
19
|
|
|
'headers' => [ |
|
20
|
|
|
'Content-type' => 'application/json', |
|
21
|
|
|
'Accept' => 'application/json', |
|
22
|
|
|
'Authorization' => 'Basic ' . $apiToken, |
|
23
|
|
|
], |
|
24
|
|
|
]); |
|
25
|
|
|
$this->message = ''; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function startTimer($description, $projectId, $taskId) |
|
29
|
|
|
{ |
|
30
|
|
|
$harvestId = null; |
|
31
|
|
|
|
|
32
|
|
|
$item = [ |
|
33
|
|
|
'notes' => $description, |
|
34
|
|
|
'project_id' => $projectId, |
|
35
|
|
|
'task_id' => $taskId, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
$response = $this->client->post('add', [ |
|
39
|
|
|
'json' => $item, |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
if ($response->getStatusCode() !== 201) { |
|
|
|
|
|
|
43
|
|
|
$this->message = '- Cannot start Harvest timer!'; |
|
44
|
|
|
} else { |
|
45
|
|
|
$data = json_decode($response->getBody(), true); |
|
46
|
|
|
$harvestId = $data['id']; |
|
47
|
|
|
$this->message = '- Harvest timer started'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $harvestId; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function stopTimer($timerId = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$res = false; |
|
56
|
|
|
|
|
57
|
|
|
if ($this->isTimerRunning($timerId) === true) { |
|
58
|
|
|
$response = $this->client->get('timer/' . $timerId); |
|
59
|
|
|
|
|
60
|
|
|
if ($response->getStatusCode() !== 200) { |
|
61
|
|
|
$this->message = '- Could not stop Harvest timer!'; |
|
62
|
|
|
} else { |
|
63
|
|
|
$this->message = '- Harvest timer stopped'; |
|
64
|
|
|
$res = true; |
|
65
|
|
|
} |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->message = '- Harvest timer was not running'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $res; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getLastMessage() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->message; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function deleteTimer($timerId = null) |
|
79
|
|
|
{ |
|
80
|
|
|
$res = false; |
|
81
|
|
|
|
|
82
|
|
|
$response = $this->client->delete('delete/' . $timerId); |
|
83
|
|
|
|
|
84
|
|
|
if ($response->getStatusCode() !== 200) { |
|
85
|
|
|
$this->message = '- Could not delete Harvest timer!'; |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->message = '- Harvest timer deleted'; |
|
88
|
|
|
$res = true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $res; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function isTimerRunning($timerId) |
|
95
|
|
|
{ |
|
96
|
|
|
$res = false; |
|
97
|
|
|
|
|
98
|
|
|
$response = $this->client->get('show/' . $timerId); |
|
99
|
|
|
|
|
100
|
|
|
if ($response->getStatusCode() === 200) { |
|
101
|
|
|
$data = json_decode($response->getBody(), true); |
|
102
|
|
|
if (isset($data['timer_started_at']) === true) { |
|
103
|
|
|
$res = true; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $res; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.