Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like admin_controller 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 admin_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class admin_controller |
||
| 17 | { |
||
| 18 | const MAX_NAME_LENGTH = 255; |
||
| 19 | const DATE_FORMAT = 'Y-m-d'; |
||
| 20 | const DEFAULT_PRIORITY = 5; |
||
| 21 | |||
| 22 | /** @var \phpbb\template\template */ |
||
| 23 | protected $template; |
||
| 24 | |||
| 25 | /** @var \phpbb\user */ |
||
| 26 | protected $user; |
||
| 27 | |||
| 28 | /** @var \phpbb\request\request */ |
||
| 29 | protected $request; |
||
| 30 | |||
| 31 | /** @var \phpbb\ads\ad\manager */ |
||
| 32 | protected $manager; |
||
| 33 | |||
| 34 | /** @var \phpbb\ads\location\manager */ |
||
| 35 | protected $location_manager; |
||
| 36 | |||
| 37 | /** @var \phpbb\log\log */ |
||
| 38 | protected $log; |
||
| 39 | |||
| 40 | /** @var \phpbb\config\db_text */ |
||
| 41 | protected $config_text; |
||
| 42 | |||
| 43 | /** @var \phpbb\config\config */ |
||
| 44 | protected $config; |
||
| 45 | |||
| 46 | /** @var string php_ext */ |
||
| 47 | protected $php_ext; |
||
| 48 | |||
| 49 | /** @var string ext_path */ |
||
| 50 | protected $ext_path; |
||
| 51 | |||
| 52 | /** @var string Custom form action */ |
||
| 53 | protected $u_action; |
||
| 54 | |||
| 55 | /** @var array Form validation errors */ |
||
| 56 | protected $errors = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructor |
||
| 60 | * |
||
| 61 | * @param \phpbb\template\template $template Template object |
||
| 62 | * @param \phpbb\user $user User object |
||
| 63 | * @param \phpbb\request\request $request Request object |
||
| 64 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 65 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
| 66 | * @param \phpbb\log\log $log The phpBB log system |
||
| 67 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 68 | * @param \phpbb\config\config $config Config object |
||
| 69 | * @param string $php_ext PHP extension |
||
| 70 | * @param string $ext_path Path to this extension |
||
| 71 | */ |
||
| 72 | 38 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, \phpbb\config\config $config, $php_ext, $ext_path) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Process user request for manage mode |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | 6 | public function mode_manage() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Process user request for settings mode |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | 3 | public function mode_settings() |
|
| 112 | { |
||
| 113 | 3 | $this->setup(); |
|
| 114 | |||
| 115 | 3 | add_form_key('phpbb/ads/settings'); |
|
| 116 | 3 | if ($this->request->is_set_post('submit')) |
|
| 117 | 3 | { |
|
| 118 | // Validate form key |
||
| 119 | 2 | if (!check_form_key('phpbb/ads/settings')) |
|
| 120 | 2 | { |
|
| 121 | 1 | $this->errors[] = $this->user->lang('FORM_INVALID'); |
|
| 122 | 1 | } |
|
| 123 | |||
| 124 | 2 | if (empty($this->errors)) |
|
| 125 | 2 | { |
|
| 126 | 1 | $this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0)); |
|
| 127 | 1 | $this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0)); |
|
| 128 | 1 | $this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0)); |
|
| 129 | 1 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
| 130 | |||
| 131 | 1 | $this->success('ACP_AD_SETTINGS_SAVED'); |
|
| 132 | } |
||
| 133 | |||
| 134 | 1 | $this->template->assign_vars(array( |
|
| 135 | 1 | 'S_ERROR' => (bool) count($this->errors), |
|
| 136 | 1 | 'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '', |
|
| 137 | 1 | )); |
|
| 138 | 1 | } |
|
| 139 | |||
| 140 | 2 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
| 141 | 2 | $groups = $this->manager->load_groups(); |
|
| 142 | 2 | foreach ($groups as $group) |
|
| 143 | { |
||
| 144 | 2 | $group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name']; |
|
| 145 | |||
| 146 | 2 | $this->template->assign_block_vars('groups', array( |
|
| 147 | 2 | 'ID' => $group['group_id'], |
|
| 148 | 2 | 'NAME' => $group_name, |
|
| 149 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
| 150 | 2 | )); |
|
| 151 | 2 | } |
|
| 152 | |||
| 153 | 2 | $this->template->assign_vars(array( |
|
| 154 | 2 | 'U_ACTION' => $this->u_action, |
|
| 155 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
| 156 | 2 | 'ENABLE_VIEWS' => $this->config['phpbb_ads_enable_views'], |
|
| 157 | 2 | 'ENABLE_CLICKS' => $this->config['phpbb_ads_enable_clicks'], |
|
| 158 | 2 | )); |
|
| 159 | 2 | } |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Set page url |
||
| 163 | * |
||
| 164 | * @param string $u_action Custom form action |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | 32 | public function set_page_url($u_action) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get ACP page title for Ads module |
||
| 174 | * |
||
| 175 | * @return string Language string for Ads ACP module |
||
| 176 | */ |
||
| 177 | 1 | public function get_page_title() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Add an advertisement |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | 9 | public function action_add() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Edit an advertisement |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | 11 | public function action_edit() |
|
| 234 | { |
||
| 235 | 11 | $ad_id = $this->request->variable('id', 0); |
|
| 236 | 11 | $preview = $this->request->is_set_post('preview'); |
|
| 237 | 11 | $submit = $this->request->is_set_post('submit'); |
|
| 238 | |||
| 239 | 11 | add_form_key('phpbb/ads/edit/' . $ad_id); |
|
| 240 | 11 | if ($preview || $submit) |
|
| 241 | 11 | { |
|
| 242 | 9 | $data = $this->get_form_data('phpbb/ads/edit/' . $ad_id); |
|
| 243 | |||
| 244 | if ($preview) |
||
| 245 | 9 | { |
|
| 246 | 1 | $this->ad_preview($data['ad_code']); |
|
| 247 | 1 | } |
|
| 248 | View Code Duplication | else if (empty($this->errors)) |
|
| 249 | 8 | { |
|
| 250 | 2 | $success = $this->manager->update_ad($ad_id, $data); |
|
| 251 | |||
| 252 | if ($success) |
||
| 253 | 2 | { |
|
| 254 | // Only insert new ad locations to DB when ad exists |
||
| 255 | 1 | $this->manager->delete_ad_locations($ad_id); |
|
| 256 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
| 257 | |||
| 258 | 1 | $this->log('EDIT', $data['ad_name']); |
|
| 259 | |||
| 260 | 1 | $this->success('ACP_AD_EDIT_SUCCESS'); |
|
| 261 | } |
||
| 262 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 263 | } |
||
| 264 | 7 | } |
|
| 265 | else |
||
| 266 | { |
||
| 267 | // Load ad data |
||
| 268 | 2 | $data = $this->manager->get_ad($ad_id); |
|
| 269 | 2 | if (empty($data)) |
|
| 270 | 2 | { |
|
| 271 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 272 | } |
||
| 273 | |||
| 274 | // Load ad template locations |
||
| 275 | 1 | $data['ad_locations'] = $this->manager->get_ad_locations($ad_id); |
|
| 276 | } |
||
| 277 | |||
| 278 | // Set output vars for display in the template |
||
| 279 | 8 | $this->template->assign_vars(array( |
|
| 280 | 8 | 'S_EDIT_AD' => true, |
|
| 281 | 8 | 'EDIT_ID' => $ad_id, |
|
| 282 | 8 | 'U_BACK' => $this->u_action, |
|
| 283 | 8 | 'U_ACTION' => "{$this->u_action}&action=edit&id=" . $ad_id, |
|
| 284 | 8 | 'PICKER_DATE_FORMAT' => self::DATE_FORMAT, |
|
| 285 | 8 | )); |
|
| 286 | 8 | $this->assign_locations($data); |
|
| 287 | 8 | $this->assign_form_data($data); |
|
| 288 | 8 | } |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Enable an advertisement |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | 3 | public function action_enable() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Disable an advertisement |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | 3 | public function action_disable() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Delete an advertisement |
||
| 312 | * |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | 3 | public function action_delete() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Display the ads |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | 1 | public function list_ads() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Perform general tasks |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | 9 | protected function setup() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Enable/disable an advertisement |
||
| 411 | * |
||
| 412 | * @param bool $enable Enable or disable the advertisement? |
||
| 413 | * @return void |
||
| 414 | */ |
||
| 415 | 4 | protected function ad_enable($enable) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Get admin form data. |
||
| 446 | * |
||
| 447 | * @param string $form_name The form name. |
||
| 448 | * @return array Form data |
||
| 449 | */ |
||
| 450 | 17 | protected function get_form_data($form_name) |
|
| 451 | { |
||
| 452 | $data = array( |
||
| 453 | 17 | 'ad_name' => $this->request->variable('ad_name', '', true), |
|
| 454 | 17 | 'ad_note' => $this->request->variable('ad_note', '', true), |
|
| 455 | 17 | 'ad_code' => $this->request->variable('ad_code', '', true), |
|
| 456 | 17 | 'ad_enabled' => $this->request->variable('ad_enabled', 0), |
|
| 457 | 17 | 'ad_locations' => $this->request->variable('ad_locations', array('')), |
|
| 458 | 17 | 'ad_end_date' => $this->request->variable('ad_end_date', ''), |
|
| 459 | 17 | 'ad_priority' => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY), |
|
| 460 | 17 | 'ad_views_limit' => $this->request->variable('ad_views_limit', 0), |
|
| 461 | 17 | 'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0), |
|
| 462 | 17 | ); |
|
| 463 | |||
| 464 | // Validate form key |
||
| 465 | 17 | if (!check_form_key($form_name)) |
|
| 466 | 17 | { |
|
| 467 | 2 | $this->errors[] = $this->user->lang('FORM_INVALID'); |
|
| 468 | 2 | } |
|
| 469 | |||
| 470 | // Validate ad name |
||
| 471 | 17 | if ($data['ad_name'] === '') |
|
| 472 | 17 | { |
|
| 473 | 2 | $this->errors[] = $this->user->lang('AD_NAME_REQUIRED'); |
|
| 474 | 2 | } |
|
| 475 | 17 | if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name']) |
|
| 476 | 17 | { |
|
| 477 | 2 | $this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH); |
|
| 478 | 2 | } |
|
| 479 | |||
| 480 | // Validate ad end date |
||
| 481 | 17 | if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date'])) |
|
| 482 | 17 | { |
|
| 483 | 4 | $data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']); |
|
| 484 | |||
| 485 | 4 | if ($data['ad_end_date'] < time()) |
|
| 486 | 4 | { |
|
| 487 | 2 | $this->errors[] = $this->user->lang('AD_END_DATE_INVALID'); |
|
| 488 | 2 | } |
|
| 489 | 4 | } |
|
| 490 | 13 | else if ($data['ad_end_date'] !== '') |
|
| 491 | 13 | { |
|
| 492 | $this->errors[] = $this->user->lang('AD_END_DATE_INVALID'); |
||
| 493 | } |
||
| 494 | else |
||
| 495 | { |
||
| 496 | 13 | $data['ad_end_date'] = 0; |
|
| 497 | } |
||
| 498 | |||
| 499 | // Validate ad priority |
||
| 500 | 17 | if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10) |
|
| 501 | 17 | { |
|
| 502 | 6 | $this->errors[] = $this->user->lang('AD_PRIORITY_INVALID'); |
|
| 503 | 6 | } |
|
| 504 | |||
| 505 | // Validate ad views limit |
||
| 506 | 17 | if ($data['ad_views_limit'] < 0) |
|
| 507 | 17 | { |
|
| 508 | $this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID'); |
||
| 509 | } |
||
| 510 | |||
| 511 | // Validate ad clicks limit |
||
| 512 | 17 | if ($data['ad_clicks_limit'] < 0) |
|
| 513 | 17 | { |
|
| 514 | $this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID'); |
||
| 515 | } |
||
| 516 | |||
| 517 | 17 | return $data; |
|
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Assign form data to the template. |
||
| 522 | * |
||
| 523 | * @param array $data The form data. |
||
| 524 | * @return void |
||
| 525 | */ |
||
| 526 | 15 | protected function assign_form_data($data) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Prepare end date for display |
||
| 545 | * |
||
| 546 | * @param mixed $end_date End date. |
||
| 547 | * @return string End date prepared for display. |
||
| 548 | */ |
||
| 549 | 15 | protected function prepare_end_date($end_date) |
|
| 550 | { |
||
| 551 | 15 | if (empty($end_date)) |
|
| 552 | 15 | { |
|
| 553 | 12 | return ''; |
|
| 554 | } |
||
| 555 | |||
| 556 | 3 | if (is_numeric($end_date)) |
|
| 557 | 3 | { |
|
| 558 | 3 | return $this->user->format_date($end_date, self::DATE_FORMAT); |
|
| 559 | } |
||
| 560 | |||
| 561 | return (string) $end_date; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Assign template locations data to the template. |
||
| 566 | * |
||
| 567 | * @param mixed $data The form data or nothing. |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | 16 | protected function assign_locations($data = false) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Prepare advertisement preview |
||
| 585 | * |
||
| 586 | * @param string $code Ad code to preview |
||
| 587 | * @return void |
||
| 588 | */ |
||
| 589 | 2 | protected function ad_preview($code) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Print success message. |
||
| 596 | * |
||
| 597 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 598 | */ |
||
| 599 | 6 | protected function success() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Print error message. |
||
| 606 | * |
||
| 607 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 608 | */ |
||
| 609 | 5 | protected function error() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Log action |
||
| 616 | * |
||
| 617 | * @param string $action Performed action in uppercase |
||
| 618 | * @param string $ad_name Advertisement name |
||
| 619 | * @return void |
||
| 620 | */ |
||
| 621 | 3 | protected function log($action, $ad_name) |
|
| 625 | } |
||
| 626 |