1 | <?php |
||
14 | class HttpApi extends AbstractApi |
||
15 | { |
||
16 | const AHGORA_BASE_URL = 'https://www.ahgora.com.br'; |
||
17 | const AHGORA_COMPANY_URL = '%s/externo/index/%s'; |
||
18 | const AHGORA_LOGIN_URL = '%s/externo/login'; |
||
19 | const AHGORA_PUNCHS_URL = '%s/externo/batidas/%d-%d'; |
||
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 | /** @var HtmlPageParser */ |
||
32 | private $htmlPageParser; |
||
33 | |||
34 | /** |
||
35 | * Api constructor. |
||
36 | * |
||
37 | * @param IHttpClient $httpClient |
||
38 | */ |
||
39 | 1 | public function __construct(IHttpClient $httpClient) |
|
45 | |||
46 | /** |
||
47 | * Set the company id of the ahgora system. |
||
48 | * |
||
49 | * @param string $companyId |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function setCompanyId($companyId) |
||
60 | |||
61 | /** |
||
62 | * Set the username of the employee, from the company set at the setCompanyId. |
||
63 | * |
||
64 | * @param string $username |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setUsername($username) |
||
75 | |||
76 | /** |
||
77 | * Set the password of the employee, from the company set at the setCompanyId. |
||
78 | * |
||
79 | * @param string $password |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function setPassword($password) |
||
90 | |||
91 | /** |
||
92 | * Try to execute the login on the page. |
||
93 | * To execute some actions the user needs to be loggedin. |
||
94 | * After a successful login, the status loggedin is saved as true. |
||
95 | * |
||
96 | * @return bool Returns true if the login was successful and false otherwise |
||
97 | */ |
||
98 | public function doLogin() |
||
116 | |||
117 | /** |
||
118 | * Get the punches at the given parameters. |
||
119 | * |
||
120 | * @param int|null $month The month you want to get the punches - Must be between 01 and 12 (both included) |
||
121 | * @param int|null $year The year you want to get the punches |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getPunches($month = null, $year = null) |
||
142 | |||
143 | /** |
||
144 | * Gets the employer name. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getEmployeeRole() |
||
152 | |||
153 | /** |
||
154 | * Get the employer department. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getDepartment() |
||
162 | |||
163 | /** |
||
164 | * Retrive all the punches for the given string. |
||
165 | * |
||
166 | * @param string $punchesStr |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | private function parsePunches($punchesStr) |
||
179 | |||
180 | /** |
||
181 | * Execute the login on the server and returns the server response. |
||
182 | * |
||
183 | * @return IHttpResponse |
||
184 | */ |
||
185 | private function executeLogin() |
||
193 | |||
194 | /** |
||
195 | * Check if the company has external access on the Ahgora system. |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | private function checkAccessEnabled() |
||
205 | |||
206 | /** |
||
207 | * Check the return of the login action. |
||
208 | * How it works: If statusCode 200 and no body, login ok, otherwise, login failed. |
||
209 | * Should return a json with property "r" with "error" and "text" with the message |
||
210 | * |
||
211 | * @param IHttpResponse $response |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | private function checkLoginStatus(IHttpResponse $response) |
||
225 | |||
226 | /** |
||
227 | * Check if the response can be decoded as json, has the property r and r is 'success'. |
||
228 | * |
||
229 | * @param IHttpResponse $response |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | private function getResponseLoginStatus(IHttpResponse $response) |
||
245 | |||
246 | /** |
||
247 | * Safely set if the user is loggedin or not. |
||
248 | * Did a separate method do eventually trigger events, if necessary. |
||
249 | * |
||
250 | * @param bool $loggedIn |
||
251 | * |
||
252 | * @return $this |
||
253 | */ |
||
254 | private function setLoggedIn($loggedIn = true) |
||
265 | |||
266 | /** |
||
267 | * Build the company url string. |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | private function companyUrl() |
||
278 | |||
279 | /** |
||
280 | * Build the login url. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | private function loginUrl() |
||
291 | |||
292 | /** |
||
293 | * Get the built punchesUrl with the given month and year. |
||
294 | * |
||
295 | * @param int $month |
||
296 | * @param int $year |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | private function punchesUrl($month, $year) |
||
308 | |||
309 | /** |
||
310 | * Make the request to the punches page of the requested time period. |
||
311 | * |
||
312 | * @param int $month |
||
313 | * @param int $year |
||
314 | * |
||
315 | * @return IHttpResponse |
||
316 | */ |
||
317 | private function getPunchesPage($month, $year) |
||
321 | |||
322 | /** |
||
323 | * Get the punches from the given response of the punches page. |
||
324 | * |
||
325 | * @param IHttpResponse $punchesPageResponse |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | private function getPunchesFromPage(IHttpResponse $punchesPageResponse) |
||
341 | |||
342 | private function parsePunchesPage(IHttpResponse $punchesPageResponse) |
||
354 | |||
355 | /** |
||
356 | * Convert the date string and the datepunch array to an array of DateTime's. |
||
357 | * |
||
358 | * @param string $date |
||
359 | * @param array $punches |
||
360 | * |
||
361 | * @return \DateTime[] |
||
362 | */ |
||
363 | private function createPunchesDate($date, array $punches = []) |
||
372 | } |
||
373 |