Complex classes like admin_transactions_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_transactions_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class admin_transactions_controller extends admin_main |
||
| 29 | { |
||
| 30 | public $ppde_operator; |
||
| 31 | protected $adm_relative_path; |
||
| 32 | protected $auth; |
||
| 33 | protected $db; |
||
| 34 | protected $config; |
||
| 35 | protected $entry_count; |
||
| 36 | protected $last_page_offset; |
||
| 37 | protected $php_ext; |
||
| 38 | protected $phpbb_admin_path; |
||
| 39 | protected $phpbb_root_path; |
||
| 40 | protected $table_ppde_transactions; |
||
| 41 | private $is_ipn_test = false; |
||
| 42 | private $suffix_ipn; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | * |
||
| 47 | * @param \phpbb\auth\auth $auth Authentication object |
||
| 48 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
| 49 | * @param \phpbb\config\config $config Config object |
||
| 50 | * @param ContainerInterface $container Service container interface |
||
| 51 | * @param \phpbb\language\language $language Language user object |
||
| 52 | * @param \phpbb\log\log $log The phpBB log system |
||
| 53 | * @param \skouat\ppde\operators\transactions $ppde_operator_transactions Operator object |
||
| 54 | * @param \phpbb\request\request $request Request object |
||
| 55 | * @param \phpbb\template\template $template Template object |
||
| 56 | * @param \phpbb\user $user User object. |
||
| 57 | * @param string $adm_relative_path phpBB admin relative path |
||
| 58 | * @param string $phpbb_root_path phpBB root path |
||
| 59 | * @param string $php_ext phpEx |
||
| 60 | * @param string $table_ppde_transactions Name of the table used to store data |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | */ |
||
| 64 | public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, ContainerInterface $container, \phpbb\language\language $language, \phpbb\log\log $log, \skouat\ppde\operators\transactions $ppde_operator_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $adm_relative_path, $phpbb_root_path, $php_ext, $table_ppde_transactions) |
||
| 65 | { |
||
| 66 | $this->auth = $auth; |
||
| 67 | $this->db = $db; |
||
| 68 | $this->config = $config; |
||
| 69 | $this->container = $container; |
||
| 70 | $this->language = $language; |
||
| 71 | $this->log = $log; |
||
| 72 | $this->ppde_operator = $ppde_operator_transactions; |
||
| 73 | $this->request = $request; |
||
| 74 | $this->template = $template; |
||
| 75 | $this->user = $user; |
||
| 76 | $this->adm_relative_path = $adm_relative_path; |
||
| 77 | $this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path; |
||
| 78 | $this->phpbb_root_path = $phpbb_root_path; |
||
| 79 | $this->php_ext = $php_ext; |
||
| 80 | $this->table_ppde_transactions = $table_ppde_transactions; |
||
| 81 | parent::__construct( |
||
| 82 | 'transactions', |
||
| 83 | 'PPDE_DT', |
||
| 84 | 'transaction' |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Display the transactions list |
||
| 90 | * |
||
| 91 | * @param string $id Module id |
||
| 92 | * @param string $mode Module categorie |
||
| 93 | * @param string $action Action name |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | * @access public |
||
| 97 | */ |
||
| 98 | public function display_transactions($id, $mode, $action) |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param bool $ipn_test |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | public function update_stats($ipn_test = false) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets properties related to ipn tests |
||
| 225 | * |
||
| 226 | * @param bool $ipn_test |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | * @access public |
||
| 230 | */ |
||
| 231 | public function set_ipn_test_properties($ipn_test) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Sets the property $this->is_ipn_test |
||
| 239 | * |
||
| 240 | * @param $ipn_test |
||
| 241 | * |
||
| 242 | * @return void |
||
| 243 | * @access private |
||
| 244 | */ |
||
| 245 | private function set_ipn_test($ipn_test) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Sets the property $this->suffix_ipn |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | * @access private |
||
| 255 | */ |
||
| 256 | private function set_suffix_ipn() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns count result for updating stats |
||
| 263 | * |
||
| 264 | * @param string $config_name |
||
| 265 | * |
||
| 266 | * @return int |
||
| 267 | * @access private |
||
| 268 | */ |
||
| 269 | private function sql_query_update_stats($config_name) |
||
| 270 | { |
||
| 271 | if (!$this->config->offsetExists($config_name)) |
||
| 272 | { |
||
| 273 | trigger_error($this->language->lang('EXCEPTION_INVALID_CONFIG_NAME', $config_name), E_USER_WARNING); |
||
| 274 | } |
||
| 275 | |||
| 276 | $this->db->sql_query($this->ppde_operator->sql_build_update_stats($config_name, $this->is_ipn_test)); |
||
| 277 | |||
| 278 | return (int) $this->db->sql_fetchfield('count_result'); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Do action regarding the value of $action |
||
| 283 | * |
||
| 284 | * @param $action |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | * @access private |
||
| 288 | */ |
||
| 289 | private function do_action($action) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * View log |
||
| 345 | * |
||
| 346 | * @param array &$log The result array with the logs |
||
| 347 | * @param mixed &$log_count If $log_count is set to false, we will skip counting all entries in the |
||
| 348 | * database. Otherwise an integer with the number of total matching entries is returned. |
||
| 349 | * @param int $limit Limit the number of entries that are returned |
||
| 350 | * @param int $offset Offset when fetching the log entries, f.e. when paginating |
||
| 351 | * @param int $limit_days |
||
| 352 | * @param string $sort_by SQL order option, e.g. 'l.log_time DESC' |
||
| 353 | * @param string $keywords Will only return log entries that have the keywords in log_operation or log_data |
||
| 354 | * |
||
| 355 | * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high) |
||
| 356 | * @access private |
||
| 357 | */ |
||
| 358 | private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param bool $count_logs |
||
| 370 | * @param int $limit |
||
| 371 | * @param int $offset |
||
| 372 | * @param int $log_time |
||
| 373 | * @param string $sort_by |
||
| 374 | * @param string $keywords |
||
| 375 | * |
||
| 376 | * @return array $log |
||
| 377 | * @access private |
||
| 378 | */ |
||
| 379 | private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
| 380 | { |
||
| 381 | $this->entry_count = 0; |
||
| 382 | $this->last_page_offset = $offset; |
||
| 383 | $url_ary = array(); |
||
| 384 | |||
| 385 | if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path) |
||
| 386 | { |
||
| 387 | $url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&mode=overview'); |
||
| 388 | $url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&mode=transactions'); |
||
| 389 | |||
| 390 | } |
||
| 391 | else |
||
| 392 | { |
||
| 393 | $url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile'); |
||
| 394 | $url_ary['txn_url'] = ''; |
||
| 395 | } |
||
| 396 | |||
| 397 | $get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time); |
||
| 398 | |||
| 399 | if ($count_logs) |
||
| 400 | { |
||
| 401 | $this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id'); |
||
| 402 | |||
| 403 | if ($this->entry_count == 0) |
||
| 404 | { |
||
| 405 | // Save the queries, because there are no logs to display |
||
| 406 | $this->last_page_offset = 0; |
||
| 407 | |||
| 408 | return array(); |
||
| 409 | } |
||
| 410 | |||
| 411 | // Return the user to the last page that is valid |
||
| 412 | while ($this->last_page_offset >= $this->entry_count) |
||
| 413 | { |
||
| 414 | $this->last_page_offset = max(0, $this->last_page_offset - $limit); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return integer |
||
| 423 | */ |
||
| 424 | public function get_log_count() |
||
| 425 | { |
||
| 426 | return ($this->entry_count) ? $this->entry_count : 0; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return integer |
||
| 431 | */ |
||
| 432 | public function get_valid_offset() |
||
| 433 | { |
||
| 434 | return ($this->last_page_offset) ? $this->last_page_offset : 0; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return boolean |
||
| 439 | */ |
||
| 440 | public function get_ipn_test() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function get_suffix_ipn() |
||
| 452 | } |
||
| 453 |