| Total Complexity | 64 |
| Total Lines | 658 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 |
||
| 40 | class admin_transactions_controller extends admin_main |
||
| 41 | { |
||
| 42 | public $ppde_operator; |
||
| 43 | protected $adm_relative_path; |
||
| 44 | protected $auth; |
||
| 45 | protected $entry_count; |
||
| 46 | protected $last_page_offset; |
||
| 47 | protected $php_ext; |
||
| 48 | protected $phpbb_admin_path; |
||
| 49 | protected $phpbb_root_path; |
||
| 50 | protected $ppde_actions; |
||
| 51 | protected $ppde_actions_currency; |
||
| 52 | protected $table_prefix; |
||
| 53 | protected $table_ppde_transactions; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Constructor |
||
| 57 | * |
||
| 58 | * @param auth $auth Authentication object |
||
| 59 | * @param config $config Config object |
||
| 60 | * @param ContainerInterface $container Service container interface |
||
| 61 | * @param language $language Language user object |
||
| 62 | * @param log $log The phpBB log system |
||
| 63 | * @param core $ppde_actions PPDE actions object |
||
| 64 | * @param currency $ppde_actions_currency PPDE currency actions object |
||
| 65 | * @param transactions $ppde_operator_transactions Operator object |
||
| 66 | * @param request $request Request object |
||
| 67 | * @param template $template Template object |
||
| 68 | * @param user $user User object. |
||
| 69 | * @param string $adm_relative_path phpBB admin relative path |
||
| 70 | * @param string $phpbb_root_path phpBB root path |
||
| 71 | * @param string $php_ext phpEx |
||
| 72 | * @param string $table_prefix The table prefix |
||
| 73 | * @param string $table_ppde_transactions Name of the table used to store data |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | */ |
||
| 77 | public function __construct(auth $auth, config $config, ContainerInterface $container, language $language, log $log, core $ppde_actions, currency $ppde_actions_currency, transactions $ppde_operator_transactions, request $request, template $template, user $user, $adm_relative_path, $phpbb_root_path, $php_ext, $table_prefix, $table_ppde_transactions) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Display the transactions list |
||
| 105 | * |
||
| 106 | * @param string $id Module id |
||
| 107 | * @param string $mode Module categorie |
||
| 108 | * @param string $action Action name |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | * @access public |
||
| 112 | */ |
||
| 113 | public function display_transactions($id, $mode, $action) |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Do action regarding the value of $action |
||
| 213 | * |
||
| 214 | * @param string $action Requested action |
||
| 215 | * @param array $args Arguments required for the action |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | * @access private |
||
| 219 | */ |
||
| 220 | private function do_action($action, $args) |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Does actions for validated transaction |
||
| 353 | * |
||
| 354 | * @param bool $is_member |
||
| 355 | * |
||
| 356 | * @return void |
||
| 357 | * @access private |
||
| 358 | */ |
||
| 359 | private function do_transactions_actions($is_member) |
||
| 360 | { |
||
| 361 | $this->ppde_actions->update_overview_stats(); |
||
| 362 | $this->ppde_actions->update_raised_amount(); |
||
| 363 | |||
| 364 | if ($is_member) |
||
| 365 | { |
||
| 366 | $this->ppde_actions->update_donor_stats(); |
||
| 367 | $this->ppde_actions->donors_group_user_add(); |
||
| 368 | $this->ppde_actions->notification->notify_donor_donation_received(); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns requested data from manual transaction form |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | * @access private |
||
| 377 | */ |
||
| 378 | private function request_transaction_vars() |
||
| 379 | { |
||
| 380 | return array( |
||
| 381 | 'MT_ANONYMOUS' => $this->request->is_set('u'), |
||
| 382 | 'MT_USERNAME' => $this->request->variable('username', '', true), |
||
| 383 | 'MT_FIRST_NAME' => $this->request->variable('first_name', '', true), |
||
| 384 | 'MT_LAST_NAME' => $this->request->variable('last_name', '', true), |
||
| 385 | 'MT_PAYER_EMAIL' => $this->request->variable('payer_email', '', true), |
||
| 386 | 'MT_RESIDENCE_COUNTRY' => $this->request->variable('residence_country', ''), |
||
| 387 | 'MT_MC_GROSS' => $this->request->variable('mc_gross', (float) 0), |
||
| 388 | 'MT_MC_CURRENCY' => $this->request->variable('mc_currency', ''), |
||
| 389 | 'MT_MC_FEE' => $this->request->variable('mc_fee', (float) 0), |
||
| 390 | 'MT_PAYMENT_DATE_YEAR' => $this->request->variable('payment_date_year', (int) $this->user->format_date(time(), 'Y')), |
||
| 391 | 'MT_PAYMENT_DATE_MONTH' => $this->request->variable('payment_date_month', (int) $this->user->format_date(time(), 'n')), |
||
| 392 | 'MT_PAYMENT_DATE_DAY' => $this->request->variable('payment_date_day', (int) $this->user->format_date(time(), 'j')), |
||
| 393 | 'MT_PAYMENT_TIME' => $this->request->variable('payment_time', $this->user->format_date(time(), 'H:i:s')), |
||
| 394 | 'MT_MEMO' => $this->request->variable('memo', '', true), |
||
| 395 | ); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns a list of valid times that the user can provide in the manual transaction form |
||
| 400 | * |
||
| 401 | * @return array Array of strings representing the current time, each in a different format |
||
| 402 | * @access private |
||
| 403 | */ |
||
| 404 | private function get_payment_time_examples() |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * View log |
||
| 425 | * |
||
| 426 | * @param array &$log The result array with the logs |
||
| 427 | * @param mixed &$log_count If $log_count is set to false, we will skip counting all entries in the |
||
| 428 | * database. Otherwise an integer with the number of total matching entries is returned. |
||
| 429 | * @param int $limit Limit the number of entries that are returned |
||
| 430 | * @param int $offset Offset when fetching the log entries, f.e. when paginating |
||
| 431 | * @param int $limit_days |
||
| 432 | * @param string $sort_by SQL order option, e.g. 'l.log_time DESC' |
||
| 433 | * @param string $keywords Will only return log entries that have the keywords in log_operation or log_data |
||
| 434 | * |
||
| 435 | * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high) |
||
| 436 | * @access private |
||
| 437 | */ |
||
| 438 | private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
| 439 | { |
||
| 440 | $count_logs = ($log_count !== false); |
||
| 441 | |||
| 442 | $log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords); |
||
| 443 | $log_count = $this->get_log_count(); |
||
| 444 | |||
| 445 | return $this->get_valid_offset(); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param bool $count_logs |
||
| 450 | * @param int $limit |
||
| 451 | * @param int $offset |
||
| 452 | * @param int $log_time |
||
| 453 | * @param string $sort_by |
||
| 454 | * @param string $keywords |
||
| 455 | * |
||
| 456 | * @return array $log |
||
| 457 | * @access private |
||
| 458 | */ |
||
| 459 | private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
| 460 | { |
||
| 461 | $this->entry_count = 0; |
||
| 462 | $this->last_page_offset = $offset; |
||
| 463 | $url_ary = array(); |
||
| 464 | |||
| 465 | if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path) |
||
| 466 | { |
||
| 467 | $url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&mode=overview'); |
||
| 468 | $url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&mode=transactions'); |
||
| 469 | |||
| 470 | } |
||
| 471 | else |
||
| 472 | { |
||
| 473 | $url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile'); |
||
| 474 | $url_ary['txn_url'] = ''; |
||
| 475 | } |
||
| 476 | |||
| 477 | $get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time); |
||
| 478 | |||
| 479 | if ($count_logs) |
||
| 480 | { |
||
| 481 | $this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id'); |
||
| 482 | |||
| 483 | if ($this->entry_count == 0) |
||
| 484 | { |
||
| 485 | // Save the queries, because there are no logs to display |
||
| 486 | $this->last_page_offset = 0; |
||
| 487 | |||
| 488 | return array(); |
||
| 489 | } |
||
| 490 | |||
| 491 | // Return the user to the last page that is valid |
||
| 492 | while ($this->last_page_offset >= $this->entry_count) |
||
| 493 | { |
||
| 494 | $this->last_page_offset = max(0, $this->last_page_offset - $limit); |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return integer |
||
| 503 | */ |
||
| 504 | public function get_log_count() |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return integer |
||
| 511 | */ |
||
| 512 | public function get_valid_offset() |
||
| 513 | { |
||
| 514 | return ($this->last_page_offset) ? (int) $this->last_page_offset : 0; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Prepare data array() before send it to $entity |
||
| 519 | * |
||
| 520 | * @param array $transaction_data |
||
| 521 | * |
||
| 522 | * @return array |
||
| 523 | * @throws transaction_exception |
||
| 524 | */ |
||
| 525 | private function build_data_ary($transaction_data) |
||
| 526 | { |
||
| 527 | $errors = array(); |
||
| 528 | |||
| 529 | if ($this->request->is_set('u') && $transaction_data['MT_USERNAME'] === '') |
||
| 530 | { |
||
| 531 | $user_id = ANONYMOUS; |
||
| 532 | } |
||
| 533 | else |
||
| 534 | { |
||
| 535 | $user_ary = $this->ppde_operator->query_donor_user_data('username', $transaction_data['MT_USERNAME']); |
||
| 536 | |||
| 537 | if ($user_ary) |
||
| 538 | { |
||
| 539 | $user_id = $user_ary['user_id']; |
||
| 540 | } |
||
| 541 | else |
||
| 542 | { |
||
| 543 | $errors[] = $this->language->lang('PPDE_MT_DONOR_NOT_FOUND', $transaction_data['MT_USERNAME']); |
||
| 544 | } |
||
| 545 | } |
||
| 546 | |||
| 547 | if ($transaction_data['MT_MC_GROSS'] <= 0) |
||
| 548 | { |
||
| 549 | $errors[] = $this->language->lang('PPDE_MT_MC_GROSS_TOO_LOW'); |
||
| 550 | } |
||
| 551 | |||
| 552 | if ($transaction_data['MT_MC_FEE'] < 0) |
||
| 553 | { |
||
| 554 | $errors[] = $this->language->lang('PPDE_MT_MC_FEE_NEGATIVE'); |
||
| 555 | } |
||
| 556 | |||
| 557 | if ($transaction_data['MT_MC_FEE'] >= $transaction_data['MT_MC_GROSS']) |
||
| 558 | { |
||
| 559 | $errors[] = $this->language->lang('PPDE_MT_MC_FEE_TOO_HIGH'); |
||
| 560 | } |
||
| 561 | |||
| 562 | $payment_date = implode('-', [ |
||
| 563 | $transaction_data['MT_PAYMENT_DATE_YEAR'], |
||
| 564 | $transaction_data['MT_PAYMENT_DATE_MONTH'], |
||
| 565 | $transaction_data['MT_PAYMENT_DATE_DAY'], |
||
| 566 | ]); |
||
| 567 | |||
| 568 | $payment_date_timestamp_at_midnight = $this->user->get_timestamp_from_format('Y-m-d H:i:s', $payment_date . ' 00:00:00'); |
||
| 569 | |||
| 570 | if ($payment_date_timestamp_at_midnight === false) |
||
|
|
|||
| 571 | { |
||
| 572 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_ERROR', $payment_date); |
||
| 573 | } |
||
| 574 | |||
| 575 | $payment_time = $transaction_data['MT_PAYMENT_TIME']; |
||
| 576 | $payment_time_timestamp = strtotime($payment_time); |
||
| 577 | |||
| 578 | if ($payment_time_timestamp === false) |
||
| 579 | { |
||
| 580 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_TIME_ERROR', $payment_time); |
||
| 581 | } |
||
| 582 | |||
| 583 | // Normalize payment time to start from today at midnight |
||
| 584 | $payment_time_timestamp_from_midnight = $payment_time_timestamp - strtotime('00:00:00'); |
||
| 585 | |||
| 586 | $payment_date_time = $payment_date_timestamp_at_midnight + $payment_time_timestamp_from_midnight; |
||
| 587 | |||
| 588 | if ($payment_date_time > time()) |
||
| 589 | { |
||
| 590 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_FUTURE', $this->user->format_date($payment_date_time)); |
||
| 591 | } |
||
| 592 | |||
| 593 | if ($errors) |
||
| 594 | { |
||
| 595 | throw (new transaction_exception())->set_errors($errors); |
||
| 596 | } |
||
| 597 | |||
| 598 | return array( |
||
| 599 | 'business' => $this->config['ppde_account_id'], |
||
| 600 | 'confirmed' => true, |
||
| 601 | 'exchange_rate' => '', |
||
| 602 | 'first_name' => $transaction_data['MT_FIRST_NAME'], |
||
| 603 | 'item_name' => '', |
||
| 604 | 'item_number' => implode('_', ['uid', $user_id, time()]), |
||
|
1 ignored issue
–
show
|
|||
| 605 | 'last_name' => $transaction_data['MT_LAST_NAME'], |
||
| 606 | 'mc_currency' => $transaction_data['MT_MC_CURRENCY'], |
||
| 607 | 'mc_gross' => $transaction_data['MT_MC_GROSS'], |
||
| 608 | 'mc_fee' => $transaction_data['MT_MC_FEE'], |
||
| 609 | 'net_amount' => (float) 0, // This value is calculated in core_actions:log_to_db() |
||
| 610 | 'parent_txn_id' => '', |
||
| 611 | 'payer_email' => $transaction_data['MT_PAYER_EMAIL'], |
||
| 612 | 'payer_id' => '', |
||
| 613 | 'payer_status' => '', |
||
| 614 | 'payment_date' => $payment_date_time, |
||
| 615 | 'payment_status' => 'Completed', |
||
| 616 | 'payment_type' => '', |
||
| 617 | 'memo' => $transaction_data['MT_MEMO'], |
||
| 618 | 'receiver_id' => '', |
||
| 619 | 'receiver_email' => '', |
||
| 620 | 'residence_country' => strtoupper($transaction_data['MT_RESIDENCE_COUNTRY']), |
||
| 621 | 'settle_amount' => (float) 0, |
||
| 622 | 'settle_currency' => '', |
||
| 623 | 'test_ipn' => false, |
||
| 624 | 'txn_errors' => '', |
||
| 625 | 'txn_id' => 'PPDE' . gen_rand_string(13), |
||
| 626 | 'txn_type' => 'ppde_manual_donation', |
||
| 627 | 'user_id' => $user_id, |
||
| 628 | ); |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Set log output vars for display in the template |
||
| 633 | * |
||
| 634 | * @param array $row |
||
| 635 | * |
||
| 636 | * @return void |
||
| 637 | * @access protected |
||
| 638 | */ |
||
| 639 | protected function display_log_assign_template_vars($row) |
||
| 652 | )); |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Set output vars for display in the template |
||
| 657 | * |
||
| 658 | * @param array $data |
||
| 659 | * |
||
| 660 | * @return void |
||
| 661 | * @access protected |
||
| 662 | */ |
||
| 663 | protected function action_assign_template_vars($data) |
||
| 698 | )); |
||
| 699 | } |
||
| 700 | } |
||
| 701 |