Total Complexity | 62 |
Total Lines | 639 |
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) |
||
221 | { |
||
222 | // Initiate an entity |
||
223 | /** @type \skouat\ppde\entity\transactions $entity */ |
||
224 | $entity = $this->get_container_entity(); |
||
225 | |||
226 | switch ($action) |
||
227 | { |
||
228 | case 'view': |
||
229 | // Request Identifier of the transaction |
||
230 | $transaction_id = $this->request->variable('id', 0); |
||
231 | |||
232 | // add field username to the table schema needed by entity->import() |
||
233 | $additional_table_schema = array( |
||
234 | 'item_username' => array('name' => 'username', 'type' => 'string'), |
||
235 | 'item_user_colour' => array('name' => 'user_colour', 'type' => 'string'), |
||
236 | ); |
||
237 | |||
238 | // Grab transaction data |
||
239 | $data_ary = $entity->get_data($this->ppde_operator->build_sql_data($transaction_id), $additional_table_schema); |
||
240 | |||
241 | array_map(array($this, 'action_assign_template_vars'), $data_ary); |
||
242 | |||
243 | $this->template->assign_vars(array( |
||
244 | 'U_ACTION' => $this->u_action, |
||
245 | 'U_BACK' => $this->u_action, |
||
246 | 'S_VIEW' => true, |
||
247 | )); |
||
248 | break; |
||
249 | case 'delete': |
||
250 | if (confirm_box(true)) |
||
251 | { |
||
252 | $where_sql = ''; |
||
253 | |||
254 | if ($args['hidden_fields']['delmarked'] && count($args['hidden_fields']['mark'])) |
||
255 | { |
||
256 | $where_sql = $this->ppde_operator->build_marked_where_sql($args['hidden_fields']['mark']); |
||
257 | } |
||
258 | |||
259 | if ($where_sql || $args['hidden_fields']['delall']) |
||
260 | { |
||
261 | $entity->delete(0, '', $where_sql, $args['hidden_fields']['delall']); |
||
262 | $this->ppde_actions->set_ipn_test_properties(true); |
||
263 | $this->ppde_actions->update_overview_stats(); |
||
264 | $this->ppde_actions->set_ipn_test_properties(false); |
||
265 | $this->ppde_actions->update_overview_stats(); |
||
266 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_PURGED', time()); |
||
267 | } |
||
268 | } |
||
269 | else |
||
270 | { |
||
271 | confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields'])); |
||
272 | } |
||
273 | // Clear $action status |
||
274 | $action = ''; |
||
275 | break; |
||
276 | case 'approve': |
||
277 | if (confirm_box(true)) |
||
278 | { |
||
279 | $transaction_id = (int) $args['hidden_fields']['id']; |
||
280 | $txn_approved = !empty($args['hidden_fields']['txn_errors_approved']) ? false : true; |
||
281 | |||
282 | // Update DB record |
||
283 | $entity->load($transaction_id); |
||
284 | $entity->set_txn_errors_approved($txn_approved); |
||
285 | $entity->save(false); |
||
286 | |||
287 | // Prepare transaction settings before doing actions |
||
288 | $this->ppde_actions->set_transaction_data($entity->get_data($this->ppde_operator->build_sql_data($transaction_id))); |
||
289 | $this->ppde_actions->set_ipn_test_properties($entity->get_test_ipn()); |
||
290 | $this->ppde_actions->is_donor_is_member(); |
||
291 | |||
292 | // Do the actions related to the approval of the transaction |
||
293 | $this->ppde_actions->update_overview_stats(); |
||
294 | $this->ppde_actions->update_raised_amount(); |
||
295 | if (!$this->ppde_actions->get_ipn_test() && $this->ppde_actions->get_donor_is_member()) |
||
296 | { |
||
297 | $this->ppde_actions->update_donor_stats(); |
||
298 | $this->ppde_actions->donors_group_user_add(); |
||
299 | $this->ppde_actions->notification->notify_donor_donation_received(); |
||
300 | } |
||
301 | |||
302 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED', time()); |
||
303 | } |
||
304 | else |
||
305 | { |
||
306 | confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields'])); |
||
307 | } |
||
308 | // Clear $action status |
||
309 | $action = ''; |
||
310 | break; |
||
311 | case 'add': |
||
312 | $errors = array(); |
||
313 | |||
314 | $transaction_data = array( |
||
315 | 'MT_ANONYMOUS' => $this->request->is_set('u'), |
||
316 | 'MT_USERNAME' => $this->request->variable('username', '', true), |
||
317 | 'MT_FIRST_NAME' => $this->request->variable('first_name', '', true), |
||
318 | 'MT_LAST_NAME' => $this->request->variable('last_name', '', true), |
||
319 | 'MT_PAYER_EMAIL' => $this->request->variable('payer_email', '', true), |
||
320 | 'MT_RESIDENCE_COUNTRY' => $this->request->variable('residence_country', ''), |
||
321 | 'MT_MC_GROSS' => $this->request->variable('mc_gross', (float) 0), |
||
322 | 'MT_MC_CURRENCY' => $this->request->variable('mc_currency', ''), |
||
323 | 'MT_MC_FEE' => $this->request->variable('mc_fee', (float) 0), |
||
324 | 'MT_PAYMENT_DATE_YEAR' => $this->request->variable('payment_date_year', (int) $this->user->format_date(time(), 'Y')), |
||
325 | 'MT_PAYMENT_DATE_MONTH' => $this->request->variable('payment_date_month', (int) $this->user->format_date(time(), 'n')), |
||
326 | 'MT_PAYMENT_DATE_DAY' => $this->request->variable('payment_date_day', (int) $this->user->format_date(time(), 'j')), |
||
327 | 'MT_PAYMENT_TIME' => $this->request->variable('payment_time', $this->user->format_date(time(), 'H:i:s')), |
||
328 | 'MT_MEMO' => $this->request->variable('memo', '', true), |
||
329 | ); |
||
330 | |||
331 | if ($this->request->is_set_post('submit')) |
||
332 | { |
||
333 | try |
||
334 | { |
||
335 | $data_ary = $this->build_data_ary($transaction_data); |
||
336 | |||
337 | $this->ppde_actions->log_to_db($data_ary); |
||
338 | $this->ppde_actions->set_transaction_data($transaction_data); |
||
339 | $this->ppde_actions->is_donor_is_member(); |
||
340 | $this->ppde_actions->update_overview_stats(); |
||
341 | $this->ppde_actions->update_raised_amount(); |
||
342 | |||
343 | if ($this->ppde_actions->get_donor_is_member()) |
||
344 | { |
||
345 | $this->ppde_actions->update_donor_stats(); |
||
346 | $this->ppde_actions->donors_group_user_add(); |
||
347 | $this->ppde_actions->notification->notify_donor_donation_received(); |
||
348 | } |
||
349 | |||
350 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_MT_ADDED', time(), array($transaction_data['MT_USERNAME'])); |
||
351 | trigger_error($this->language->lang('PPDE_MT_ADDED') . adm_back_link($this->u_action)); |
||
352 | } |
||
353 | catch (transaction_exception $e) |
||
354 | { |
||
355 | $errors = $e->get_errors(); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | $this->ppde_actions_currency->build_currency_select_menu((int) $this->config['ppde_default_currency']); |
||
360 | |||
361 | $this->s_error_assign_template_vars($errors); |
||
362 | |||
363 | $this->template->assign_vars($transaction_data); |
||
364 | |||
365 | $this->template->assign_vars(array( |
||
366 | 'U_ACTION' => $this->u_action, |
||
367 | 'U_BACK' => $this->u_action, |
||
368 | 'S_ADD' => true, |
||
369 | 'ANONYMOUS_USER_ID' => ANONYMOUS, |
||
370 | 'U_FIND_USERNAME' => append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=searchuser&form=manual_transaction&field=username&select_single=true'), |
||
371 | 'PAYMENT_TIME_FORMATS' => $this->get_payment_time_examples(), |
||
372 | )); |
||
373 | break; |
||
374 | } |
||
375 | |||
376 | return $action; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Returns a list of valid times that the user can provide in the manual transaction form |
||
381 | * |
||
382 | * @return array Array of strings representing the current time, each in a different format |
||
383 | * @access private |
||
384 | */ |
||
385 | private function get_payment_time_examples() |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * View log |
||
406 | * |
||
407 | * @param array &$log The result array with the logs |
||
408 | * @param mixed &$log_count If $log_count is set to false, we will skip counting all entries in the |
||
409 | * database. Otherwise an integer with the number of total matching entries is returned. |
||
410 | * @param int $limit Limit the number of entries that are returned |
||
411 | * @param int $offset Offset when fetching the log entries, f.e. when paginating |
||
412 | * @param int $limit_days |
||
413 | * @param string $sort_by SQL order option, e.g. 'l.log_time DESC' |
||
414 | * @param string $keywords Will only return log entries that have the keywords in log_operation or log_data |
||
415 | * |
||
416 | * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high) |
||
417 | * @access private |
||
418 | */ |
||
419 | private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
420 | { |
||
421 | $count_logs = ($log_count !== false); |
||
422 | |||
423 | $log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords); |
||
424 | $log_count = $this->get_log_count(); |
||
425 | |||
426 | return $this->get_valid_offset(); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @param bool $count_logs |
||
431 | * @param int $limit |
||
432 | * @param int $offset |
||
433 | * @param int $log_time |
||
434 | * @param string $sort_by |
||
435 | * @param string $keywords |
||
436 | * |
||
437 | * @return array $log |
||
438 | * @access private |
||
439 | */ |
||
440 | private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
441 | { |
||
442 | $this->entry_count = 0; |
||
443 | $this->last_page_offset = $offset; |
||
444 | $url_ary = array(); |
||
445 | |||
446 | if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path) |
||
447 | { |
||
448 | $url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&mode=overview'); |
||
449 | $url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&mode=transactions'); |
||
450 | |||
451 | } |
||
452 | else |
||
453 | { |
||
454 | $url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile'); |
||
455 | $url_ary['txn_url'] = ''; |
||
456 | } |
||
457 | |||
458 | $get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time); |
||
459 | |||
460 | if ($count_logs) |
||
461 | { |
||
462 | $this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id'); |
||
463 | |||
464 | if ($this->entry_count == 0) |
||
465 | { |
||
466 | // Save the queries, because there are no logs to display |
||
467 | $this->last_page_offset = 0; |
||
468 | |||
469 | return array(); |
||
470 | } |
||
471 | |||
472 | // Return the user to the last page that is valid |
||
473 | while ($this->last_page_offset >= $this->entry_count) |
||
474 | { |
||
475 | $this->last_page_offset = max(0, $this->last_page_offset - $limit); |
||
476 | } |
||
477 | } |
||
478 | |||
479 | return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset); |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * @return integer |
||
484 | */ |
||
485 | public function get_log_count() |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @return integer |
||
492 | */ |
||
493 | public function get_valid_offset() |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Prepare data array() before send it to $entity |
||
500 | * |
||
501 | * @param array $transaction_data |
||
502 | * |
||
503 | * @return array |
||
504 | * @throws transaction_exception |
||
505 | */ |
||
506 | private function build_data_ary($transaction_data) |
||
507 | { |
||
508 | $errors = array(); |
||
509 | |||
510 | if ($this->request->is_set('u') && $transaction_data['MT_USERNAME'] === '') |
||
511 | { |
||
512 | $user_id = ANONYMOUS; |
||
513 | } |
||
514 | else |
||
515 | { |
||
516 | $user_ary = $this->ppde_operator->query_donor_user_data('username', $transaction_data['MT_USERNAME']); |
||
517 | |||
518 | if ($user_ary) |
||
519 | { |
||
520 | $user_id = $user_ary['user_id']; |
||
521 | } |
||
522 | else |
||
523 | { |
||
524 | $errors[] = $this->language->lang('PPDE_MT_DONOR_NOT_FOUND', $transaction_data['MT_USERNAME']); |
||
525 | } |
||
526 | } |
||
527 | |||
528 | if ($transaction_data['MT_MC_GROSS'] <= 0) |
||
529 | { |
||
530 | $errors[] = $this->language->lang('PPDE_MT_MC_GROSS_TOO_LOW'); |
||
531 | } |
||
532 | |||
533 | if ($transaction_data['MT_MC_FEE'] < 0) |
||
534 | { |
||
535 | $errors[] = $this->language->lang('PPDE_MT_MC_FEE_NEGATIVE'); |
||
536 | } |
||
537 | |||
538 | if ($transaction_data['MT_MC_FEE'] >= $transaction_data['MT_MC_GROSS']) |
||
539 | { |
||
540 | $errors[] = $this->language->lang('PPDE_MT_MC_FEE_TOO_HIGH'); |
||
541 | } |
||
542 | |||
543 | $payment_date = implode('-', [ |
||
544 | $transaction_data['MT_PAYMENT_DATE_YEAR'], |
||
545 | $transaction_data['MT_PAYMENT_DATE_MONTH'], |
||
546 | $transaction_data['MT_PAYMENT_DATE_DAY'], |
||
547 | ]); |
||
548 | |||
549 | $payment_date_timestamp_at_midnight = $this->user->get_timestamp_from_format('Y-m-d H:i:s', $payment_date . ' 00:00:00'); |
||
550 | |||
551 | if ($payment_date_timestamp_at_midnight === false) |
||
|
|||
552 | { |
||
553 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_ERROR', $payment_date); |
||
554 | } |
||
555 | |||
556 | $payment_time = $transaction_data['MT_PAYMENT_TIME']; |
||
557 | $payment_time_timestamp = strtotime($payment_time); |
||
558 | |||
559 | if ($payment_time_timestamp === false) |
||
560 | { |
||
561 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_TIME_ERROR', $payment_time); |
||
562 | } |
||
563 | |||
564 | // Normalize payment time to start from today at midnight |
||
565 | $payment_time_timestamp_from_midnight = $payment_time_timestamp - strtotime('00:00:00'); |
||
566 | |||
567 | $payment_date_time = $payment_date_timestamp_at_midnight + $payment_time_timestamp_from_midnight; |
||
568 | |||
569 | if ($payment_date_time > time()) |
||
570 | { |
||
571 | $errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_FUTURE', $this->user->format_date($payment_date_time)); |
||
572 | } |
||
573 | |||
574 | if ($errors) |
||
575 | { |
||
576 | throw (new transaction_exception())->set_errors($errors); |
||
577 | } |
||
578 | |||
579 | return array( |
||
580 | 'business' => $this->config['ppde_account_id'], |
||
581 | 'confirmed' => true, |
||
582 | 'exchange_rate' => '', |
||
583 | 'first_name' => $transaction_data['MT_FIRST_NAME'], |
||
584 | 'item_name' => '', |
||
585 | 'item_number' => implode('_', ['uid', $user_id, time()]), |
||
1 ignored issue
–
show
|
|||
586 | 'last_name' => $transaction_data['MT_LAST_NAME'], |
||
587 | 'mc_currency' => $transaction_data['MT_MC_CURRENCY'], |
||
588 | 'mc_gross' => $transaction_data['MT_MC_GROSS'], |
||
589 | 'mc_fee' => $transaction_data['MT_MC_FEE'], |
||
590 | 'net_amount' => (float) 0, // This value is calculated in core_actions:log_to_db() |
||
591 | 'parent_txn_id' => '', |
||
592 | 'payer_email' => $transaction_data['MT_PAYER_EMAIL'], |
||
593 | 'payer_id' => '', |
||
594 | 'payer_status' => '', |
||
595 | 'payment_date' => $payment_date_time, |
||
596 | 'payment_status' => 'Completed', |
||
597 | 'payment_type' => '', |
||
598 | 'memo' => $transaction_data['MT_MEMO'], |
||
599 | 'receiver_id' => '', |
||
600 | 'receiver_email' => '', |
||
601 | 'residence_country' => strtoupper($transaction_data['MT_RESIDENCE_COUNTRY']), |
||
602 | 'settle_amount' => (float) 0, |
||
603 | 'settle_currency' => '', |
||
604 | 'test_ipn' => false, |
||
605 | 'txn_errors' => '', |
||
606 | 'txn_id' => 'PPDE' . gen_rand_string(13), |
||
607 | 'txn_type' => 'ppde_manual_donation', |
||
608 | 'user_id' => $user_id, |
||
609 | ); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Set log output vars for display in the template |
||
614 | * |
||
615 | * @param array $row |
||
616 | * |
||
617 | * @return void |
||
618 | * @access protected |
||
619 | */ |
||
620 | protected function display_log_assign_template_vars($row) |
||
633 | )); |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Set output vars for display in the template |
||
638 | * |
||
639 | * @param array $data |
||
640 | * |
||
641 | * @return void |
||
642 | * @access protected |
||
643 | */ |
||
644 | protected function action_assign_template_vars($data) |
||
679 | )); |
||
680 | } |
||
682 |