Total Complexity | 70 |
Total Lines | 704 |
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 |
||
42 | class admin_transactions_controller extends admin_main |
||
43 | { |
||
44 | public $ppde_operator; |
||
45 | protected $adm_relative_path; |
||
46 | protected $auth; |
||
47 | protected $user_loader; |
||
48 | protected $entry_count; |
||
49 | protected $last_page_offset; |
||
50 | protected $php_ext; |
||
51 | protected $phpbb_admin_path; |
||
52 | protected $phpbb_root_path; |
||
53 | protected $ppde_actions; |
||
54 | protected $ppde_actions_currency; |
||
55 | protected $table_prefix; |
||
56 | protected $table_ppde_transactions; |
||
57 | |||
58 | /** |
||
59 | * Constructor |
||
60 | * |
||
61 | * @param auth $auth Authentication object |
||
62 | * @param config $config Config object |
||
63 | * @param ContainerInterface $container Service container interface |
||
64 | * @param language $language Language user object |
||
65 | * @param log $log The phpBB log system |
||
66 | * @param core $ppde_actions PPDE actions object |
||
67 | * @param currency $ppde_actions_currency PPDE currency actions object |
||
68 | * @param transactions $ppde_operator_transactions Operator object |
||
69 | * @param request $request Request object |
||
70 | * @param template $template Template object |
||
71 | * @param user $user User object. |
||
72 | * @param user_loader $user_loader User loader object |
||
73 | * @param string $adm_relative_path phpBB admin relative path |
||
74 | * @param string $phpbb_root_path phpBB root path |
||
75 | * @param string $php_ext phpEx |
||
76 | * @param string $table_prefix The table prefix |
||
77 | * @param string $table_ppde_transactions Name of the table used to store data |
||
78 | * |
||
79 | * @access public |
||
80 | */ |
||
81 | 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, user_loader $user_loader, $adm_relative_path, $phpbb_root_path, $php_ext, $table_prefix, $table_ppde_transactions) |
||
82 | { |
||
83 | $this->auth = $auth; |
||
84 | $this->config = $config; |
||
85 | $this->container = $container; |
||
86 | $this->language = $language; |
||
87 | $this->log = $log; |
||
88 | $this->ppde_actions = $ppde_actions; |
||
89 | $this->ppde_actions_currency = $ppde_actions_currency; |
||
90 | $this->ppde_operator = $ppde_operator_transactions; |
||
91 | $this->request = $request; |
||
92 | $this->template = $template; |
||
93 | $this->user = $user; |
||
94 | $this->user_loader = $user_loader; |
||
95 | $this->adm_relative_path = $adm_relative_path; |
||
96 | $this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path; |
||
97 | $this->phpbb_root_path = $phpbb_root_path; |
||
98 | $this->php_ext = $php_ext; |
||
99 | $this->table_prefix = $table_prefix; |
||
100 | $this->table_ppde_transactions = $table_ppde_transactions; |
||
101 | parent::__construct( |
||
102 | 'transactions', |
||
103 | 'PPDE_DT', |
||
104 | 'transaction' |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Display the transactions list |
||
110 | * |
||
111 | * @param string $id Module id |
||
112 | * @param string $mode Module categorie |
||
113 | * @param string $action Action name |
||
114 | * |
||
115 | * @return void |
||
116 | * @access public |
||
117 | */ |
||
118 | public function display_transactions($id, $mode, $action) |
||
119 | { |
||
120 | // Set up general vars |
||
121 | $args = array(); |
||
122 | $start = $this->request->variable('start', 0); |
||
123 | $deletemark = $this->request->is_set('delmarked') ? $this->request->variable('delmarked', false) : false; |
||
124 | $deleteall = $this->request->is_set('delall') ? $this->request->variable('delall', false) : false; |
||
125 | $marked = $this->request->variable('mark', array(0)); |
||
126 | $txn_approve = $this->request->is_set('approve'); |
||
127 | $txn_approved = $this->request->variable('txn_errors_approved', 0); |
||
128 | $txn_add = $this->request->is_set('add'); |
||
129 | $txn_change = $this->request->is_set_post('change'); |
||
130 | // Sort keys |
||
131 | $sort_days = $this->request->variable('st', 0); |
||
132 | $sort_key = $this->request->variable('sk', 't'); |
||
133 | $sort_dir = $this->request->variable('sd', 'd'); |
||
134 | |||
135 | // Prepares args for entries deletion |
||
136 | if (($deletemark || $deleteall) && $this->auth->acl_get('a_ppde_manage')) |
||
137 | { |
||
138 | $action = 'delete'; |
||
139 | $args = array( |
||
140 | 'hidden_fields' => array( |
||
141 | 'start' => $start, |
||
142 | 'delall' => $deleteall, |
||
143 | 'delmarked' => $deletemark, |
||
144 | 'mark' => $marked, |
||
145 | 'st' => $sort_days, |
||
146 | 'sk' => $sort_key, |
||
147 | 'sd' => $sort_dir, |
||
148 | 'i' => $id, |
||
149 | 'mode' => $mode, |
||
150 | ), |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | if ($txn_approve) |
||
155 | { |
||
156 | $transaction_id = $this->request->variable('id', 0); |
||
157 | $action = 'approve'; |
||
158 | $args = array( |
||
159 | 'hidden_fields' => array( |
||
160 | 'approve' => true, |
||
161 | 'id' => $transaction_id, |
||
162 | 'txn_errors_approved' => $txn_approved, |
||
163 | ), |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | if ($txn_add) |
||
168 | { |
||
169 | $action = 'add'; |
||
170 | } |
||
171 | else if ($txn_change) |
||
172 | { |
||
173 | $action = 'change'; |
||
174 | } |
||
175 | |||
176 | $action = $this->do_action($action, $args); |
||
177 | |||
178 | if (!$action) |
||
179 | { |
||
180 | /** @type \phpbb\pagination $pagination */ |
||
181 | $pagination = $this->container->get('pagination'); |
||
182 | |||
183 | // Sorting |
||
184 | $limit_days = array(0 => $this->language->lang('ALL_ENTRIES'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR')); |
||
185 | $sort_by_text = array('txn' => $this->language->lang('PPDE_DT_SORT_TXN_ID'), 'u' => $this->language->lang('PPDE_DT_SORT_DONORS'), 'ipn' => $this->language->lang('PPDE_DT_SORT_IPN_STATUS'), 'ipn_test' => $this->language->lang('PPDE_DT_SORT_IPN_TYPE'), 'ps' => $this->language->lang('PPDE_DT_SORT_PAYMENT_STATUS'), 't' => $this->language->lang('SORT_DATE')); |
||
186 | $sort_by_sql = array('txn' => 'txn.txn_id', 'u' => 'u.username_clean', 'ipn' => 'txn.confirmed', 'ipn_test' => 'txn.test_ipn', 'ps' => 'txn.payment_status', 't' => 'txn.payment_date'); |
||
187 | |||
188 | $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = ''; |
||
189 | gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); |
||
190 | |||
191 | // Define where and sort sql for use in displaying transactions |
||
192 | $sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0; |
||
193 | $sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC'); |
||
194 | |||
195 | $keywords = $this->request->variable('keywords', '', true); |
||
196 | $keywords_param = !empty($keywords) ? '&keywords=' . urlencode(htmlspecialchars_decode($keywords)) : ''; |
||
197 | |||
198 | // Grab log data |
||
199 | $log_data = array(); |
||
200 | $log_count = 0; |
||
201 | |||
202 | $this->view_txn_log($log_data, $log_count, (int) $this->config['topics_per_page'], $start, $sql_where, $sql_sort, $keywords); |
||
203 | |||
204 | $base_url = $this->u_action . '&' . $u_sort_param . $keywords_param; |
||
205 | $pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, (int) $this->config['topics_per_page'], $start); |
||
206 | |||
207 | $this->template->assign_vars(array( |
||
208 | 'S_CLEARLOGS' => $this->auth->acl_get('a_ppde_manage'), |
||
209 | 'S_KEYWORDS' => $keywords, |
||
210 | 'S_LIMIT_DAYS' => $s_limit_days, |
||
211 | 'S_SORT_KEY' => $s_sort_key, |
||
212 | 'S_SORT_DIR' => $s_sort_dir, |
||
213 | 'S_TXN' => $mode, |
||
214 | 'U_ACTION' => $this->u_action . '&' . $u_sort_param . $keywords_param . '&start=' . $start, |
||
215 | )); |
||
216 | |||
217 | array_map(array($this, 'display_log_assign_template_vars'), $log_data); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Do action regarding the value of $action |
||
223 | * |
||
224 | * @param string $action Requested action |
||
225 | * @param array $args Arguments required for the action |
||
226 | * |
||
227 | * @return string |
||
228 | * @access private |
||
229 | */ |
||
230 | private function do_action($action, $args) |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Does actions for validated transaction |
||
401 | * |
||
402 | * @param bool $is_member |
||
403 | * |
||
404 | * @return void |
||
405 | * @access private |
||
406 | */ |
||
407 | private function do_transactions_actions($is_member) |
||
408 | { |
||
409 | $this->ppde_actions->update_overview_stats(); |
||
410 | $this->ppde_actions->update_raised_amount(); |
||
411 | |||
412 | if ($is_member) |
||
413 | { |
||
414 | $this->ppde_actions->update_donor_stats(); |
||
415 | $this->ppde_actions->donors_group_user_add(); |
||
416 | $this->ppde_actions->notification->notify_donor_donation_received(); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Returns requested data from manual transaction form |
||
422 | * |
||
423 | * @return array |
||
424 | * @access private |
||
425 | */ |
||
426 | private function request_transaction_vars() |
||
427 | { |
||
428 | return array( |
||
429 | 'MT_ANONYMOUS' => $this->request->is_set('u'), |
||
430 | 'MT_USERNAME' => $this->request->variable('username', '', true), |
||
431 | 'MT_FIRST_NAME' => $this->request->variable('first_name', '', true), |
||
432 | 'MT_LAST_NAME' => $this->request->variable('last_name', '', true), |
||
433 | 'MT_PAYER_EMAIL' => $this->request->variable('payer_email', '', true), |
||
434 | 'MT_RESIDENCE_COUNTRY' => $this->request->variable('residence_country', ''), |
||
435 | 'MT_MC_GROSS' => $this->request->variable('mc_gross', (float) 0), |
||
436 | 'MT_MC_CURRENCY' => $this->request->variable('mc_currency', ''), |
||
437 | 'MT_MC_FEE' => $this->request->variable('mc_fee', (float) 0), |
||
438 | 'MT_PAYMENT_DATE_YEAR' => $this->request->variable('payment_date_year', (int) $this->user->format_date(time(), 'Y')), |
||
439 | 'MT_PAYMENT_DATE_MONTH' => $this->request->variable('payment_date_month', (int) $this->user->format_date(time(), 'n')), |
||
440 | 'MT_PAYMENT_DATE_DAY' => $this->request->variable('payment_date_day', (int) $this->user->format_date(time(), 'j')), |
||
441 | 'MT_PAYMENT_TIME' => $this->request->variable('payment_time', $this->user->format_date(time(), 'H:i:s')), |
||
442 | 'MT_MEMO' => $this->request->variable('memo', '', true), |
||
443 | ); |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Returns a list of valid times that the user can provide in the manual transaction form |
||
448 | * |
||
449 | * @return array Array of strings representing the current time, each in a different format |
||
450 | * @access private |
||
451 | */ |
||
452 | private function get_payment_time_examples() |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * View log |
||
473 | * |
||
474 | * @param array &$log The result array with the logs |
||
475 | * @param mixed &$log_count If $log_count is set to false, we will skip counting all entries in the |
||
476 | * database. Otherwise an integer with the number of total matching entries is returned. |
||
477 | * @param int $limit Limit the number of entries that are returned |
||
478 | * @param int $offset Offset when fetching the log entries, f.e. when paginating |
||
479 | * @param int $limit_days |
||
480 | * @param string $sort_by SQL order option, e.g. 'l.log_time DESC' |
||
481 | * @param string $keywords Will only return log entries that have the keywords in log_operation or log_data |
||
482 | * |
||
483 | * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high) |
||
484 | * @access private |
||
485 | */ |
||
486 | private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
487 | { |
||
488 | $count_logs = ($log_count !== false); |
||
489 | |||
490 | $log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords); |
||
491 | $log_count = $this->get_log_count(); |
||
492 | |||
493 | return $this->get_valid_offset(); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * @param bool $count_logs |
||
498 | * @param int $limit |
||
499 | * @param int $offset |
||
500 | * @param int $log_time |
||
501 | * @param string $sort_by |
||
502 | * @param string $keywords |
||
503 | * |
||
504 | * @return array $log |
||
505 | * @access private |
||
506 | */ |
||
507 | private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
508 | { |
||
509 | $this->entry_count = 0; |
||
510 | $this->last_page_offset = $offset; |
||
511 | $url_ary = array(); |
||
512 | |||
513 | if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path) |
||
514 | { |
||
515 | $url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&mode=overview'); |
||
516 | $url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&mode=transactions'); |
||
517 | |||
518 | } |
||
519 | else |
||
520 | { |
||
521 | $url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile'); |
||
522 | $url_ary['txn_url'] = ''; |
||
523 | } |
||
524 | |||
525 | $get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time); |
||
526 | |||
527 | if ($count_logs) |
||
528 | { |
||
529 | $this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id'); |
||
530 | |||
531 | if ($this->entry_count == 0) |
||
532 | { |
||
533 | // Save the queries, because there are no logs to display |
||
534 | $this->last_page_offset = 0; |
||
535 | |||
536 | return array(); |
||
537 | } |
||
538 | |||
539 | // Return the user to the last page that is valid |
||
540 | while ($this->last_page_offset >= $this->entry_count) |
||
541 | { |
||
542 | $this->last_page_offset = max(0, $this->last_page_offset - $limit); |
||
543 | } |
||
544 | } |
||
545 | |||
546 | return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset); |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * @return integer |
||
551 | */ |
||
552 | public function get_log_count() |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * @return integer |
||
559 | */ |
||
560 | public function get_valid_offset() |
||
561 | { |
||
562 | return ($this->last_page_offset) ? (int) $this->last_page_offset : 0; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Prepare data array() before send it to $entity |
||
567 | * |
||
568 | * @param array $transaction_data |
||
569 | * |
||
570 | * @return array |
||
571 | * @throws transaction_exception |
||
572 | */ |
||
573 | private function build_data_ary($transaction_data) |
||
574 | { |
||
575 | $errors = array(); |
||
576 | |||
577 | if ($this->request->is_set('u') && $transaction_data['MT_USERNAME'] === '') |
||
578 | { |
||
579 | $user_id = ANONYMOUS; |
||
580 | } |
||
581 | else |
||
582 | { |
||
583 | $user_ary = $this->ppde_operator->query_donor_user_data('username', $transaction_data['MT_USERNAME']); |
||
584 | |||
585 | if ($user_ary) |
||
586 | { |
||
587 | $user_id = $user_ary['user_id']; |
||
588 | } |
||
589 | else |
||
590 | { |
||
591 | $errors[] = $this->language->lang('PPDE_MT_DONOR_NOT_FOUND', $transaction_data['MT_USERNAME']); |
||
592 | } |
||
593 | } |
||
594 | |||
595 | if ($transaction_data['MT_MC_GROSS'] <= 0) |
||
596 | { |
||
597 | $errors[] = $this->language->lang('PPDE_MT_MC_GROSS_TOO_LOW'); |
||
598 | } |
||
599 | |||
600 | if ($transaction_data['MT_MC_FEE'] < 0) |
||
601 | { |
||
602 | $errors[] = $this->language->lang('PPDE_MT_MC_FEE_NEGATIVE'); |
||
603 | } |
||
604 | |||
605 | if ($transaction_data['MT_MC_FEE'] >= $transaction_data['MT_MC_GROSS']) |
||
606 | { |
||
607 | $errors[] = $this->language->lang('PPDE_MT_MC_FEE_TOO_HIGH'); |
||
608 | } |
||
609 | |||
610 | $payment_date = implode('-', [ |
||
611 | $transaction_data['MT_PAYMENT_DATE_YEAR'], |
||
612 | $transaction_data['MT_PAYMENT_DATE_MONTH'], |
||
613 | $transaction_data['MT_PAYMENT_DATE_DAY'], |
||
614 | ]); |
||
615 | |||
616 | $payment_date_timestamp_at_midnight = $this->user->get_timestamp_from_format('Y-m-d H:i:s', $payment_date . ' 00:00:00'); |
||
617 | |||
618 | if ($payment_date_timestamp_at_midnight === false) |
||
|
|||
619 | { |
||
620 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_ERROR', $payment_date); |
||
621 | } |
||
622 | |||
623 | $payment_time = $transaction_data['MT_PAYMENT_TIME']; |
||
624 | $payment_time_timestamp = strtotime($payment_time); |
||
625 | |||
626 | if ($payment_time_timestamp === false) |
||
627 | { |
||
628 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_TIME_ERROR', $payment_time); |
||
629 | } |
||
630 | |||
631 | // Normalize payment time to start from today at midnight |
||
632 | $payment_time_timestamp_from_midnight = $payment_time_timestamp - strtotime('00:00:00'); |
||
633 | |||
634 | $payment_date_time = $payment_date_timestamp_at_midnight + $payment_time_timestamp_from_midnight; |
||
635 | |||
636 | if ($payment_date_time > time()) |
||
637 | { |
||
638 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_FUTURE', $this->user->format_date($payment_date_time)); |
||
639 | } |
||
640 | |||
641 | if ($errors) |
||
642 | { |
||
643 | throw (new transaction_exception())->set_errors($errors); |
||
644 | } |
||
645 | |||
646 | return array( |
||
647 | 'business' => $this->config['ppde_account_id'], |
||
648 | 'confirmed' => true, |
||
649 | 'exchange_rate' => '', |
||
650 | 'first_name' => $transaction_data['MT_FIRST_NAME'], |
||
651 | 'item_name' => '', |
||
652 | 'item_number' => implode('_', ['uid', $user_id, time()]), |
||
1 ignored issue
–
show
|
|||
653 | 'last_name' => $transaction_data['MT_LAST_NAME'], |
||
654 | 'mc_currency' => $transaction_data['MT_MC_CURRENCY'], |
||
655 | 'mc_gross' => $transaction_data['MT_MC_GROSS'], |
||
656 | 'mc_fee' => $transaction_data['MT_MC_FEE'], |
||
657 | 'net_amount' => (float) 0, // This value is calculated in core_actions:log_to_db() |
||
658 | 'parent_txn_id' => '', |
||
659 | 'payer_email' => $transaction_data['MT_PAYER_EMAIL'], |
||
660 | 'payer_id' => '', |
||
661 | 'payer_status' => '', |
||
662 | 'payment_date' => $payment_date_time, |
||
663 | 'payment_status' => 'Completed', |
||
664 | 'payment_type' => '', |
||
665 | 'memo' => $transaction_data['MT_MEMO'], |
||
666 | 'receiver_id' => '', |
||
667 | 'receiver_email' => '', |
||
668 | 'residence_country' => strtoupper($transaction_data['MT_RESIDENCE_COUNTRY']), |
||
669 | 'settle_amount' => (float) 0, |
||
670 | 'settle_currency' => '', |
||
671 | 'test_ipn' => false, |
||
672 | 'txn_errors' => '', |
||
673 | 'txn_id' => 'PPDE' . gen_rand_string(13), |
||
674 | 'txn_type' => 'ppde_manual_donation', |
||
675 | 'user_id' => $user_id, |
||
676 | ); |
||
677 | } |
||
678 | |||
679 | /** |
||
680 | * Set log output vars for display in the template |
||
681 | * |
||
682 | * @param array $row |
||
683 | * |
||
684 | * @return void |
||
685 | * @access protected |
||
686 | */ |
||
687 | protected function display_log_assign_template_vars($row) |
||
700 | )); |
||
701 | } |
||
702 | |||
703 | /** |
||
704 | * Set output vars for display in the template |
||
705 | * |
||
706 | * @param array $data |
||
707 | * |
||
708 | * @return void |
||
709 | * @access protected |
||
710 | */ |
||
711 | protected function action_assign_template_vars($data) |
||
746 | )); |
||
747 | } |
||
748 | } |
||
749 |