Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Connection |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Sotre only one instance of this class |
||
| 44 | * @var instance |
||
| 45 | * @access private |
||
| 46 | * @since GIT: 0.1.0 |
||
| 47 | */ |
||
| 48 | private static $_instance; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * API version that will be defined in the request |
||
| 52 | * The freeIPA returns VersionError if diferent version of server version is sent |
||
| 53 | * and a warning if the version is not sent |
||
| 54 | * |
||
| 55 | * @var string|null api_version API version that will be sent in each requisition |
||
| 56 | * @access private |
||
| 57 | * @since GIT: 0.1.0 |
||
| 58 | */ |
||
| 59 | protected $api_version = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var mixed cURL handler |
||
| 63 | * @access public |
||
| 64 | * @since GIT: 0.1.0 |
||
| 65 | */ |
||
| 66 | protected $curl_handler = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var bool curl_initiated if cURL was initiated or not |
||
| 70 | * @access protected |
||
| 71 | * @since GIT: 0.1.0 |
||
| 72 | */ |
||
| 73 | protected $curl_initiated = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool curl_debug if cURL will be initiated with debug or not |
||
| 77 | * @access private |
||
| 78 | * @since GIT: 0.1.0 |
||
| 79 | */ |
||
| 80 | protected $curl_debug = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string|null curl_response Stores response/return of cURL |
||
| 84 | * @access protected |
||
| 85 | * @since GIT: 0.1.0 |
||
| 86 | */ |
||
| 87 | protected $curl_response = null; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int curl_timeout Timeout for cURL connection |
||
| 91 | * @access public |
||
| 92 | * @sice GIT 0.1.0 |
||
| 93 | */ |
||
| 94 | protected $curl_timeout = 10; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string|null cookie_file Full path for file that will stores cookie |
||
| 98 | * @access private |
||
| 99 | * @sice GIT 0.1.0 |
||
| 100 | */ |
||
| 101 | protected $cookie_file = null; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string|null cookie_string String that contains cookie for use in cURL. workaround_for_auth |
||
| 105 | * @access private |
||
| 106 | * @sice GIT 0.1.0 |
||
| 107 | */ |
||
| 108 | protected $cookie_string = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var string|null certificate_file Full path of certificate file for use in connections with the server |
||
| 112 | * @access public |
||
| 113 | * @sice GIT 0.1.0 |
||
| 114 | */ |
||
| 115 | protected $certificate_file = null; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array curl_http_header HTTP header that will be used with cURL |
||
| 119 | * @access public |
||
| 120 | * @sice GIT 0.1.0 |
||
| 121 | */ |
||
| 122 | protected $curl_http_header = array(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var string|null ipa_server IP address or hostname of freeIPA server |
||
| 126 | * @access protected |
||
| 127 | * @sice GIT 0.1.0 |
||
| 128 | */ |
||
| 129 | protected $ipa_server = null; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string|null jsonrpc_url URL where the server accept json RPC connections |
||
| 133 | * @access protected |
||
| 134 | * @sice GIT 0.1.0 |
||
| 135 | */ |
||
| 136 | protected $jsonrpc_url = null; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string|null jsonrpc_login_url URL where the server accept loggin connections |
||
| 140 | * @access protected |
||
| 141 | * @sice GIT 0.1.0 |
||
| 142 | */ |
||
| 143 | protected $jsonrpc_login_url = null; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var bool user_logged If user made login or not |
||
| 147 | * @access protected |
||
| 148 | * @sice GIT 0.1.0 |
||
| 149 | */ |
||
| 150 | protected $user_logged = false; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var string|null $json_request String that contains the last json request what will be (or was) sent to the server |
||
| 154 | * @access protected |
||
| 155 | * @since GIT: 0.1.0 |
||
| 156 | */ |
||
| 157 | protected $json_request = null; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var string|null $json_response String that contains the last json response from server |
||
| 161 | * @access protected |
||
| 162 | * @since GIT: 0.1.0 |
||
| 163 | */ |
||
| 164 | protected $json_response = null; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var array Stores information about a previous authentication |
||
| 168 | * @see authenticate() |
||
| 169 | * @see getAuthenticationInfo() |
||
| 170 | * @since GIT: 0.2.0 |
||
| 171 | */ |
||
| 172 | protected $authentication_info = array(); |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Executa ações necessárias ao início do uso de uma instância desta classe. |
||
| 177 | * Por favor, note que o servidor e certificado não são obrigatórios ao instanciar a classe, |
||
| 178 | * mas são obrigatórios em diversos métodos. |
||
| 179 | * |
||
| 180 | * <code> |
||
| 181 | * $ipa = FreeIPA\APIAccess\Connection::getInstance('192.168.0.5', '/tmp/certificate.crt'); |
||
| 182 | * $ipa2 = FreeIPA\APIAccess\Connection::getInstance(); |
||
| 183 | * $ipa2->setIPAServer('192.168.0.5'); |
||
| 184 | * $ipa2->setCertificateFile('/tmp/certificado.crt'); |
||
| 185 | * </code> |
||
| 186 | * |
||
| 187 | * @param string|null $server address (IP or hostname) of server |
||
| 188 | * @param string|null $certificate full path of server certificate |
||
| 189 | * @return void |
||
|
|
|||
| 190 | * @sice GIT 0.1.0 |
||
| 191 | * @see getInstance() |
||
| 192 | * @see setIPAServer() |
||
| 193 | * @see setCertificateFile() |
||
| 194 | * @throws \Exception caso o módulo não esteja instalado curl |
||
| 195 | * @throws \Exception caso o método setIPAServer() retorne false |
||
| 196 | * @throws \Exception caso o método setCertificateFile() retorne false |
||
| 197 | */ |
||
| 198 | private function __construct($server = null, $certificate = null) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * This a Singleton class |
||
| 215 | * |
||
| 216 | * @since GIT: 0.1.0 |
||
| 217 | */ |
||
| 218 | private function __clone() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * This is a Singlton class |
||
| 225 | * |
||
| 226 | * @since GIT: 0.1.0 |
||
| 227 | */ |
||
| 228 | private function __wakeup() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * To finalize a instance of this class |
||
| 235 | * |
||
| 236 | * @param void |
||
| 237 | * @return void |
||
| 238 | * @sice GIT 0.1.0 |
||
| 239 | */ |
||
| 240 | public function __destruct() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * |
||
| 248 | * @param type $server address (IP or hostname) of server |
||
| 249 | * @param type $certificate full path of server certificate |
||
| 250 | * @param type $force_new if true, a new instance is returned (breaking the Singleton) |
||
| 251 | * @return type |
||
| 252 | * @return instance of this class |
||
| 253 | * @since GIT: 0.1.0 |
||
| 254 | * @version GIT: 0.1.0 |
||
| 255 | */ |
||
| 256 | public static function getInstance($server = null, $certificate = null, $force_new = false) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Define a version that will be used in json sent to the server. The server will refuse |
||
| 274 | * requests from API that are greater than him |
||
| 275 | * |
||
| 276 | * @param string |
||
| 277 | * @return void |
||
| 278 | * @sice GIT 0.1.0 |
||
| 279 | * @version GIT: 0.1.0 |
||
| 280 | * @see getAPIVersion() |
||
| 281 | */ |
||
| 282 | private function setAPIVersion($version) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get the API version that is being used in this class |
||
| 289 | * |
||
| 290 | * @param void |
||
| 291 | * @return string |
||
| 292 | * @sice GIT 0.1.0 |
||
| 293 | * @version GIT: 0.2.0 |
||
| 294 | * @see setAPIVersion() |
||
| 295 | */ |
||
| 296 | private function getAPIVersion() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Define the server address (IP or hostname) |
||
| 303 | * |
||
| 304 | * @param string $host endereço (IP ou hostname) do servidor |
||
| 305 | * @return bool |
||
| 306 | * @sice GIT 0.1.0 |
||
| 307 | * @version GIT: 0.1.0 |
||
| 308 | * @see __construct() |
||
| 309 | * @see getIPAServer() |
||
| 310 | */ |
||
| 311 | public function setIPAServer($host = null) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get the server address (IP or hostname) |
||
| 324 | * |
||
| 325 | * @param void |
||
| 326 | * @return string|bool |
||
| 327 | * @sice GIT 0.1.0 |
||
| 328 | * @version GIT: 0.1.0 |
||
| 329 | * @see setIPAServer() |
||
| 330 | */ |
||
| 331 | public function getIPAServer() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Define the full path of certificate file |
||
| 338 | * |
||
| 339 | * @param string $file full path of certificate file |
||
| 340 | * @return bool false if the file is not stated nor string. True in success |
||
| 341 | * @sice GIT 0.1.0 |
||
| 342 | * @version GIT: 0.1.0 |
||
| 343 | * @see __construct() |
||
| 344 | * @see getCertificateFile() |
||
| 345 | * @throws \Exception if the file does not exist or can't be read |
||
| 346 | */ |
||
| 347 | public function setCertificateFile($file) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the full path of certificate file |
||
| 362 | * |
||
| 363 | * @return string|bool |
||
| 364 | * @sice GIT 0.1.0 |
||
| 365 | * @version GIT: 0.1.0 |
||
| 366 | * @see setCertificateFile() |
||
| 367 | */ |
||
| 368 | public function getCertificateFile() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Define the string returned by cURL |
||
| 375 | * |
||
| 376 | * @param string $string string returned by cURL |
||
| 377 | * @return void |
||
| 378 | * @sice GIT 0.1.0 |
||
| 379 | * @version GIT: 0.1.0 |
||
| 380 | * @see getCurlReturn() |
||
| 381 | */ |
||
| 382 | public function setCurlReturn($string = null) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the string returned by cURL |
||
| 389 | * |
||
| 390 | * @param void |
||
| 391 | * @return string|bool |
||
| 392 | * @sice GIT 0.1.0 |
||
| 393 | * @version GIT: 0.1.0 |
||
| 394 | * @see setCurlReturn() |
||
| 395 | */ |
||
| 396 | public function getCurlReturn() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the string of last json request (or the one that will be made) to the server |
||
| 403 | * |
||
| 404 | * @param void |
||
| 405 | * @return string|null |
||
| 406 | * @see buildJsonRequest() |
||
| 407 | * @since GIT: 0.1.0 |
||
| 408 | * @version GIT: 0.1.0 |
||
| 409 | */ |
||
| 410 | public function getJsonRequest() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get the string of last json return of freeIPA server |
||
| 417 | * |
||
| 418 | * @param void |
||
| 419 | * @return string|null |
||
| 420 | * @since GIT: 0.1.0 |
||
| 421 | * @version GIT: 0.1.0 |
||
| 422 | */ |
||
| 423 | public function getJsonResponse() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get the cURL handler with options already defined |
||
| 430 | * |
||
| 431 | * @param bool $force force cURL to be initiated again |
||
| 432 | * @return mixed cURL handler |
||
| 433 | * @sice GIT 0.1.0 |
||
| 434 | * @version GIT: 0.1.0 |
||
| 435 | * @see endCurl() |
||
| 436 | */ |
||
| 437 | public function startCurl($force = false) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Close the cURL handler |
||
| 483 | * |
||
| 484 | * @param void |
||
| 485 | * @return void |
||
| 486 | * @sice GIT 0.1.0 |
||
| 487 | * @version GIT: 0.1.0 |
||
| 488 | * @see startCurl() |
||
| 489 | */ |
||
| 490 | public function endCurl() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Aid in cURL debug. Must be used in the place of startCurl() |
||
| 498 | * |
||
| 499 | * @param void |
||
| 500 | * @return Manipulador (handler) para o cURL |
||
| 501 | * @sice GIT 0.1.0 |
||
| 502 | * @version GIT: 0.1.0 |
||
| 503 | * @see startCurl() |
||
| 504 | * @todo need improvements |
||
| 505 | */ |
||
| 506 | // public function debugCurl() |
||
| 507 | // { |
||
| 508 | // $this->startCurl(); |
||
| 509 | // print PHP_EOL . '<br/>Debug do curl ativado<br/>' . PHP_EOL; |
||
| 510 | // $curl_options = array( |
||
| 511 | // // Verbosity |
||
| 512 | // CURLOPT_VERBOSE => true, |
||
| 513 | // // Include header in the response |
||
| 514 | // CURLOPT_HEADER => true, |
||
| 515 | // // true to output SSL certification information to STDERR on secure transfers. |
||
| 516 | // CURLOPT_CERTINFO => true, |
||
| 517 | // // |
||
| 518 | // CURLINFO_HEADER_OUT => true, |
||
| 519 | // ); |
||
| 520 | // $this->curl_debug = true; |
||
| 521 | // return curl_setopt_array($this->curl_handler, $curl_options); |
||
| 522 | // } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * If a previous use of cURL has generated an error |
||
| 526 | * |
||
| 527 | * @param void |
||
| 528 | * @return bool |
||
| 529 | * @sice GIT 0.1.0 |
||
| 530 | * @version GIT: 0.1.0 |
||
| 531 | */ |
||
| 532 | public function curlHaveError() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Return an array that contains the message and error number of last cURL error |
||
| 539 | * |
||
| 540 | * @param void |
||
| 541 | * @return array |
||
| 542 | * @sice GIT 0.1.0 |
||
| 543 | * @version GIT: 0.1.0 |
||
| 544 | * @link http://curl.haxx.se/libcurl/c/libcurl-errors.html |
||
| 545 | */ |
||
| 546 | public function getCurlError() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Return an array that contains cURL information |
||
| 556 | * |
||
| 557 | * @param void |
||
| 558 | * @return array |
||
| 559 | * @sice GIT 0.1.0 |
||
| 560 | * @link http://php.net/manual/en/function.curl-getinfo.php |
||
| 561 | */ |
||
| 562 | public function getCurlInfo() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Execute a statement with cURL handler. |
||
| 569 | * In error, use the method getCurlError to obtain more information e |
||
| 570 | * getCurlReturn to obtain the cURL response |
||
| 571 | * |
||
| 572 | * @param void |
||
| 573 | * @return string|bool return false in error or HTTP response code |
||
| 574 | * @sice GIT 0.1.0 |
||
| 575 | * @version GIT: 0.1.0 |
||
| 576 | * @see getCurlError() |
||
| 577 | * @see getCurlReturn() |
||
| 578 | * @link http://php.net/manual/en/function.curl-exec.php |
||
| 579 | * @TODO without certificate the $http_code é 0 e nenhum output é gerado |
||
| 580 | */ |
||
| 581 | public function curlExec() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Try to authenticate the user and password in the server through URL |
||
| 598 | * defined in $jsonrpc_login_url |
||
| 599 | * |
||
| 600 | * @param string $user |
||
| 601 | * @param string $password |
||
| 602 | * @return bool |
||
| 603 | * @sice GIT 0.1.0 |
||
| 604 | * @version GIT: 0.2.0 |
||
| 605 | * @throws \Exception if cURL has error |
||
| 606 | * @throws \Exception if $this->ipa_server is invalid |
||
| 607 | * @throws \Exception if $this->certificate_file is invalid |
||
| 608 | * @throws \Exception if unable to find the session cookie. workaround_for_auth |
||
| 609 | * @see docs/return_samples/authentication.txt |
||
| 610 | * @TODO this method contains a workaround_for_auth |
||
| 611 | */ |
||
| 612 | public function authenticate($user = null, $password = null) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Get information about a previous authentication through |
||
| 729 | * authenticate() method |
||
| 730 | * |
||
| 731 | * $return |
||
| 732 | * ['authenticate'] bool if user is authenticated |
||
| 733 | * ['reason'] string the reason of the last action |
||
| 734 | * ['message'] string with the message generated for the last action |
||
| 735 | * ['http_code'] HTTP code for the response |
||
| 736 | * |
||
| 737 | * @return array $return see description above |
||
| 738 | * @since GIT: 0.2.0 |
||
| 739 | * @version GIT: 0.2.0 |
||
| 740 | */ |
||
| 741 | public function getAuthenticationInfo() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Retorna bool que diz se o usuário está logado ou não |
||
| 748 | * |
||
| 749 | * @param void |
||
| 750 | * @return bool |
||
| 751 | * @sice GIT 0.1.0 |
||
| 752 | * @version GIT: 0.1.0 |
||
| 753 | */ |
||
| 754 | public function userLogged() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Checks if a variable is a associative array |
||
| 761 | * |
||
| 762 | * @param array $var |
||
| 763 | * @param bool $force if true array must be associative. If false, must be associative only if not empty |
||
| 764 | * @return bool |
||
| 765 | * @link http://php.net/manual/en/function.is-array.php#89332 |
||
| 766 | * @since GIT: 0.1.0 |
||
| 767 | * @version GIT: 0.1.0 |
||
| 768 | */ |
||
| 769 | public function isAssociativeArray($var, $force = true) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Returns a json string in the format required by FreeIPA |
||
| 784 | * |
||
| 785 | * @param string $method required parameter that defines the method that will be executed in the server |
||
| 786 | * @param array $args arguments for the method |
||
| 787 | * @param array $options options for the method |
||
| 788 | * @return string|bool returns false if there is error in passed parameters |
||
| 789 | * @sice GIT 0.1.0 |
||
| 790 | * @version GIT: 0.1.0 |
||
| 791 | * @link http://php.net/manual/en/function.json-encode.php |
||
| 792 | */ |
||
| 793 | public function buildJsonRequest($method = null, $args = array(), $options = array()) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Sends requests for the freeIPA server using the previous established session |
||
| 829 | * and stores the return in $this->json_response |
||
| 830 | * With this method is possible to make requests for any freeIPA API method |
||
| 831 | * |
||
| 832 | * @param string $method required parameter that defines the method that will be executed in the server |
||
| 833 | * @param array $params arguments for the method |
||
| 834 | * @param array $options options for the method |
||
| 835 | * @param bool $exceptionInError if true, will lauch \Exception if error field in response comes filled |
||
| 836 | * @return array with response object (comes of json_decode()) and http code of response |
||
| 837 | * @sice GIT 0.1.0 |
||
| 838 | * @version GIT: 0.1.0 |
||
| 839 | * @throws \Exception if user is not logged in |
||
| 840 | * @throws \Exception if has error while create request |
||
| 841 | * @throws \Exception if has error while define cURL options or make a request |
||
| 842 | * @throws \Exception if a http code of response is not 200 |
||
| 843 | * @throws \Exception if json response is empty |
||
| 844 | * @throws \Exception (if $exceptionInError is true) with description and number of error if json returns error |
||
| 845 | * @see userLogged() |
||
| 846 | * @see buildJsonRequest() |
||
| 847 | * @see $json_response |
||
| 848 | * @see ../../docs/return_samples/invalid_json_request.txt |
||
| 849 | * @link http://php.net/manual/en/function.json-decode.php |
||
| 850 | */ |
||
| 851 | public function buildRequest($method = null, $params = array(), $options = array(), $exceptionInError = true) |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Makes a ping in FreeIPA server through of api |
||
| 900 | * |
||
| 901 | * @param bool if $return_string is true, will return the summary field of json response |
||
| 902 | * @return string|bool true if success or string if $return_string is true |
||
| 903 | * @sice GIT 0.1.0 |
||
| 904 | * @version GIT: 0.1.0 |
||
| 905 | * @see ../../docs/return_samples/ping.txt |
||
| 906 | */ |
||
| 907 | public function ping($return_string = false) |
||
| 920 | |||
| 921 | } |
||
| 922 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.