1 | <?php |
||
13 | class HttpApi extends AbstractApi |
||
14 | { |
||
15 | const AHGORA_BASE_URL = 'https://www.ahgora.com.br'; |
||
16 | const AHGORA_COMPANY_URL = '%s/externo/index/%s'; |
||
17 | const AHGORA_LOGIN_URL = '%s/externo/login'; |
||
18 | const AHGORA_PUNCHS_URL = '%s/externo/batidas/%d-%d'; |
||
19 | |||
20 | /** @var \Katapoka\Ahgora\Contracts\IHttpClient */ |
||
21 | private $httpClient; |
||
22 | /** @var string */ |
||
23 | private $password; |
||
24 | /** @var string */ |
||
25 | private $companyId; |
||
26 | /** @var string */ |
||
27 | private $username; |
||
28 | /** @var bool */ |
||
29 | private $loggedIn = false; |
||
30 | |||
31 | /** |
||
32 | * Api constructor. |
||
33 | * |
||
34 | * @param IHttpClient $httpClient |
||
35 | */ |
||
36 | 1 | public function __construct(IHttpClient $httpClient) |
|
41 | |||
42 | /** |
||
43 | * Set the company id of the ahgora system. |
||
44 | * |
||
45 | * @param string $companyId |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function setCompanyId($companyId) |
||
56 | |||
57 | /** |
||
58 | * Set the username of the employee, from the company set at the setCompanyId. |
||
59 | * |
||
60 | * @param string $username |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function setUsername($username) |
||
71 | |||
72 | /** |
||
73 | * Set the password of the employee, from the company set at the setCompanyId. |
||
74 | * |
||
75 | * @param string $password |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setPassword($password) |
||
86 | |||
87 | /** |
||
88 | * Try to execute the login on the page. |
||
89 | * To execute some actions the user needs to be loggedin. |
||
90 | * After a successful login, the status loggedin is saved as true. |
||
91 | * |
||
92 | * @return bool Returns true if the login was successful and false otherwise |
||
93 | */ |
||
94 | public function doLogin() |
||
112 | |||
113 | /** |
||
114 | * Get the punchs at the given parameters. |
||
115 | * |
||
116 | * @param int|null $month The month you want to get the punchs - Must be between 01 and 12 (both included) |
||
117 | * @param int|null $year The year you want to get the punchs |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function getPunchs($month = null, $year = null) |
||
134 | |||
135 | /** |
||
136 | * Gets the employer name. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getEmployeeRole() |
||
144 | |||
145 | /** |
||
146 | * Get the employer department. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getDepartment() |
||
154 | |||
155 | /** |
||
156 | * Execute the login on the server and returns the server response. |
||
157 | * |
||
158 | * @return IHttpResponse |
||
159 | */ |
||
160 | private function executeLogin() |
||
168 | |||
169 | /** |
||
170 | * Check if the company has external access on the Ahgora system. |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | private function checkAccessEnabled() |
||
180 | |||
181 | /** |
||
182 | * Check the return of the login action. |
||
183 | * How it works: If statusCode 200 and no body, login ok, otherwise, login failed. |
||
184 | * Should return a json with property "r" with "error" and "text" with the message |
||
185 | * |
||
186 | * @param IHttpResponse $response |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | private function checkLoginStatus(IHttpResponse $response) |
||
200 | |||
201 | /** |
||
202 | * Check if the response can be decoded as json, has the property r and r is 'success'. |
||
203 | * |
||
204 | * @param IHttpResponse $response |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | private function getResponseLoginStatus(IHttpResponse $response) |
||
220 | |||
221 | /** |
||
222 | * Safely set if the user is loggedin or not. |
||
223 | * Did a separate method do eventually trigger events, if necessary. |
||
224 | * |
||
225 | * @param bool $loggedIn |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | private function setLoggedIn($loggedIn = true) |
||
240 | |||
241 | /** |
||
242 | * Build the company url string. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | private function companyUrl() |
||
253 | |||
254 | /** |
||
255 | * Build the login url. |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | private function loginUrl() |
||
266 | |||
267 | /** |
||
268 | * Get the built punchsUrl with the given month and year. |
||
269 | * |
||
270 | * @param int $month |
||
271 | * @param int $year |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | private function punchsUrl($month, $year) |
||
283 | |||
284 | /** |
||
285 | * Make the request to the punchs page of the requested time period. |
||
286 | * |
||
287 | * @param int $month |
||
288 | * @param int $year |
||
289 | * |
||
290 | * @return IHttpResponse |
||
291 | */ |
||
292 | private function getPunchsPage($month, $year) |
||
296 | |||
297 | /** |
||
298 | * Get the punches from the given response of the punchs page. |
||
299 | * |
||
300 | * @param IHttpResponse $punchsPageResponse |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | private function getPunchsFromPage(IHttpResponse $punchsPageResponse) |
||
316 | |||
317 | private function parsePunchsPage(IHttpResponse $punchsPageResponse) |
||
343 | |||
344 | private function getPunchsTableHtml(IHttpResponse $punchsPageResponse) |
||
351 | |||
352 | /** |
||
353 | * Get both tables and return the strings into an array with the properties 'summary' and 'punchs'. |
||
354 | * |
||
355 | * @param IHttpResponse $punchsPageResponse |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | private function getPageTables(IHttpResponse $punchsPageResponse) |
||
372 | |||
373 | /** |
||
374 | * Retrive all the punches for the given string. |
||
375 | * |
||
376 | * @param string $punchsStr |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | private function parsePunchs($punchsStr) |
||
389 | |||
390 | private function createPunchesDate($date, array $punches = []) |
||
399 | |||
400 | } |
||
401 |
If you suppress an error, we recommend checking for the error condition explicitly: