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 | |||
| 20 | /** @var \phpbb\template\template */ |
||
| 21 | protected $template; |
||
| 22 | |||
| 23 | /** @var \phpbb\user */ |
||
| 24 | protected $user; |
||
| 25 | |||
| 26 | /** @var \phpbb\request\request */ |
||
| 27 | protected $request; |
||
| 28 | |||
| 29 | /** @var \phpbb\admanagement\ad\manager */ |
||
| 30 | protected $manager; |
||
| 31 | |||
| 32 | /** @var \phpbb\admanagement\location\manager */ |
||
| 33 | protected $location_manager; |
||
| 34 | |||
| 35 | /** @var string php_ext */ |
||
| 36 | protected $php_ext; |
||
| 37 | |||
| 38 | /** @var string ext_path */ |
||
| 39 | protected $ext_path; |
||
| 40 | |||
| 41 | /** @var string Custom form action */ |
||
| 42 | protected $u_action; |
||
| 43 | |||
| 44 | /** @var array Form validation errors */ |
||
| 45 | protected $errors = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor |
||
| 49 | * |
||
| 50 | * @param \phpbb\template\template $template Template object |
||
| 51 | * @param \phpbb\user $user User object |
||
| 52 | * @param \phpbb\request\request $request Request object |
||
| 53 | * @param \phpbb\admanagement\ad\manager $manager Advertisement manager object |
||
| 54 | * @param \phpbb\admanagement\location\manager $location_manager Template location manager object |
||
| 55 | * @param string $php_ext PHP extension |
||
| 56 | * @param string $ext_path Path to this extension |
||
| 57 | */ |
||
| 58 | 27 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\admanagement\ad\manager $manager, \phpbb\admanagement\location\manager $location_manager, $php_ext, $ext_path) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Process user request |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | 6 | public function main() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set page url |
||
| 91 | * |
||
| 92 | * @param string $u_action Custom form action |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 21 | public function set_page_url($u_action) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get ACP page title for Ads module |
||
| 102 | * |
||
| 103 | * @return string Language string for Ads ACP module |
||
| 104 | */ |
||
| 105 | 1 | public function get_page_title() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Add an advertisement |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | 5 | public function action_add() |
|
| 116 | { |
||
| 117 | 5 | add_form_key('phpbb/admanagement/add'); |
|
| 118 | 5 | if ($this->request->is_set_post('submit')) |
|
| 119 | 5 | { |
|
| 120 | 4 | $data = $this->get_form_data(); |
|
| 121 | |||
| 122 | 4 | $this->validate($data, 'phpbb/admanagement/add'); |
|
| 123 | |||
| 124 | 4 | if (empty($this->errors)) |
|
| 125 | 4 | { |
|
| 126 | 1 | $ad_id = $this->manager->insert_ad($data); |
|
| 127 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
| 128 | |||
| 129 | 1 | $this->success('ACP_AD_ADD_SUCCESS'); |
|
| 130 | } |
||
| 131 | |||
| 132 | 3 | $this->assign_locations($data); |
|
| 133 | 3 | $this->assign_form_data($data); |
|
| 134 | 3 | } |
|
| 135 | |||
| 136 | // Set output vars for display in the template |
||
| 137 | 4 | $this->template->assign_vars(array( |
|
| 138 | 4 | 'S_ADD_AD' => true, |
|
| 139 | 4 | 'U_BACK' => $this->u_action, |
|
| 140 | 4 | )); |
|
| 141 | 4 | $this->assign_locations(); |
|
| 142 | 4 | } |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Edit an advertisement |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | 14 | public function action_edit() |
|
| 150 | { |
||
| 151 | 7 | $ad_id = $this->request->variable('id', 0); |
|
| 152 | |||
| 153 | 7 | add_form_key('phpbb/admanagement/edit/' . $ad_id); |
|
| 154 | 7 | if ($this->request->is_set_post('submit')) |
|
| 155 | 7 | { |
|
| 156 | 5 | $data = $this->get_form_data(); |
|
| 157 | |||
| 158 | 5 | $this->validate($data, 'phpbb/admanagement/edit/' . $ad_id); |
|
| 159 | |||
| 160 | 5 | if (empty($this->errors)) |
|
| 161 | 5 | { |
|
| 162 | 2 | $success = $this->manager->update_ad($ad_id, $data); |
|
| 163 | |||
| 164 | if ($success) |
||
| 165 | 2 | { |
|
| 166 | // Only insert new ad locations to DB when ad exists |
||
| 167 | 1 | $this->manager->delete_ad_locations($ad_id); |
|
| 168 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
| 169 | |||
| 170 | 1 | $this->success('ACP_AD_EDIT_SUCCESS'); |
|
| 171 | } |
||
| 172 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 173 | } |
||
| 174 | 3 | } |
|
| 175 | else |
||
| 176 | { |
||
| 177 | // Load ad data |
||
| 178 | 2 | $data = $this->manager->get_ad($ad_id); |
|
| 179 | 2 | if (empty($data)) |
|
| 180 | 2 | { |
|
| 181 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 182 | } |
||
| 183 | |||
| 184 | // Load ad template locations |
||
| 185 | 1 | $data['ad_locations'] = $this->manager->get_ad_locations($ad_id); |
|
| 186 | } |
||
| 187 | |||
| 188 | // Set output vars for display in the template |
||
| 189 | 4 | $this->template->assign_vars(array( |
|
| 190 | 4 | 'S_EDIT_AD' => true, |
|
| 191 | 4 | 'EDIT_ID' => $ad_id, |
|
| 192 | 4 | 'U_BACK' => $this->u_action, |
|
| 193 | 4 | )); |
|
| 194 | 4 | $this->assign_locations($data); |
|
| 195 | 4 | $this->assign_form_data($data); |
|
| 196 | 14 | } |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Enable an advertisement |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | 3 | public function action_enable() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Disable an advertisement |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | 3 | public function action_disable() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Delete an advertisement |
||
| 220 | * |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | 3 | public function action_delete() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Display the ads |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | 1 | public function list_ads() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Enable/disable an advertisement |
||
| 285 | * |
||
| 286 | * @param bool $enable Enable or disable the advertisement? |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | 4 | protected function ad_enable($enable) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get admin form data. |
||
| 320 | * |
||
| 321 | * @return array Form data |
||
| 322 | */ |
||
| 323 | 9 | protected function get_form_data() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Validate form data. |
||
| 336 | * |
||
| 337 | * @param array $data The form data. |
||
| 338 | * @param string $form_name The form name. |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | 9 | protected function validate($data, $form_name) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Assign form data to the template. |
||
| 360 | * |
||
| 361 | * @param array $data The form data. |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | 7 | protected function assign_form_data($data) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Assign template locations data to the template. |
||
| 379 | * |
||
| 380 | * @param mixed $data The form data or nothing. |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | 8 | protected function assign_locations($data = false) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Print success message. |
||
| 398 | * |
||
| 399 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 400 | */ |
||
| 401 | 5 | protected function success() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Print error message. |
||
| 408 | * |
||
| 409 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 410 | */ |
||
| 411 | 5 | protected function error() |
|
| 415 | } |
||
| 416 |