| Total Complexity | 61 |
| Total Lines | 780 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Connections 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.
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 Connections, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Connections extends AbstractionController |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | private $identity; |
||
|
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * @var EntityAdapter |
||
| 27 | */ |
||
| 28 | private $usersEntity; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var EntityAdapter |
||
| 32 | */ |
||
| 33 | private $connectionTypesEntity; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var EntityAdapter |
||
| 37 | */ |
||
| 38 | private $connectionFieldsEntity; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var EntityAdapter |
||
| 42 | */ |
||
| 43 | private $userConnectionEntity; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var EntityAdapter |
||
| 47 | */ |
||
| 48 | private $userConnectionDetailsEntity; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return integer |
||
| 52 | */ |
||
| 53 | private function getIdentity() |
||
| 54 | { |
||
| 55 | $config = include 'module/Auth/config/user.config.php'; |
||
| 56 | $method = $config["authentication"]["method"]; |
||
| 57 | $key = $config["authentication"]["key"]; |
||
| 58 | |||
| 59 | switch ($method) |
||
| 60 | { |
||
| 61 | case '_COOKIE': |
||
| 62 | |||
| 63 | $user = $this->getUsersEntity()->select([ |
||
| 64 | "USERNAME" => $_COOKIE[$key] |
||
| 65 | ]); |
||
| 66 | |||
| 67 | break; |
||
| 68 | |||
| 69 | case '_SESSION': |
||
| 70 | |||
| 71 | $user = $this->getUsersEntity()->select([ |
||
| 72 | "USERNAME" => $_SESSION[$key] |
||
| 73 | ]); |
||
| 74 | |||
| 75 | break; |
||
| 76 | } |
||
| 77 | |||
| 78 | $user = array_shift($user); |
||
| 79 | |||
| 80 | return $user->USER_ID; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return UsersEntity |
||
| 85 | */ |
||
| 86 | private function getUsersEntity() |
||
| 87 | { |
||
| 88 | if (!is_null($this->usersEntity)) |
||
| 89 | return $this->usersEntity; |
||
| 90 | |||
| 91 | $this->usersEntity = new EntityAdapter(new TableGateway(new UserModel())); |
||
| 92 | |||
| 93 | return $this->usersEntity; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return EntityAdapter |
||
| 98 | */ |
||
| 99 | private function getConnectionTypesEntity() |
||
| 100 | { |
||
| 101 | if (!is_null($this->connectionTypesEntity)) |
||
| 102 | return $this->connectionTypesEntity; |
||
| 103 | |||
| 104 | $this->connectionTypesEntity = new EntityAdapter(new TableGateway(new ConnectionType())); |
||
| 105 | |||
| 106 | return $this->connectionTypesEntity; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return EntityAdapter |
||
| 111 | */ |
||
| 112 | private function getConnectionFieldsEntity() |
||
| 113 | { |
||
| 114 | if (!is_null($this->connectionFieldsEntity)) |
||
| 115 | return $this->connectionFieldsEntity; |
||
| 116 | |||
| 117 | $this->connectionFieldsEntity = new EntityAdapter(new TableGateway(new ConnectionTypeField())); |
||
| 118 | |||
| 119 | return $this->connectionFieldsEntity; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return EntityAdapter |
||
| 124 | */ |
||
| 125 | private function getUserConnectionEntity() |
||
| 126 | { |
||
| 127 | if (!is_null($this->userConnectionEntity)) |
||
| 128 | return $this->userConnectionEntity; |
||
| 129 | |||
| 130 | $this->userConnectionEntity = new EntityAdapter(new UserConnectionsTable(new UserConnection())); |
||
| 131 | |||
| 132 | return $this->userConnectionEntity; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return EntityAdapter |
||
| 137 | */ |
||
| 138 | private function getUserConnectionDetailsEntity() |
||
| 139 | { |
||
| 140 | if (!is_null($this->userConnectionDetailsEntity)) |
||
| 141 | return $this->userConnectionDetailsEntity; |
||
| 142 | |||
| 143 | $this->userConnectionDetailsEntity = new EntityAdapter(new TableGateway(new UserConnectionDetails())); |
||
| 144 | |||
| 145 | return $this->userConnectionDetailsEntity; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Lists all user connections |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function list() |
||
| 154 | { |
||
| 155 | # data to send |
||
| 156 | $data = array(); |
||
| 157 | |||
| 158 | # environment settings |
||
| 159 | $this->setTerminal(true); # set terminal |
||
| 160 | |||
| 161 | # TRY-CATCH-BLOCK |
||
| 162 | try { |
||
| 163 | |||
| 164 | # STANDARD VALIDATIONS [check method] |
||
| 165 | if (!$this->isGet()) |
||
| 166 | { |
||
| 167 | $http = new Http(); |
||
| 168 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
| 169 | |||
| 170 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
| 171 | } |
||
| 172 | |||
| 173 | $data["connections"] = $this->getUserConnectionEntity()->select([ |
||
| 174 | "USER_ID" => $this->getIdentity(), |
||
| 175 | "STATE" => "A" |
||
| 176 | ]); |
||
| 177 | |||
| 178 | # SUCCESS-MESSAGE |
||
| 179 | $data["process"] = "success"; |
||
| 180 | } |
||
| 181 | catch (\Drone\Exception\Exception $e) |
||
| 182 | { |
||
| 183 | # ERROR-MESSAGE |
||
| 184 | $data["process"] = "warning"; |
||
| 185 | $data["message"] = $e->getMessage(); |
||
| 186 | } |
||
| 187 | catch (\Exception $e) |
||
| 188 | { |
||
| 189 | $file = str_replace('\\', '', __CLASS__); |
||
| 190 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 191 | |||
| 192 | # stores the error code |
||
| 193 | if (($errorCode = $storage->store($e)) === false) |
||
| 194 | { |
||
| 195 | $errors = $storage->getErrors(); |
||
| 196 | |||
| 197 | # if error storing is not possible, handle it (internal app error) |
||
| 198 | $this->handleErrors($errors, __METHOD__); |
||
| 199 | } |
||
| 200 | |||
| 201 | $data["code"] = $errorCode; |
||
| 202 | $data["message"] = $e->getMessage(); |
||
| 203 | |||
| 204 | $config = include 'config/application.config.php'; |
||
| 205 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 206 | |||
| 207 | # redirect view |
||
| 208 | $this->setMethod('error'); |
||
| 209 | |||
| 210 | return $data; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $data; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Deletes a connection |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function delete() |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Adds a connection |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function add() |
||
| 354 | { |
||
| 355 | clearstatcache(); |
||
| 356 | session_write_close(); |
||
| 357 | |||
| 358 | # data to send |
||
| 359 | $data = array(); |
||
| 360 | |||
| 361 | # environment settings |
||
| 362 | $post = $this->getPost(); # catch $_POST |
||
| 363 | $this->setTerminal(true); # set terminal |
||
| 364 | |||
| 365 | # TRY-CATCH-BLOCK |
||
| 366 | try { |
||
| 367 | |||
| 368 | if ($this->isGet()) |
||
| 369 | { |
||
| 370 | $types = $data["types"] = $this->getConnectionTypesEntity()->select([]); |
||
| 371 | |||
| 372 | $fields = $this->getConnectionFieldsEntity()->select([]); |
||
| 373 | |||
| 374 | $fieldTypes = []; |
||
| 375 | |||
| 376 | foreach ($types as $type) |
||
| 377 | { |
||
| 378 | $fieldTypes[$type->CONN_TYPE_ID] = []; |
||
| 379 | } |
||
| 380 | |||
| 381 | foreach ($fields as $field) |
||
| 382 | { |
||
| 383 | $fieldTypes[$field->CONN_TYPE_ID][$field->CONN_IDENTI_ID] = $field; |
||
| 384 | } |
||
| 385 | |||
| 386 | $data["fieldTypes"] = $fieldTypes; |
||
| 387 | |||
| 388 | # SUCCESS-MESSAGE |
||
| 389 | $data["process"] = "register-form"; |
||
| 390 | } |
||
| 391 | else if ($this->isPost()) |
||
| 392 | { |
||
| 393 | # STANDARD VALIDATIONS [check needed arguments] |
||
| 394 | $needles = ['field', 'type', 'aliasname']; |
||
| 395 | |||
| 396 | array_walk($needles, function(&$item) use ($post) { |
||
| 397 | if (!array_key_exists($item, $post)) |
||
| 398 | { |
||
| 399 | $http = new Http(); |
||
| 400 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 401 | |||
| 402 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 403 | } |
||
| 404 | }); |
||
| 405 | |||
| 406 | $components = [ |
||
| 407 | "attributes" => [ |
||
| 408 | "type" => [ |
||
| 409 | "required" => true, |
||
| 410 | "type" => "number" |
||
| 411 | ], |
||
| 412 | "aliasname" => [ |
||
| 413 | "required" => true, |
||
| 414 | "type" => "text", |
||
| 415 | "minlength" => 2, |
||
| 416 | "maxlength" => 100 |
||
| 417 | ], |
||
| 418 | "field" => [ |
||
| 419 | "required" => false, |
||
| 420 | "type" => "text", |
||
| 421 | "minlength" => 2, |
||
| 422 | "maxlength" => 50 |
||
| 423 | ], |
||
| 424 | ], |
||
| 425 | ]; |
||
| 426 | |||
| 427 | $options = [ |
||
| 428 | "type" => [ |
||
| 429 | "label" => "Type" |
||
| 430 | ], |
||
| 431 | "aliasname" => [ |
||
| 432 | "label" => "Connection name", |
||
| 433 | "validators" => [ |
||
| 434 | "Alnum" => ["allowWhiteSpace" => true] |
||
| 435 | ], |
||
| 436 | ], |
||
| 437 | "field" => [ |
||
| 438 | "label" => "Connection Parameter" |
||
| 439 | ], |
||
| 440 | ]; |
||
| 441 | |||
| 442 | $form = new Form($components); |
||
| 443 | $form->fill($post); |
||
| 444 | |||
| 445 | $validator = new FormValidator($form, $options); |
||
| 446 | $validator->validate(); |
||
| 447 | |||
| 448 | $data["validator"] = $validator; |
||
| 449 | |||
| 450 | # form validation |
||
| 451 | if (!$validator->isValid()) |
||
| 452 | { |
||
| 453 | $data["messages"] = $validator->getMessages(); |
||
| 454 | throw new \Drone\Exception\Exception("Form validation errors", 300); |
||
| 455 | } |
||
| 456 | |||
| 457 | $this->getUserConnectionEntity()->getTableGateway()->getDriver()->getDb()->beginTransaction(); |
||
| 458 | |||
| 459 | $userConnection = new UserConnection(); |
||
| 460 | $user_conn_id = $this->getUserConnectionEntity()->getTableGateway()->getNextId(); |
||
| 461 | |||
| 462 | $userConnection->exchangeArray([ |
||
| 463 | "USER_CONN_ID" => $user_conn_id, |
||
| 464 | "USER_ID" => $this->getIdentity(), |
||
| 465 | "CONN_TYPE_ID" => $post["type"], |
||
| 466 | "CONNECTION_NAME" => $post["aliasname"], |
||
| 467 | "STATE" => 'A' |
||
| 468 | ]); |
||
| 469 | |||
| 470 | $this->getUserConnectionEntity()->insert($userConnection); |
||
| 471 | |||
| 472 | foreach ($post['field'][$post["type"]] as $field_number => $field_value) |
||
| 473 | { |
||
| 474 | $userconnectionDetails = new UserConnectionDetails(); |
||
| 475 | |||
| 476 | $userconnectionDetails->exchangeArray([ |
||
| 477 | "USER_CONN_ID" => $user_conn_id, |
||
| 478 | "CONN_IDENTI_ID" => $field_number, |
||
| 479 | "FIELD_VALUE" => $field_value |
||
| 480 | ]); |
||
| 481 | |||
| 482 | $this->getUserConnectionDetailsEntity()->insert($userconnectionDetails); |
||
| 483 | } |
||
| 484 | |||
| 485 | $this->getUserConnectionEntity()->getTableGateway()->getDriver()->getDb()->endTransaction(); |
||
| 486 | |||
| 487 | # SUCCESS-MESSAGE |
||
| 488 | $data["process"] = "process-response"; |
||
| 489 | } |
||
| 490 | else |
||
| 491 | { |
||
| 492 | $http = new Http(); |
||
| 493 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
| 494 | |||
| 495 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | catch (\Drone\Exception\Exception $e) |
||
| 499 | { |
||
| 500 | # ERROR-MESSAGE |
||
| 501 | $data["process"] = "warning"; |
||
| 502 | $data["message"] = $e->getMessage(); |
||
| 503 | } |
||
| 504 | catch (\Exception $e) |
||
| 505 | { |
||
| 506 | $file = str_replace('\\', '', __CLASS__); |
||
| 507 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 508 | |||
| 509 | # stores the error code |
||
| 510 | if (($errorCode = $storage->store($e)) === false) |
||
| 511 | { |
||
| 512 | $errors = $storage->getErrors(); |
||
| 513 | |||
| 514 | # if error storing is not possible, handle it (internal app error) |
||
| 515 | $this->handleErrors($errors, __METHOD__); |
||
| 516 | } |
||
| 517 | |||
| 518 | $data["code"] = $errorCode; |
||
| 519 | $data["message"] = $e->getMessage(); |
||
| 520 | |||
| 521 | $config = include 'config/application.config.php'; |
||
| 522 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 523 | |||
| 524 | # redirect view |
||
| 525 | $this->setMethod('error'); |
||
| 526 | |||
| 527 | return $data; |
||
| 528 | } |
||
| 529 | |||
| 530 | return $data; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Updates a connection |
||
| 535 | * |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | public function edit() |
||
| 768 | } |
||
| 769 | |||
| 770 | private function handleErrors(Array $errors, $method) |
||
| 771 | { |
||
| 772 | if (count($errors)) |
||
| 773 | { |
||
| 774 | $errorInformation = ""; |
||
| 800 | } |