|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Katapoka\Ahgora; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class responsible for getting the data from the Ahgora system. |
|
9
|
|
|
*/ |
|
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) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->httpClient = $httpClient; |
|
37
|
1 |
|
$this->debug('Api instance created'); |
|
38
|
1 |
|
} |
|
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) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->companyId = $companyId; |
|
50
|
|
|
$this->debug('Company ID set', ['company_id' => $companyId]); |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
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) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->username = $username; |
|
65
|
|
|
$this->debug('Username set', ['username' => $username]); |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
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) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->password = $password; |
|
80
|
|
|
$this->debug('Password set', ['password' => $password]); |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
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() |
|
93
|
|
|
{ |
|
94
|
|
|
$hasLoggedIn = false; |
|
95
|
|
|
$this->debug('Started login proccess'); |
|
96
|
|
|
$response = $this->httpClient->get($this->companyUrl()); |
|
97
|
|
|
$accessEnabled = stripos($response->body, 'Sua Empresa não liberou o acesso a essa ferramenta') === false; |
|
98
|
|
|
|
|
99
|
|
|
// If the company haven't enabled external access, or maybe the company id isn't correct, don't even try to continue to the login process |
|
100
|
|
|
if ($accessEnabled) { |
|
101
|
|
|
$this->debug('Company has external access enabled'); |
|
102
|
|
|
|
|
103
|
|
|
$response = $this->httpClient->post($this->loginUrl(), [ |
|
104
|
|
|
'empresa' => $this->companyId, |
|
105
|
|
|
'matricula' => $this->username, |
|
106
|
|
|
'senha' => $this->password, |
|
107
|
|
|
]); |
|
108
|
|
|
|
|
109
|
|
|
$hasLoggedIn = $this->checkLoginStatus($response); |
|
110
|
|
|
} else { |
|
111
|
|
|
$this->debug("Company hasn't external access enabled"); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->setLoggedIn($hasLoggedIn); |
|
115
|
|
|
|
|
116
|
|
|
return $hasLoggedIn; |
|
117
|
|
|
} |
|
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) |
|
129
|
|
|
{ |
|
130
|
|
|
try { |
|
131
|
|
|
if ($response->httpStatus === IHttpClient::HTTP_STATUS_OK) { |
|
132
|
|
|
$json = $this->safeJsonDecode($response->body, true); |
|
133
|
|
|
if ($json['r'] === 'success') { |
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} catch (InvalidArgumentException $iaex) { |
|
138
|
|
|
$this->error($iaex->getMessage(), ['expcetion' => $iaex]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
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) |
|
155
|
|
|
{ |
|
156
|
|
|
$json = json_decode($string, $asArray); |
|
157
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
158
|
|
|
$this->debug('Failed to debug json', ['str' => $string]); |
|
159
|
|
|
throw new InvalidArgumentException('Failed to decode json'); |
|
160
|
|
|
} else { |
|
161
|
|
|
return $json; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
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) |
|
174
|
|
|
{ |
|
175
|
|
|
if (!is_bool($loggedIn)) { |
|
176
|
|
|
throw new InvalidArgumentException('LoggedIn parameter must be boolean'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$this->debug('setLoggedIn', ['logged_in' => $loggedIn]); |
|
180
|
|
|
$this->loggedIn = $loggedIn; |
|
181
|
|
|
|
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Build the company url string. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
private function companyUrl() |
|
191
|
|
|
{ |
|
192
|
|
|
$companyUrl = sprintf(self::AHGORA_COMPANY_URL, self::AHGORA_BASE_URL, $this->companyId); |
|
193
|
|
|
$this->debug('CompanyURL', ['company_url' => $companyUrl]); |
|
194
|
|
|
|
|
195
|
|
|
return $companyUrl; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Build the login url. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
private function loginUrl() |
|
204
|
|
|
{ |
|
205
|
|
|
$loginUrl = sprintf(self::AHGORA_LOGIN_URL, self::AHGORA_BASE_URL); |
|
206
|
|
|
$this->debug('loginUrl', ['login_url' => $loginUrl]); |
|
207
|
|
|
|
|
208
|
|
|
return $loginUrl; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|