1 | <?php |
||
10 | class Api |
||
11 | { |
||
12 | use Loggable; |
||
13 | |||
14 | const AHGORA_BASE_URL = 'https://www.ahgora.com.br'; |
||
15 | const AHGORA_COMPANY_URL = '%s/externo/index/%s'; |
||
16 | const AHGORA_LOGIN_URL = '%s/externo/login'; |
||
17 | |||
18 | /** @var \Katapoka\Ahgora\IHttpClient */ |
||
19 | private $httpClient; |
||
20 | /** @var string */ |
||
21 | private $password; |
||
22 | /** @var string */ |
||
23 | private $companyId; |
||
24 | /** @var string */ |
||
25 | private $username; |
||
26 | /** @var bool */ |
||
27 | private $loggedIn = false; |
||
28 | |||
29 | /** |
||
30 | * Api constructor. |
||
31 | * |
||
32 | * @param IHttpClient $httpClient |
||
33 | */ |
||
34 | 1 | public function __construct(IHttpClient $httpClient) |
|
39 | |||
40 | /** |
||
41 | * Set the company id of the ahgora system. |
||
42 | * |
||
43 | * @param string $companyId |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function setCompanyId($companyId) |
||
54 | |||
55 | /** |
||
56 | * Set the username of the employee, from the company set at the setCompanyId. |
||
57 | * |
||
58 | * @param string $username |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function setUsername($username) |
||
69 | |||
70 | /** |
||
71 | * Set the password of the employee, from the company set at the setCompanyId. |
||
72 | * |
||
73 | * @param string $password |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setPassword($password) |
||
84 | |||
85 | /** |
||
86 | * Try to execute the login on the page. |
||
87 | * To execute some actions the user needs to be loggedin. |
||
88 | * After a successful login, the status loggedin is saved as true. |
||
89 | * |
||
90 | * @return bool Returns true if the login was successful and false otherwise |
||
91 | */ |
||
92 | public function doLogin() |
||
118 | |||
119 | /** |
||
120 | * Check the return of the login action. |
||
121 | * How it works: If statusCode 200 and no body, login ok, otherwise, login failed. |
||
122 | * Should return a json with property "r" with "error" and "text" with the message |
||
123 | * |
||
124 | * @param HttpResponse $response |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | private function checkLoginStatus(HttpResponse $response) |
||
143 | |||
144 | /** |
||
145 | * Safely decodes a json. |
||
146 | * |
||
147 | * @param string $string |
||
148 | * @param bool $asArray |
||
149 | * |
||
150 | * @throws InvalidArgumentException |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | private function safeJsonDecode($string, $asArray = false) |
||
164 | |||
165 | /** |
||
166 | * Safely set if the user is loggedin or not. |
||
167 | * Did a separate method do eventually trigger events, if necessary. |
||
168 | * |
||
169 | * @param bool $loggedIn |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | private function setLoggedIn($loggedIn = true) |
||
184 | |||
185 | /** |
||
186 | * Build the company url string. |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | private function companyUrl() |
||
197 | |||
198 | /** |
||
199 | * Build the login url. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | private function loginUrl() |
||
210 | } |
||
211 |