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 | /** @var HtmlPageParser */ |
||
31 | private $htmlPageParser; |
||
32 | |||
33 | /** |
||
34 | * Api constructor. |
||
35 | * |
||
36 | * @param IHttpClient $httpClient |
||
37 | */ |
||
38 | 1 | public function __construct(IHttpClient $httpClient) |
|
44 | |||
45 | /** |
||
46 | * Set the company id of the ahgora system. |
||
47 | * |
||
48 | * @param string $companyId |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setCompanyId($companyId) |
||
59 | |||
60 | /** |
||
61 | * Set the username of the employee, from the company set at the setCompanyId. |
||
62 | * |
||
63 | * @param string $username |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setUsername($username) |
||
74 | |||
75 | /** |
||
76 | * Set the password of the employee, from the company set at the setCompanyId. |
||
77 | * |
||
78 | * @param string $password |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function setPassword($password) |
||
89 | |||
90 | /** |
||
91 | * Try to execute the login on the page. |
||
92 | * To execute some actions the user needs to be loggedin. |
||
93 | * After a successful login, the status loggedin is saved as true. |
||
94 | * |
||
95 | * @return bool Returns true if the login was successful and false otherwise |
||
96 | */ |
||
97 | public function doLogin() |
||
115 | |||
116 | /** |
||
117 | * Get the punchs at the given parameters. |
||
118 | * |
||
119 | * @param int|null $month The month you want to get the punchs - Must be between 01 and 12 (both included) |
||
120 | * @param int|null $year The year you want to get the punchs |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | public function getPunchs($month = null, $year = null) |
||
137 | |||
138 | /** |
||
139 | * Gets the employer name. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 1 | public function getEmployeeRole() |
|
147 | |||
148 | /** |
||
149 | * Get the employer department. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getDepartment() |
||
157 | |||
158 | /** |
||
159 | * Retrive all the punches for the given string. |
||
160 | * |
||
161 | * @param string $punchsStr |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | private function parsePunchs($punchsStr) |
||
174 | |||
175 | /** |
||
176 | * Execute the login on the server and returns the server response. |
||
177 | * |
||
178 | * @return IHttpResponse |
||
179 | */ |
||
180 | private function executeLogin() |
||
188 | |||
189 | /** |
||
190 | * Check if the company has external access on the Ahgora system. |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | private function checkAccessEnabled() |
||
200 | |||
201 | /** |
||
202 | * Check the return of the login action. |
||
203 | * How it works: If statusCode 200 and no body, login ok, otherwise, login failed. |
||
204 | * Should return a json with property "r" with "error" and "text" with the message |
||
205 | * |
||
206 | * @param IHttpResponse $response |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | private function checkLoginStatus(IHttpResponse $response) |
||
220 | |||
221 | /** |
||
222 | * Check if the response can be decoded as json, has the property r and r is 'success'. |
||
223 | * |
||
224 | * @param IHttpResponse $response |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | private function getResponseLoginStatus(IHttpResponse $response) |
||
240 | |||
241 | /** |
||
242 | * Safely set if the user is loggedin or not. |
||
243 | * Did a separate method do eventually trigger events, if necessary. |
||
244 | * |
||
245 | * @param bool $loggedIn |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | private function setLoggedIn($loggedIn = true) |
||
260 | |||
261 | /** |
||
262 | * Build the company url string. |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | private function companyUrl() |
||
273 | |||
274 | /** |
||
275 | * Build the login url. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | private function loginUrl() |
||
286 | |||
287 | /** |
||
288 | * Get the built punchsUrl with the given month and year. |
||
289 | * |
||
290 | * @param int $month |
||
291 | * @param int $year |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | private function punchsUrl($month, $year) |
||
303 | |||
304 | /** |
||
305 | * Make the request to the punchs page of the requested time period. |
||
306 | * |
||
307 | * @param int $month |
||
308 | * @param int $year |
||
309 | * |
||
310 | * @return IHttpResponse |
||
311 | */ |
||
312 | private function getPunchsPage($month, $year) |
||
316 | |||
317 | /** |
||
318 | * Get the punches from the given response of the punchs page. |
||
319 | * |
||
320 | * @param IHttpResponse $punchsPageResponse |
||
321 | * |
||
322 | * @return array |
||
323 | */ |
||
324 | private function getPunchsFromPage(IHttpResponse $punchsPageResponse) |
||
336 | |||
337 | private function parsePunchsPage(IHttpResponse $punchsPageResponse) |
||
353 | |||
354 | /** |
||
355 | * Convert the date string and the datepunch array to an array of DateTime's. |
||
356 | * |
||
357 | * @param string $date |
||
358 | * @param array $punches |
||
359 | * |
||
360 | * @return \DateTime[] |
||
361 | */ |
||
362 | private function createPunchesDate($date, array $punches = []) |
||
371 | } |
||
372 |