Complex classes like HttpApi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HttpApi, and based on these observations, apply Extract Interface, too.
| 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 | |||
| 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() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the punchs at the given parameters. |
||
| 116 | * |
||
| 117 | * @param int|null $month The month you want to get the punchs - Must be between 01 and 12 (both included) |
||
| 118 | * @param int|null $year The year you want to get the punchs |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getPunchs($month = null, $year = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Gets the employer name. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getEmployeeRole() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the employer department. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getDepartment() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Execute the login on the server and returns the server response. |
||
| 158 | * |
||
| 159 | * @return IHttpResponse |
||
| 160 | */ |
||
| 161 | private function executeLogin() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if the company has external access on the Ahgora system. |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | private function checkAccessEnabled() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check the return of the login action. |
||
| 184 | * How it works: If statusCode 200 and no body, login ok, otherwise, login failed. |
||
| 185 | * Should return a json with property "r" with "error" and "text" with the message |
||
| 186 | * |
||
| 187 | * @param IHttpResponse $response |
||
| 188 | * |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | private function checkLoginStatus(IHttpResponse $response) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check if the response can be decoded as json, has the property r and r is 'success'. |
||
| 204 | * |
||
| 205 | * @param IHttpResponse $response |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | private function getResponseLoginStatus(IHttpResponse $response) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Safely set if the user is loggedin or not. |
||
| 224 | * Did a separate method do eventually trigger events, if necessary. |
||
| 225 | * |
||
| 226 | * @param bool $loggedIn |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | private function setLoggedIn($loggedIn = true) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Build the company url string. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | private function companyUrl() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Build the login url. |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | private function loginUrl() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the built punchsUrl with the given month and year. |
||
| 270 | * |
||
| 271 | * @param int $month |
||
| 272 | * @param int $year |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | private function punchsUrl($month, $year) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Make the request to the punchs page of the requested time period. |
||
| 287 | * |
||
| 288 | * @param int $month |
||
| 289 | * @param int $year |
||
| 290 | * |
||
| 291 | * @return IHttpResponse |
||
| 292 | */ |
||
| 293 | private function getPunchsPage($month, $year) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get the punches from the given response of the punchs page. |
||
| 300 | * |
||
| 301 | * @param IHttpResponse $punchsPageResponse |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | private function getPunchsFromPage(IHttpResponse $punchsPageResponse) |
||
| 317 | |||
| 318 | private function parsePunchsPage(IHttpResponse $punchsPageResponse) |
||
| 346 | |||
| 347 | private function getPunchsTableHtml(IHttpResponse $punchsPageResponse) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get both tables and return the strings into an array with the properties 'summary' and 'punchs'. |
||
| 357 | * |
||
| 358 | * @param IHttpResponse $punchsPageResponse |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | private function getPageTables(IHttpResponse $punchsPageResponse) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Retrive all the punches for the given string. |
||
| 378 | * |
||
| 379 | * @param string $punchsStr |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | private function parsePunchs($punchsStr) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Convert the date string and the datepunch array to an array of DateTime's. |
||
| 395 | * |
||
| 396 | * @param string $date |
||
| 397 | * @param array $punches |
||
| 398 | * |
||
| 399 | * @return \DateTime[] |
||
| 400 | */ |
||
| 401 | private function createPunchesDate($date, array $punches = []) |
||
| 410 | |||
| 411 | } |
||
| 412 |