| Total Complexity | 47 |
| Total Lines | 373 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like admin_main 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 admin_main, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class admin_main |
||
| 17 | { |
||
| 18 | /** @var object \phpbb\config\config */ |
||
| 19 | protected $config; |
||
| 20 | /** @var object Symfony\Component\DependencyInjection\ContainerInterface */ |
||
| 21 | protected $container; |
||
| 22 | /** @var string */ |
||
| 23 | protected $id_prefix_name; |
||
| 24 | /** @var string */ |
||
| 25 | protected $lang_key_prefix; |
||
| 26 | /** @var \phpbb\language\language */ |
||
| 27 | protected $language; |
||
| 28 | /** @var \phpbb\log\log */ |
||
| 29 | protected $log; |
||
| 30 | /** @var string */ |
||
| 31 | protected $module_name; |
||
| 32 | /** @var bool */ |
||
| 33 | protected $preview; |
||
| 34 | /** @var \phpbb\request\request */ |
||
| 35 | protected $request; |
||
| 36 | /** @var bool */ |
||
| 37 | protected $submit; |
||
| 38 | /** @var \phpbb\template\template */ |
||
| 39 | protected $template; |
||
| 40 | /** @var string */ |
||
| 41 | protected $u_action; |
||
| 42 | /** @var \phpbb\user */ |
||
| 43 | protected $user; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor |
||
| 47 | * |
||
| 48 | * @param string $lang_key_prefix Prefix for the messages thrown by exceptions |
||
| 49 | * @param string $id_prefix_name Prefix name for identifier in the URL |
||
| 50 | * @param string $module_name Name of the module currently used |
||
| 51 | * |
||
| 52 | * @access public |
||
| 53 | */ |
||
| 54 | public function __construct($module_name, $lang_key_prefix, $id_prefix_name) |
||
| 55 | { |
||
| 56 | $this->module_name = $module_name; |
||
| 57 | $this->lang_key_prefix = $lang_key_prefix; |
||
| 58 | $this->id_prefix_name = $id_prefix_name; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set page url |
||
| 63 | * |
||
| 64 | * @param string $u_action Custom form action |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | * @access public |
||
| 68 | */ |
||
| 69 | public function set_page_url($u_action) |
||
| 70 | { |
||
| 71 | $this->u_action = $u_action; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function display() |
||
| 75 | { |
||
| 76 | } |
||
| 77 | |||
| 78 | public function add() |
||
| 79 | { |
||
| 80 | } |
||
| 81 | |||
| 82 | public function edit($id) |
||
| 83 | { |
||
| 84 | } |
||
| 85 | |||
| 86 | public function delete($id) |
||
| 87 | { |
||
| 88 | } |
||
| 89 | |||
| 90 | public function enable($id, $action) |
||
| 92 | } |
||
| 93 | |||
| 94 | public function move($id, $action) |
||
| 95 | { |
||
| 96 | } |
||
| 97 | |||
| 98 | protected function set_settings() |
||
| 99 | { |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The form submitting if 'submit' is true |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | * @access protected |
||
| 107 | */ |
||
| 108 | protected function submit_settings() |
||
| 109 | { |
||
| 110 | $this->submit = $this->request->is_set_post('submit'); |
||
| 111 | |||
| 112 | // Test if the submitted form is valid |
||
| 113 | $errors = $this->is_invalid_form('ppde_' . $this->module_name, $this->submit); |
||
| 114 | |||
| 115 | if ($this->can_submit_data($errors)) |
||
| 116 | { |
||
| 117 | // Set the options the user configured |
||
| 118 | $this->set_settings(); |
||
| 119 | |||
| 120 | // Add option settings change action to the admin log |
||
| 121 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED'); |
||
| 122 | |||
| 123 | // Option settings have been updated and logged |
||
| 124 | // Confirm this to the user and provide link back to previous page |
||
| 125 | trigger_error($this->language->lang($this->lang_key_prefix . '_SAVED') . adm_back_link($this->u_action)); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Check if form is valid or not |
||
| 131 | * |
||
| 132 | * @param string $form_name |
||
| 133 | * @param bool $submit_or_preview |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | * @access protected |
||
| 137 | */ |
||
| 138 | protected function is_invalid_form($form_name, $submit_or_preview = false) |
||
| 139 | { |
||
| 140 | return (!check_form_key($form_name) && $submit_or_preview) ? array($this->language->lang('FORM_INVALID')) : array(); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $errors |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | * @access protected |
||
| 148 | */ |
||
| 149 | protected function can_submit_data(array $errors) |
||
| 150 | { |
||
| 151 | return $this->submit && empty($errors) && !$this->preview; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Trigger error message if data already exists |
||
| 156 | * |
||
| 157 | * @param \skouat\ppde\entity\main $entity The entity object |
||
| 158 | * |
||
| 159 | * @access protected |
||
| 160 | */ |
||
| 161 | protected function trigger_error_data_already_exists(main $entity) |
||
| 162 | { |
||
| 163 | if ($this->is_added_data_exists($entity)) |
||
| 164 | { |
||
| 165 | // Show user warning for an already exist page and provide link back to the edit page |
||
| 166 | $message = $this->language->lang($this->lang_key_prefix . '_EXISTS'); |
||
| 167 | $message .= '<br><br>'; |
||
| 168 | $message .= $this->language->lang($this->lang_key_prefix . '_GO_TO_PAGE', '<a href="' . $this->u_action . '&action=edit&' . $this->id_prefix_name . '_id=' . $entity->get_id() . '">» ', '</a>'); |
||
| 169 | trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param \skouat\ppde\entity\main $entity The entity object |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | * @access protected |
||
| 178 | */ |
||
| 179 | protected function is_added_data_exists(main $entity) |
||
| 180 | { |
||
| 181 | return $entity->data_exists($entity->build_sql_data_exists()) && $this->request->variable('action', '') === 'add'; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Check some settings before submitting data |
||
| 186 | * |
||
| 187 | * @param \skouat\ppde\entity\main $entity The entity object |
||
| 188 | * @param string $field_name Name of the entity function to call |
||
| 189 | * @param string|int $value_cmp Default value to compare with the call_user_func() return value |
||
| 190 | * @param bool $submit_or_preview Form submit or preview status |
||
| 191 | * |
||
| 192 | * @return array $errors |
||
| 193 | * @access protected |
||
| 194 | */ |
||
| 195 | protected function is_empty_data(main $entity, $field_name, $value_cmp, $submit_or_preview = false) |
||
| 196 | { |
||
| 197 | $errors = array(); |
||
| 198 | |||
| 199 | if (call_user_func(array($entity, 'get_' . $field_name)) == $value_cmp && $submit_or_preview) |
||
| 200 | { |
||
| 201 | $errors[] = $this->language->lang($this->lang_key_prefix . '_EMPTY_' . strtoupper($field_name)); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $errors; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get result of submit and preview expression |
||
| 209 | * |
||
| 210 | * @param bool $submit |
||
| 211 | * @param bool $preview |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | * @access protected |
||
| 215 | */ |
||
| 216 | protected function submit_or_preview($submit = false, $preview = false) |
||
| 217 | { |
||
| 218 | return (bool) $submit || (bool) $preview; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Show user a result message if AJAX was used |
||
| 223 | * |
||
| 224 | * @param string $message Text message to show to the user |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | * @access protected |
||
| 228 | */ |
||
| 229 | protected function ajax_delete_result_message($message = '') |
||
| 230 | { |
||
| 231 | if ($this->request->is_ajax()) |
||
| 232 | { |
||
| 233 | $json_response = new \phpbb\json_response; |
||
| 234 | $json_response->send(array( |
||
| 235 | 'MESSAGE_TITLE' => $this->language->lang('INFORMATION'), |
||
| 236 | 'MESSAGE_TEXT' => $message, |
||
| 237 | 'REFRESH_DATA' => array( |
||
| 238 | 'time' => 3, |
||
| 239 | ), |
||
| 240 | )); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set u_action output vars for display in the template |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | * @access protected |
||
| 249 | */ |
||
| 250 | protected function u_action_assign_template_vars() |
||
| 251 | { |
||
| 252 | $this->template->assign_vars(array( |
||
| 253 | 'U_ACTION' => $this->u_action, |
||
| 254 | )); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set add/edit action output vars for display in the template |
||
| 259 | * |
||
| 260 | * @param string $type Action type: 'add' or 'edit' |
||
| 261 | * @param integer $id Identifier to Edit. If action = add, then let to '0'. |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | * @access protected |
||
| 265 | */ |
||
| 266 | protected function add_edit_action_assign_template_vars($type, $id = 0) |
||
| 267 | { |
||
| 268 | $id_action = !empty($id) ? '&' . $this->id_prefix_name . '_id=' . (int) $id : ''; |
||
| 269 | |||
| 270 | $this->template->assign_vars(array( |
||
| 271 | 'S_ADD_EDIT' => true, |
||
| 272 | 'U_ACTION' => $this->u_action . '&action=' . $type . $id_action, |
||
| 273 | 'U_BACK' => $this->u_action, |
||
| 274 | )); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set error output vars for display in the template |
||
| 279 | * |
||
| 280 | * @param array $errors |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | * @access protected |
||
| 284 | */ |
||
| 285 | protected function s_error_assign_template_vars($errors) |
||
| 286 | { |
||
| 287 | $this->template->assign_vars(array( |
||
| 288 | 'S_ERROR' => (count($errors)) ? true : false, |
||
| 289 | 'ERROR_MSG' => (count($errors)) ? implode('<br>', $errors) : '', |
||
| 290 | )); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Check if a config value is true |
||
| 295 | * |
||
| 296 | * @param mixed $config Config value |
||
| 297 | * @param string $type (see settype()) |
||
| 298 | * @param mixed $default |
||
| 299 | * |
||
| 300 | * @return mixed |
||
| 301 | * @access protected |
||
| 302 | */ |
||
| 303 | protected function check_config($config, $type = 'boolean', $default = '') |
||
| 304 | { |
||
| 305 | // We're using settype to enforce data types |
||
| 306 | settype($config, $type); |
||
| 307 | settype($default, $type); |
||
| 308 | |||
| 309 | return $config ? $config : $default; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Check if settings is required |
||
| 314 | * |
||
| 315 | * @param $settings |
||
| 316 | * @param $depend_on |
||
| 317 | * |
||
| 318 | * @return mixed |
||
| 319 | * @access protected |
||
| 320 | */ |
||
| 321 | protected function required_settings($settings, $depend_on) |
||
| 322 | { |
||
| 323 | if (empty($settings) && $depend_on == true) |
||
| 324 | { |
||
| 325 | trigger_error($this->language->lang($this->lang_key_prefix . '_MISSING') . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 326 | } |
||
| 327 | |||
| 328 | return $settings; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Check if a settings depend on another. |
||
| 333 | * |
||
| 334 | * @param $config_name |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | * @access protected |
||
| 338 | */ |
||
| 339 | protected function depend_on($config_name) |
||
| 340 | { |
||
| 341 | return !empty($this->config[$config_name]) ? (bool) $this->config[$config_name] : false; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Build pull down menu options of available currency |
||
| 346 | * |
||
| 347 | * @param mixed $default ID of the selected value. |
||
| 348 | * @param string $type Can be 'live' or 'sandbox' |
||
| 349 | * |
||
| 350 | * @return void |
||
| 351 | * @access public |
||
| 352 | */ |
||
| 353 | public function build_remote_uri_select_menu($default, $type) |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Enforce the type of remote provided |
||
| 380 | * |
||
| 381 | * @param string $type |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | * @access private |
||
| 385 | */ |
||
| 386 | private function force_type($type) |
||
| 387 | { |
||
| 388 | return $type === 'live' || $type === 'sandbox' ? (string) $type : 'live'; |
||
| 389 | } |
||
| 390 | } |
||
| 391 |