| Total Complexity | 96 |
| Total Lines | 558 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ControllerCustomerCustomField 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 ControllerCustomerCustomField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ControllerCustomerCustomField extends \Divine\Engine\Core\Controller |
||
|
|
|||
| 24 | { |
||
| 25 | private $error = array(); |
||
| 26 | |||
| 27 | public function index() |
||
| 28 | { |
||
| 29 | $this->load->language('customer/custom_field'); |
||
| 30 | |||
| 31 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 32 | |||
| 33 | $this->load->model('customer/custom_field'); |
||
| 34 | |||
| 35 | $this->getList(); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function add() |
||
| 39 | { |
||
| 40 | $this->load->language('customer/custom_field'); |
||
| 41 | |||
| 42 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 43 | |||
| 44 | $this->load->model('customer/custom_field'); |
||
| 45 | |||
| 46 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
| 47 | $this->model_customer_custom_field->addCustomField($this->request->post); |
||
| 48 | |||
| 49 | $this->session->data['success'] = $this->language->get('text_success'); |
||
| 50 | |||
| 51 | $url = ''; |
||
| 52 | |||
| 53 | if (isset($this->request->get['sort'])) { |
||
| 54 | $url .= '&sort=' . $this->request->get['sort']; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (isset($this->request->get['order'])) { |
||
| 58 | $url .= '&order=' . $this->request->get['order']; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (isset($this->request->get['page'])) { |
||
| 62 | $url .= '&page=' . $this->request->get['page']; |
||
| 63 | } |
||
| 64 | |||
| 65 | $this->response->redirect($this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true)); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->getForm(); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function edit() |
||
| 72 | { |
||
| 73 | $this->load->language('customer/custom_field'); |
||
| 74 | |||
| 75 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 76 | |||
| 77 | $this->load->model('customer/custom_field'); |
||
| 78 | |||
| 79 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
| 80 | $this->model_customer_custom_field->editCustomField($this->request->get['custom_field_id'], $this->request->post); |
||
| 81 | |||
| 82 | $this->session->data['success'] = $this->language->get('text_success'); |
||
| 83 | |||
| 84 | $url = ''; |
||
| 85 | |||
| 86 | if (isset($this->request->get['sort'])) { |
||
| 87 | $url .= '&sort=' . $this->request->get['sort']; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (isset($this->request->get['order'])) { |
||
| 91 | $url .= '&order=' . $this->request->get['order']; |
||
| 92 | } |
||
| 93 | |||
| 94 | if (isset($this->request->get['page'])) { |
||
| 95 | $url .= '&page=' . $this->request->get['page']; |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->response->redirect($this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true)); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->getForm(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function delete() |
||
| 105 | { |
||
| 106 | $this->load->language('customer/custom_field'); |
||
| 107 | |||
| 108 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 109 | |||
| 110 | $this->load->model('customer/custom_field'); |
||
| 111 | |||
| 112 | if (isset($this->request->post['selected']) && $this->validateDelete()) { |
||
| 113 | foreach ($this->request->post['selected'] as $custom_field_id) { |
||
| 114 | $this->model_customer_custom_field->deleteCustomField($custom_field_id); |
||
| 115 | } |
||
| 116 | |||
| 117 | $this->session->data['success'] = $this->language->get('text_success'); |
||
| 118 | |||
| 119 | $url = ''; |
||
| 120 | |||
| 121 | if (isset($this->request->get['sort'])) { |
||
| 122 | $url .= '&sort=' . $this->request->get['sort']; |
||
| 123 | } |
||
| 124 | |||
| 125 | if (isset($this->request->get['order'])) { |
||
| 126 | $url .= '&order=' . $this->request->get['order']; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (isset($this->request->get['page'])) { |
||
| 130 | $url .= '&page=' . $this->request->get['page']; |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->response->redirect($this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true)); |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->getList(); |
||
| 137 | } |
||
| 138 | |||
| 139 | protected function getList() |
||
| 331 | } |
||
| 332 | |||
| 333 | protected function getForm() |
||
| 334 | { |
||
| 335 | $data['heading_title'] = $this->language->get('heading_title'); |
||
| 336 | |||
| 337 | $data['text_form'] = !isset($this->request->get['custom_field_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); |
||
| 338 | $data['text_choose'] = $this->language->get('text_choose'); |
||
| 339 | $data['text_select'] = $this->language->get('text_select'); |
||
| 340 | $data['text_radio'] = $this->language->get('text_radio'); |
||
| 341 | $data['text_checkbox'] = $this->language->get('text_checkbox'); |
||
| 342 | $data['text_input'] = $this->language->get('text_input'); |
||
| 343 | $data['text_text'] = $this->language->get('text_text'); |
||
| 344 | $data['text_textarea'] = $this->language->get('text_textarea'); |
||
| 345 | $data['text_file'] = $this->language->get('text_file'); |
||
| 346 | $data['text_date'] = $this->language->get('text_date'); |
||
| 347 | $data['text_datetime'] = $this->language->get('text_datetime'); |
||
| 348 | $data['text_time'] = $this->language->get('text_time'); |
||
| 349 | $data['text_account'] = $this->language->get('text_account'); |
||
| 350 | $data['text_address'] = $this->language->get('text_address'); |
||
| 351 | $data['text_enabled'] = $this->language->get('text_enabled'); |
||
| 352 | $data['text_disabled'] = $this->language->get('text_disabled'); |
||
| 353 | $data['text_regex'] = $this->language->get('text_regex'); |
||
| 354 | |||
| 355 | $data['entry_name'] = $this->language->get('entry_name'); |
||
| 356 | $data['entry_location'] = $this->language->get('entry_location'); |
||
| 357 | $data['entry_type'] = $this->language->get('entry_type'); |
||
| 358 | $data['entry_value'] = $this->language->get('entry_value'); |
||
| 359 | $data['entry_validation'] = $this->language->get('entry_validation'); |
||
| 360 | $data['entry_custom_value'] = $this->language->get('entry_custom_value'); |
||
| 361 | $data['entry_customer_group'] = $this->language->get('entry_customer_group'); |
||
| 362 | $data['entry_required'] = $this->language->get('entry_required'); |
||
| 363 | $data['entry_status'] = $this->language->get('entry_status'); |
||
| 364 | $data['entry_sort_order'] = $this->language->get('entry_sort_order'); |
||
| 365 | |||
| 366 | $data['help_regex'] = $this->language->get('help_regex'); |
||
| 367 | $data['help_sort_order'] = $this->language->get('help_sort_order'); |
||
| 368 | |||
| 369 | $data['button_save'] = $this->language->get('button_save'); |
||
| 370 | $data['button_cancel'] = $this->language->get('button_cancel'); |
||
| 371 | $data['button_custom_field_value_add'] = $this->language->get('button_custom_field_value_add'); |
||
| 372 | $data['button_remove'] = $this->language->get('button_remove'); |
||
| 373 | |||
| 374 | if (isset($this->error['warning'])) { |
||
| 375 | $data['error_warning'] = $this->error['warning']; |
||
| 376 | } else { |
||
| 377 | $data['error_warning'] = ''; |
||
| 378 | } |
||
| 379 | |||
| 380 | if (isset($this->error['name'])) { |
||
| 381 | $data['error_name'] = $this->error['name']; |
||
| 382 | } else { |
||
| 383 | $data['error_name'] = array(); |
||
| 384 | } |
||
| 385 | |||
| 386 | if (isset($this->error['custom_field_value'])) { |
||
| 387 | $data['error_custom_field_value'] = $this->error['custom_field_value']; |
||
| 388 | } else { |
||
| 389 | $data['error_custom_field_value'] = array(); |
||
| 390 | } |
||
| 391 | |||
| 392 | $url = ''; |
||
| 393 | |||
| 394 | if (isset($this->request->get['sort'])) { |
||
| 395 | $url .= '&sort=' . $this->request->get['sort']; |
||
| 396 | } |
||
| 397 | |||
| 398 | if (isset($this->request->get['order'])) { |
||
| 399 | $url .= '&order=' . $this->request->get['order']; |
||
| 400 | } |
||
| 401 | |||
| 402 | if (isset($this->request->get['page'])) { |
||
| 403 | $url .= '&page=' . $this->request->get['page']; |
||
| 404 | } |
||
| 405 | |||
| 406 | $data['breadcrumbs'] = array(); |
||
| 407 | |||
| 408 | $data['breadcrumbs'][] = array( |
||
| 409 | 'text' => $this->language->get('text_home'), |
||
| 410 | 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true) |
||
| 411 | ); |
||
| 412 | |||
| 413 | $data['breadcrumbs'][] = array( |
||
| 414 | 'text' => $this->language->get('heading_title'), |
||
| 415 | 'href' => $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true) |
||
| 416 | ); |
||
| 417 | |||
| 418 | if (!isset($this->request->get['custom_field_id'])) { |
||
| 419 | $data['action'] = $this->url->link('customer/custom_field/add', 'token=' . $this->session->data['token'] . $url, true); |
||
| 420 | } else { |
||
| 421 | $data['action'] = $this->url->link('customer/custom_field/edit', 'token=' . $this->session->data['token'] . '&custom_field_id=' . $this->request->get['custom_field_id'] . $url, true); |
||
| 422 | } |
||
| 423 | |||
| 424 | $data['cancel'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true); |
||
| 425 | |||
| 426 | if (isset($this->request->get['custom_field_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { |
||
| 427 | $custom_field_info = $this->model_customer_custom_field->getCustomField($this->request->get['custom_field_id']); |
||
| 428 | } |
||
| 429 | |||
| 430 | $data['token'] = $this->session->data['token']; |
||
| 431 | |||
| 432 | $this->load->model('localisation/language'); |
||
| 433 | |||
| 434 | $data['languages'] = $this->model_localisation_language->getLanguages(); |
||
| 435 | |||
| 436 | if (isset($this->request->post['custom_field_description'])) { |
||
| 437 | $data['custom_field_description'] = $this->request->post['custom_field_description']; |
||
| 438 | } elseif (isset($this->request->get['custom_field_id'])) { |
||
| 439 | $data['custom_field_description'] = $this->model_customer_custom_field->getCustomFieldDescriptions($this->request->get['custom_field_id']); |
||
| 440 | } else { |
||
| 441 | $data['custom_field_description'] = array(); |
||
| 442 | } |
||
| 443 | |||
| 444 | if (isset($this->request->post['location'])) { |
||
| 445 | $data['location'] = $this->request->post['location']; |
||
| 446 | } elseif (!empty($custom_field_info)) { |
||
| 447 | $data['location'] = $custom_field_info['location']; |
||
| 448 | } else { |
||
| 449 | $data['location'] = ''; |
||
| 450 | } |
||
| 451 | |||
| 452 | if (isset($this->request->post['type'])) { |
||
| 453 | $data['type'] = $this->request->post['type']; |
||
| 454 | } elseif (!empty($custom_field_info)) { |
||
| 455 | $data['type'] = $custom_field_info['type']; |
||
| 456 | } else { |
||
| 457 | $data['type'] = ''; |
||
| 458 | } |
||
| 459 | |||
| 460 | if (isset($this->request->post['value'])) { |
||
| 461 | $data['value'] = $this->request->post['value']; |
||
| 462 | } elseif (!empty($custom_field_info)) { |
||
| 463 | $data['value'] = $custom_field_info['value']; |
||
| 464 | } else { |
||
| 465 | $data['value'] = ''; |
||
| 466 | } |
||
| 467 | |||
| 468 | if (isset($this->request->post['validation'])) { |
||
| 469 | $data['validation'] = $this->request->post['validation']; |
||
| 470 | } elseif (!empty($custom_field_info)) { |
||
| 471 | $data['validation'] = $custom_field_info['validation']; |
||
| 472 | } else { |
||
| 473 | $data['validation'] = ''; |
||
| 474 | } |
||
| 475 | |||
| 476 | if (isset($this->request->post['status'])) { |
||
| 477 | $data['status'] = $this->request->post['status']; |
||
| 478 | } elseif (!empty($custom_field_info)) { |
||
| 479 | $data['status'] = $custom_field_info['status']; |
||
| 480 | } else { |
||
| 481 | $data['status'] = ''; |
||
| 482 | } |
||
| 483 | |||
| 484 | if (isset($this->request->post['sort_order'])) { |
||
| 485 | $data['sort_order'] = $this->request->post['sort_order']; |
||
| 486 | } elseif (!empty($custom_field_info)) { |
||
| 487 | $data['sort_order'] = $custom_field_info['sort_order']; |
||
| 488 | } else { |
||
| 489 | $data['sort_order'] = ''; |
||
| 490 | } |
||
| 491 | |||
| 492 | if (isset($this->request->post['custom_field_value'])) { |
||
| 493 | $custom_field_values = $this->request->post['custom_field_value']; |
||
| 494 | } elseif (isset($this->request->get['custom_field_id'])) { |
||
| 495 | $custom_field_values = $this->model_customer_custom_field->getCustomFieldValueDescriptions($this->request->get['custom_field_id']); |
||
| 496 | } else { |
||
| 497 | $custom_field_values = array(); |
||
| 498 | } |
||
| 499 | |||
| 500 | $data['custom_field_values'] = array(); |
||
| 501 | |||
| 502 | foreach ($custom_field_values as $custom_field_value) { |
||
| 503 | $data['custom_field_values'][] = array( |
||
| 504 | 'custom_field_value_id' => $custom_field_value['custom_field_value_id'], |
||
| 505 | 'custom_field_value_description' => $custom_field_value['custom_field_value_description'], |
||
| 506 | 'sort_order' => $custom_field_value['sort_order'] |
||
| 507 | ); |
||
| 508 | } |
||
| 509 | |||
| 510 | if (isset($this->request->post['custom_field_customer_group'])) { |
||
| 511 | $custom_field_customer_groups = $this->request->post['custom_field_customer_group']; |
||
| 512 | } elseif (isset($this->request->get['custom_field_id'])) { |
||
| 513 | $custom_field_customer_groups = $this->model_customer_custom_field->getCustomFieldCustomerGroups($this->request->get['custom_field_id']); |
||
| 514 | } else { |
||
| 515 | $custom_field_customer_groups = array(); |
||
| 516 | } |
||
| 517 | |||
| 518 | $data['custom_field_customer_group'] = array(); |
||
| 519 | |||
| 520 | foreach ($custom_field_customer_groups as $custom_field_customer_group) { |
||
| 521 | $data['custom_field_customer_group'][] = $custom_field_customer_group['customer_group_id']; |
||
| 522 | } |
||
| 523 | |||
| 524 | $data['custom_field_required'] = array(); |
||
| 525 | |||
| 526 | foreach ($custom_field_customer_groups as $custom_field_customer_group) { |
||
| 527 | if ($custom_field_customer_group['required']) { |
||
| 528 | $data['custom_field_required'][] = $custom_field_customer_group['customer_group_id']; |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | $this->load->model('customer/customer_group'); |
||
| 533 | |||
| 534 | $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups(); |
||
| 535 | |||
| 536 | $data['header'] = $this->load->controller('common/header'); |
||
| 537 | $data['column'] = $this->load->controller('common/column_left'); |
||
| 538 | $data['footer'] = $this->load->controller('common/footer'); |
||
| 539 | |||
| 540 | $this->response->setOutput($this->load->view('customer/custom_field_form', $data)); |
||
| 541 | } |
||
| 542 | |||
| 543 | protected function validateForm() |
||
| 544 | { |
||
| 545 | if (!$this->user->hasPermission('modify', 'customer/custom_field')) { |
||
| 546 | $this->error['warning'] = $this->language->get('error_permission'); |
||
| 547 | } |
||
| 548 | |||
| 549 | foreach ($this->request->post['custom_field_description'] as $language_id => $value) { |
||
| 550 | if ((\voku\helper\UTF8::strlen($value['name']) < 1) || (\voku\helper\UTF8::strlen($value['name']) > 128)) { |
||
| 551 | $this->error['name'][$language_id] = $this->language->get('error_name'); |
||
| 552 | } |
||
| 553 | } |
||
| 554 | |||
| 555 | if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox')) { |
||
| 556 | if (!isset($this->request->post['custom_field_value'])) { |
||
| 557 | $this->error['warning'] = $this->language->get('error_type'); |
||
| 558 | } |
||
| 559 | |||
| 560 | if (isset($this->request->post['custom_field_value'])) { |
||
| 561 | foreach ($this->request->post['custom_field_value'] as $custom_field_value_id => $custom_field_value) { |
||
| 562 | foreach ($custom_field_value['custom_field_value_description'] as $language_id => $custom_field_value_description) { |
||
| 563 | if ((\voku\helper\UTF8::strlen($custom_field_value_description['name']) < 1) || (\voku\helper\UTF8::strlen($custom_field_value_description['name']) > 128)) { |
||
| 564 | $this->error['custom_field_value'][$custom_field_value_id][$language_id] = $this->language->get('error_custom_value'); |
||
| 565 | } |
||
| 566 | } |
||
| 567 | } |
||
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | return !$this->error; |
||
| 572 | } |
||
| 573 | |||
| 574 | protected function validateDelete() |
||
| 581 | } |
||
| 582 | } |
||
| 583 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.