1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Katapoka\Ahgora; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Katapoka\Ahgora\Contracts\IAhgoraApi; |
7
|
|
|
use Katapoka\Ahgora\Contracts\IHttpClient; |
8
|
|
|
use Katapoka\Ahgora\Contracts\IHttpResponse; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class responsible for getting the data from the Ahgora system. |
12
|
|
|
*/ |
13
|
|
|
class HttpApi implements IAhgoraApi |
14
|
|
|
{ |
15
|
|
|
use Loggable; |
16
|
|
|
|
17
|
|
|
const AHGORA_BASE_URL = 'https://www.ahgora.com.br'; |
18
|
|
|
const AHGORA_COMPANY_URL = '%s/externo/index/%s'; |
19
|
|
|
const AHGORA_LOGIN_URL = '%s/externo/login'; |
20
|
|
|
|
21
|
|
|
/** @var \Katapoka\Ahgora\Contracts\IHttpClient */ |
22
|
|
|
private $httpClient; |
23
|
|
|
/** @var string */ |
24
|
|
|
private $password; |
25
|
|
|
/** @var string */ |
26
|
|
|
private $companyId; |
27
|
|
|
/** @var string */ |
28
|
|
|
private $username; |
29
|
|
|
/** @var bool */ |
30
|
|
|
private $loggedIn = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Api constructor. |
34
|
|
|
* |
35
|
|
|
* @param IHttpClient $httpClient |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct(IHttpClient $httpClient) |
38
|
|
|
{ |
39
|
1 |
|
$this->httpClient = $httpClient; |
40
|
1 |
|
$this->debug('Api instance created'); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the company id of the ahgora system. |
45
|
|
|
* |
46
|
|
|
* @param string $companyId |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function setCompanyId($companyId) |
51
|
|
|
{ |
52
|
|
|
$this->companyId = $companyId; |
53
|
|
|
$this->debug('Company ID set', ['company_id' => $companyId]); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the username of the employee, from the company set at the setCompanyId. |
60
|
|
|
* |
61
|
|
|
* @param string $username |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setUsername($username) |
66
|
|
|
{ |
67
|
|
|
$this->username = $username; |
68
|
|
|
$this->debug('Username set', ['username' => $username]); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the password of the employee, from the company set at the setCompanyId. |
75
|
|
|
* |
76
|
|
|
* @param string $password |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setPassword($password) |
81
|
|
|
{ |
82
|
|
|
$this->password = $password; |
83
|
|
|
$this->debug('Password set', ['password' => $password]); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Try to execute the login on the page. |
90
|
|
|
* To execute some actions the user needs to be loggedin. |
91
|
|
|
* After a successful login, the status loggedin is saved as true. |
92
|
|
|
* |
93
|
|
|
* @return bool Returns true if the login was successful and false otherwise |
94
|
|
|
*/ |
95
|
|
|
public function doLogin() |
96
|
|
|
{ |
97
|
|
|
$hasLoggedIn = false; |
98
|
|
|
$this->debug('Started login proccess'); |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
$accessEnabled = $this->checkAccessEnabled(); |
102
|
|
|
|
103
|
|
|
if ($accessEnabled) { |
104
|
|
|
$response = $this->executeLogin(); |
105
|
|
|
$hasLoggedIn = $this->checkLoginStatus($response); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->debug($accessEnabled ? "Company has external access enabled" : "Company hasn't external access enabled"); |
109
|
|
|
|
110
|
|
|
$this->setLoggedIn($hasLoggedIn); |
111
|
|
|
|
112
|
|
|
return $hasLoggedIn; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Execute the login on the server and returns the server response. |
117
|
|
|
* |
118
|
|
|
* @return IHttpResponse |
119
|
|
|
*/ |
120
|
|
|
private function executeLogin() |
121
|
|
|
{ |
122
|
|
|
return $this->httpClient->post($this->loginUrl(), [ |
123
|
|
|
'empresa' => $this->companyId, |
124
|
|
|
'matricula' => $this->username, |
125
|
|
|
'senha' => $this->password, |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check if the company has external access on the Ahgora system. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
private function checkAccessEnabled() |
135
|
|
|
{ |
136
|
|
|
$response = $this->httpClient->get($this->companyUrl()); |
137
|
|
|
|
138
|
|
|
return stripos($response->getBody(), 'Sua Empresa não liberou o acesso a essa ferramenta') === false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check the return of the login action. |
143
|
|
|
* How it works: If statusCode 200 and no body, login ok, otherwise, login failed. |
144
|
|
|
* Should return a json with property "r" with "error" and "text" with the message |
145
|
|
|
* |
146
|
|
|
* @param IHttpResponse $response |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
private function checkLoginStatus(IHttpResponse $response) |
151
|
|
|
{ |
152
|
|
|
try { |
153
|
|
|
if ($response->getHttpStatus() === IHttpClient::HTTP_STATUS_OK) { |
154
|
|
|
return $this->getResponseLoginStatus($response); |
155
|
|
|
} |
156
|
|
|
} catch (InvalidArgumentException $iaex) { |
157
|
|
|
$this->error($iaex->getMessage(), ['expcetion' => $iaex]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function getResponseLoginStatus(IHttpResponse $response) |
164
|
|
|
{ |
165
|
|
|
try { |
166
|
|
|
$json = $response->json(); |
167
|
|
|
|
168
|
|
|
return array_key_exists('r', $json) && $json->r === 'success'; |
169
|
|
|
} catch (InvalidArgumentException $iaex) { |
170
|
|
|
$this->debug('getResponseLoginStatus', ['exception', $iaex]); |
171
|
|
|
|
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Safely set if the user is loggedin or not. |
178
|
|
|
* Did a separate method do eventually trigger events, if necessary. |
179
|
|
|
* |
180
|
|
|
* @param bool $loggedIn |
181
|
|
|
* |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
private function setLoggedIn($loggedIn = true) |
185
|
|
|
{ |
186
|
|
|
if (!is_bool($loggedIn)) { |
187
|
|
|
throw new InvalidArgumentException('LoggedIn parameter must be boolean'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->debug('setLoggedIn', ['logged_in' => $loggedIn]); |
191
|
|
|
$this->loggedIn = $loggedIn; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Build the company url string. |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
private function companyUrl() |
202
|
|
|
{ |
203
|
|
|
$companyUrl = sprintf(self::AHGORA_COMPANY_URL, self::AHGORA_BASE_URL, $this->companyId); |
204
|
|
|
$this->debug('CompanyURL', ['company_url' => $companyUrl]); |
205
|
|
|
|
206
|
|
|
return $companyUrl; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Build the login url. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
private function loginUrl() |
215
|
|
|
{ |
216
|
|
|
$loginUrl = sprintf(self::AHGORA_LOGIN_URL, self::AHGORA_BASE_URL); |
217
|
|
|
$this->debug('loginUrl', ['login_url' => $loginUrl]); |
218
|
|
|
|
219
|
|
|
return $loginUrl; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the punchs at the given parameters. |
224
|
|
|
* |
225
|
|
|
* @param int|null $month The month you want to get the punchs - Must be between 01 and 12 (both included) |
226
|
|
|
* @param int|null $year The year you want to get the punchs |
227
|
|
|
* |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
public function getPunchs($month = null, $year = null) |
231
|
|
|
{ |
232
|
|
|
return []; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Gets the employer name. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getEmployeeRole() |
241
|
|
|
{ |
242
|
|
|
return "NOT IMPLEMENTED YET"; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get the employer department. |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function getDepartment() |
251
|
|
|
{ |
252
|
|
|
return "NOT IMPLEMENTED YET"; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|