| Total Complexity | 88 |
| Total Lines | 550 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ControllerCatalogDownload 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 ControllerCatalogDownload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ControllerCatalogDownload extends \Divine\Engine\Core\Controller |
||
|
|
|||
| 24 | { |
||
| 25 | private $error = array(); |
||
| 26 | |||
| 27 | public function index() |
||
| 28 | { |
||
| 29 | $this->load->language('catalog/download'); |
||
| 30 | |||
| 31 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 32 | |||
| 33 | $this->load->model('catalog/download'); |
||
| 34 | |||
| 35 | $this->getList(); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function add() |
||
| 39 | { |
||
| 40 | $this->load->language('catalog/download'); |
||
| 41 | |||
| 42 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 43 | |||
| 44 | $this->load->model('catalog/download'); |
||
| 45 | |||
| 46 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
| 47 | $this->model_catalog_download->addDownload($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('catalog/download', 'token=' . $this->session->data['token'] . $url, true)); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->getForm(); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function edit() |
||
| 72 | { |
||
| 73 | $this->load->language('catalog/download'); |
||
| 74 | |||
| 75 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 76 | |||
| 77 | $this->load->model('catalog/download'); |
||
| 78 | |||
| 79 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
| 80 | $this->model_catalog_download->editDownload($this->request->get['download_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('catalog/download', 'token=' . $this->session->data['token'] . $url, true)); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->getForm(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function delete() |
||
| 105 | { |
||
| 106 | $this->load->language('catalog/download'); |
||
| 107 | |||
| 108 | $this->document->setTitle($this->language->get('heading_title')); |
||
| 109 | |||
| 110 | $this->load->model('catalog/download'); |
||
| 111 | |||
| 112 | if (isset($this->request->post['selected']) && $this->validateDelete()) { |
||
| 113 | foreach ($this->request->post['selected'] as $download_id) { |
||
| 114 | $this->model_catalog_download->deleteDownload($download_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('catalog/download', 'token=' . $this->session->data['token'] . $url, true)); |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->getList(); |
||
| 137 | } |
||
| 138 | |||
| 139 | protected function getList() |
||
| 287 | } |
||
| 288 | |||
| 289 | protected function getForm() |
||
| 410 | } |
||
| 411 | |||
| 412 | protected function validateForm() |
||
| 413 | { |
||
| 414 | if (!$this->user->hasPermission('modify', 'catalog/download')) { |
||
| 415 | $this->error['warning'] = $this->language->get('error_permission'); |
||
| 416 | } |
||
| 417 | |||
| 418 | foreach ($this->request->post['download_description'] as $language_id => $value) { |
||
| 419 | if ((\voku\helper\UTF8::strlen($value['name']) < 3) || (\voku\helper\UTF8::strlen($value['name']) > 64)) { |
||
| 420 | $this->error['name'][$language_id] = $this->language->get('error_name'); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | if ((\voku\helper\UTF8::strlen($this->request->post['filename']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['filename']) > 128)) { |
||
| 425 | $this->error['filename'] = $this->language->get('error_filename'); |
||
| 426 | } |
||
| 427 | |||
| 428 | if (!is_file($_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $this->request->post['filename'])) { |
||
| 429 | $this->error['filename'] = $this->language->get('error_exists'); |
||
| 430 | } |
||
| 431 | |||
| 432 | if ((\voku\helper\UTF8::strlen($this->request->post['mask']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['mask']) > 128)) { |
||
| 433 | $this->error['mask'] = $this->language->get('error_mask'); |
||
| 434 | } |
||
| 435 | |||
| 436 | return !$this->error; |
||
| 437 | } |
||
| 438 | |||
| 439 | protected function validateDelete() |
||
| 440 | { |
||
| 441 | if (!$this->user->hasPermission('modify', 'catalog/download')) { |
||
| 442 | $this->error['warning'] = $this->language->get('error_permission'); |
||
| 443 | } |
||
| 444 | |||
| 445 | $this->load->model('catalog/product'); |
||
| 446 | |||
| 447 | foreach ($this->request->post['selected'] as $download_id) { |
||
| 448 | $product_total = $this->model_catalog_product->getTotalProductsByDownloadId($download_id); |
||
| 449 | |||
| 450 | if ($product_total) { |
||
| 451 | $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | return !$this->error; |
||
| 456 | } |
||
| 457 | |||
| 458 | public function upload() |
||
| 459 | { |
||
| 460 | $this->load->language('catalog/download'); |
||
| 461 | |||
| 462 | $json = array(); |
||
| 463 | |||
| 464 | // Check user has permission |
||
| 465 | if (!$this->user->hasPermission('modify', 'catalog/download')) { |
||
| 466 | $json['error'] = $this->language->get('error_permission'); |
||
| 467 | } |
||
| 468 | |||
| 469 | if (!$json) { |
||
| 470 | if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) { |
||
| 471 | // Sanitize the filename |
||
| 472 | $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8')); |
||
| 473 | |||
| 474 | // Validate the filename length |
||
| 475 | if ((\voku\helper\UTF8::strlen($filename) < 3) || (\voku\helper\UTF8::strlen($filename) > 128)) { |
||
| 476 | $json['error'] = $this->language->get('error_filename'); |
||
| 477 | } |
||
| 478 | |||
| 479 | // Allowed file extension types |
||
| 480 | $allowed = array(); |
||
| 481 | |||
| 482 | $extension_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_ext_allowed')); |
||
| 483 | |||
| 484 | $filetypes = explode("\n", $extension_allowed); |
||
| 485 | |||
| 486 | foreach ($filetypes as $filetype) { |
||
| 487 | $allowed[] = trim($filetype); |
||
| 488 | } |
||
| 489 | |||
| 490 | if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) { |
||
| 491 | $json['error'] = $this->language->get('error_filetype'); |
||
| 492 | } |
||
| 493 | |||
| 494 | // Allowed file mime types |
||
| 495 | $allowed = array(); |
||
| 496 | |||
| 497 | $mime_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_mime_allowed')); |
||
| 498 | |||
| 499 | $filetypes = explode("\n", $mime_allowed); |
||
| 500 | |||
| 501 | foreach ($filetypes as $filetype) { |
||
| 502 | $allowed[] = trim($filetype); |
||
| 503 | } |
||
| 504 | |||
| 505 | if (!in_array($this->request->files['file']['type'], $allowed)) { |
||
| 506 | $json['error'] = $this->language->get('error_filetype'); |
||
| 507 | } |
||
| 508 | |||
| 509 | // Check to see if any PHP files are trying to be uploaded |
||
| 510 | $content = file_get_contents($this->request->files['file']['tmp_name']); |
||
| 511 | |||
| 512 | if (preg_match('/\<\?php/i', $content)) { |
||
| 513 | $json['error'] = $this->language->get('error_filetype'); |
||
| 514 | } |
||
| 515 | |||
| 516 | // Return any upload error |
||
| 517 | if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) { |
||
| 518 | $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']); |
||
| 519 | } |
||
| 520 | } else { |
||
| 521 | $json['error'] = $this->language->get('error_upload'); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | if (!$json) { |
||
| 526 | $file = $filename . '.' . (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(32, 'SR'); |
||
| 527 | |||
| 528 | move_uploaded_file($this->request->files['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/storage/download/' . $file); |
||
| 529 | |||
| 530 | $json['filename'] = $file; |
||
| 531 | $json['mask'] = $filename; |
||
| 532 | |||
| 533 | $json['success'] = $this->language->get('text_upload'); |
||
| 534 | } |
||
| 535 | |||
| 536 | $this->response->addHeader('Content-Type: application/json'); |
||
| 537 | $this->response->setOutput(json_encode($json)); |
||
| 538 | } |
||
| 539 | |||
| 540 | public function autocomplete() |
||
| 573 | } |
||
| 574 | } |
||
| 575 |
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.