|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Katapoka\Ahgora; |
|
4
|
|
|
|
|
5
|
|
|
use Katapoka\Ahgora\Contracts\IHttpClient; |
|
6
|
|
|
use Mockery\Exception; |
|
7
|
|
|
|
|
8
|
|
|
class RestApi extends AbstractApi |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The API base url. |
|
12
|
|
|
*/ |
|
13
|
|
|
const API_BASE_URL = 'https://www.ahgora.com.br/externo'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The endpoint to get the punches. |
|
17
|
|
|
*/ |
|
18
|
|
|
const ENDPOINT_APURACAO = '%s/getApuracao'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The company id. |
|
22
|
|
|
* string @var |
|
23
|
|
|
*/ |
|
24
|
|
|
private $companyId; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The account username. |
|
28
|
|
|
* string @var |
|
29
|
|
|
*/ |
|
30
|
|
|
private $username; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The account password. |
|
34
|
|
|
* @var |
|
35
|
|
|
*/ |
|
36
|
|
|
private $password; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \Katapoka\Ahgora\Contracts\IHttpClient |
|
40
|
|
|
*/ |
|
41
|
|
|
private $httpClient; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Api constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param IHttpClient $httpClient |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(IHttpClient $httpClient) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->httpClient = $httpClient; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set the company id of the ahgora system. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $companyId |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setCompanyId($companyId) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->companyId = $companyId; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the username of the employee, from the company set at the setCompanyId. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $username |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setUsername($username) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->username = $username; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set the password of the employee, from the company set at the setCompanyId. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $password |
|
85
|
|
|
* |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setPassword($password) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->password = $password; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Try to execute the login on the page. |
|
97
|
|
|
* To execute some actions the user needs to be loggedin. |
|
98
|
|
|
* After a successful login, the status loggedin is saved as true. |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool Returns true if the login was successful and false otherwise |
|
101
|
|
|
*/ |
|
102
|
|
|
public function doLogin() |
|
103
|
|
|
{ |
|
104
|
|
|
$params = [ |
|
105
|
|
|
'company' => $this->companyId, |
|
106
|
|
|
'matricula' => $this->username, |
|
107
|
|
|
'senha' => $this->password, |
|
108
|
|
|
'mes' => date('m'), |
|
109
|
|
|
'ano' => date('Y'), |
|
110
|
|
|
]; |
|
111
|
|
|
try { |
|
112
|
|
|
$response = $this->httpClient->post($this->buildUrl(static::ENDPOINT_APURACAO), $params)->json(); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
return isset($response->empresa->empresa) && $response->empresa->empresa === $this->companyId; |
|
115
|
|
|
} catch (Exception $e) { |
|
116
|
|
|
$this->error($e->getMessage(), $params); |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get the punches at the given parameters. |
|
123
|
|
|
* |
|
124
|
|
|
* @param int|null $month The month you want to get the punches - Must be between 01 and 12 (both included) |
|
125
|
|
|
* @param int|null $year The year you want to get the punches |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getPunches($month = null, $year = null) |
|
130
|
|
|
{ |
|
131
|
|
|
$params = [ |
|
132
|
|
|
'company' => $this->companyId, |
|
133
|
|
|
'matricula' => $this->username, |
|
134
|
|
|
'senha' => $this->password, |
|
135
|
|
|
'mes' => str_pad($month, 2, '0', STR_PAD_LEFT), |
|
136
|
|
|
'ano' => $year, |
|
137
|
|
|
]; |
|
138
|
|
|
try { |
|
139
|
|
|
$response = $this->httpClient->post($this->buildUrl(static::ENDPOINT_APURACAO), $params)->json(); |
|
|
|
|
|
|
140
|
|
|
if (isset($response->error)) { |
|
141
|
|
|
throw new \BadMethodCallException($response->error); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return ['punches' => $this->parsePunches($response->dias)]; |
|
145
|
|
|
} catch (Exception $e) { |
|
146
|
|
|
$this->error($e->getMessage(), $params); |
|
147
|
|
|
|
|
148
|
|
|
return []; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the punches from some given day. |
|
154
|
|
|
* |
|
155
|
|
|
* @param int $day |
|
156
|
|
|
* @param int $month |
|
157
|
|
|
* @param int $year |
|
158
|
|
|
* |
|
159
|
|
|
* @return mixed |
|
160
|
|
|
*/ |
|
161
|
|
View Code Duplication |
public function getPunchesFromDay($day, $month, $year) |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$date = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); |
|
164
|
|
|
list($year, $month, $day) = explode('-', $date); |
|
165
|
|
|
|
|
166
|
|
|
if ($day > 19) { |
|
167
|
|
|
$month++; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($month > 12) { |
|
171
|
|
|
$month = 1; |
|
172
|
|
|
$year++; |
|
173
|
|
|
} |
|
174
|
|
|
$punches = $this->getPunches($month, $year); |
|
175
|
|
|
|
|
176
|
|
|
return array_filter($punches['punches'], function (\DateTime $punchDateTime) use ($day) { |
|
177
|
|
|
return (int) $punchDateTime->format('d') === (int) $day; |
|
178
|
|
|
}); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Gets the employee name. |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getEmployeeName() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->getEmployeeData('nome'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Gets the employer name. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getEmployeeRole() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->getEmployeeData('cargo'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get the employer department. |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getDepartment() |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->getEmployeeData('departamento'); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Gets the user data and punches. |
|
213
|
|
|
* |
|
214
|
|
|
* @param null $field |
|
215
|
|
|
* |
|
216
|
|
|
* @return mixed |
|
217
|
|
|
*/ |
|
218
|
|
|
private function getEmployeeData($field = null) |
|
219
|
|
|
{ |
|
220
|
|
|
if (!empty($field) && property_exists($this->getData()->funcionario, $field)) { |
|
221
|
|
|
return $this->getData()->funcionario->{$field}; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $this->getData()->funcionario; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Gets all the data and punches. Cached method. |
|
229
|
|
|
* |
|
230
|
|
|
* @return mixed |
|
231
|
|
|
*/ |
|
232
|
|
|
private function getData() { |
|
233
|
|
|
return once(function () { |
|
234
|
|
|
$params = [ |
|
235
|
|
|
'company' => $this->companyId, |
|
236
|
|
|
'matricula' => $this->username, |
|
237
|
|
|
'senha' => $this->password, |
|
238
|
|
|
'mes' => date('m'), |
|
239
|
|
|
'ano' => date('Y'), |
|
240
|
|
|
]; |
|
241
|
|
|
$response = $this->httpClient->post($this->buildUrl(static::ENDPOINT_APURACAO), $params)->json(); |
|
|
|
|
|
|
242
|
|
|
if (isset($response->error)) { |
|
243
|
|
|
throw new \BadMethodCallException($response->error); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $response; |
|
247
|
|
|
}); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
private function buildUrl($endpoint, array $params = []) { |
|
251
|
|
|
return once(function () use ($endpoint, $params) { |
|
252
|
|
|
return vsprintf($endpoint, array_merge([static::API_BASE_URL], $params)); |
|
253
|
|
|
}); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
private function parsePunches($dias) |
|
257
|
|
|
{ |
|
258
|
|
|
$tmp = []; |
|
259
|
|
|
|
|
260
|
|
|
foreach ($dias as $dia => $diaConfig) { |
|
261
|
|
|
foreach ($diaConfig->batidas as $batida) { |
|
262
|
|
|
$hora = substr($batida->hora, 0, 2); |
|
263
|
|
|
$minuto = substr($batida->hora, 2, 2); |
|
264
|
|
|
$tmp[] = new \DateTime(sprintf('%s %s:%s:00', $dia, $hora, $minuto)); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $tmp; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: