1 | <?php |
||
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) |
|
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) |
||
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) |
||
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) |
||
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() |
||
114 | |||
115 | /** |
||
116 | * Execute the login on the server and returns the server response. |
||
117 | * |
||
118 | * @return IHttpResponse |
||
119 | */ |
||
120 | private function executeLogin() |
||
128 | |||
129 | /** |
||
130 | * Check if the company has external access on the Ahgora system. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | private function checkAccessEnabled() |
||
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) |
||
162 | |||
163 | private function getResponseLoginStatus(IHttpResponse $response) |
||
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) |
||
195 | |||
196 | /** |
||
197 | * Build the company url string. |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | private function companyUrl() |
||
208 | |||
209 | /** |
||
210 | * Build the login url. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | private function loginUrl() |
||
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) |
||
234 | |||
235 | /** |
||
236 | * Gets the employer name. |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getEmployeeRole() |
||
244 | |||
245 | /** |
||
246 | * Get the employer department. |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getDepartment() |
||
254 | } |
||
255 |