Complex classes like EE_PMT_Base 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 EE_PMT_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class EE_PMT_Base |
||
| 29 | { |
||
| 30 | |||
| 31 | const onsite = 'on-site'; |
||
| 32 | const offsite = 'off-site'; |
||
| 33 | const offline = 'off-line'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var EE_Payment_Method |
||
| 37 | */ |
||
| 38 | protected $_pm_instance = NULL; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var boolean |
||
| 42 | */ |
||
| 43 | protected $_requires_https = FALSE; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var boolean |
||
| 47 | */ |
||
| 48 | protected $_has_billing_form; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var EE_Gateway |
||
| 52 | */ |
||
| 53 | protected $_gateway = NULL; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var EE_Payment_Method_Form |
||
| 57 | */ |
||
| 58 | protected $_settings_form = NULL; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var EE_Form_Section_Proper |
||
| 62 | */ |
||
| 63 | protected $_billing_form = NULL; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var boolean |
||
| 67 | */ |
||
| 68 | protected $_cache_billing_form = TRUE; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * String of the absolute path to the folder containing this file, with a trailing slash. |
||
| 72 | * eg '/public_html/wp-site/wp-content/plugins/event-espresso/payment_methods/Invoice/' |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $_file_folder = NULL; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * String to the absolute URL to this file (useful for getting its web-accessible resources |
||
| 79 | * like images, js, or css) |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $_file_url = NULL; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Pretty name for the payment method |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $_pretty_name = NULL; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $_default_button_url = NULL; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $_default_description = NULL; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var MoneyFactory |
||
| 104 | */ |
||
| 105 | protected $money_factory; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var CurrencyFactory |
||
| 109 | */ |
||
| 110 | protected $currency_factory; |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | * @param EE_Payment_Method $pm_instance |
||
| 116 | * @param MoneyFactory|null $money_factory |
||
| 117 | * @param CurrencyFactory $currency_factory |
||
| 118 | * @throws InvalidArgumentException |
||
| 119 | * @throws InvalidInterfaceException |
||
| 120 | * @throws InvalidDataTypeException |
||
| 121 | * @throws InvalidEntityException |
||
| 122 | * @throws EE_Error |
||
| 123 | */ |
||
| 124 | public function __construct( |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @param boolean $has_billing_form |
||
| 171 | */ |
||
| 172 | public function set_has_billing_form($has_billing_form) |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * sets the file_folder property |
||
| 180 | * @throws ReflectionException |
||
| 181 | */ |
||
| 182 | protected function _set_file_folder() |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * sets the file URL with a trailing slash for this PMT |
||
| 192 | */ |
||
| 193 | protected function _set_file_url() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Gets the default description on all payment methods of this type |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function default_description() |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the folder containing the PMT child class, with a trailing slash |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function file_folder() |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function file_url() |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Sets the payment method instance this payment method type is for. |
||
| 238 | * Its important teh payment method instance is set before |
||
| 239 | * @param EE_Payment_Method $payment_method_instance |
||
| 240 | */ |
||
| 241 | function set_instance($payment_method_instance) |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Gets teh form for displaying to admins where they setup the payment method |
||
| 257 | * @return EE_Payment_Method_Form |
||
| 258 | * @throws EE_Error |
||
| 259 | */ |
||
| 260 | function settings_form() |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Gets the form for all the settings related to this payment method type |
||
| 277 | * @return EE_Payment_Method_Form |
||
| 278 | */ |
||
| 279 | abstract function generate_new_settings_form(); |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Sets the form for settings. This may be useful if we have already received |
||
| 284 | * a form submission and have form data it in, and want to use it anytime we're showing |
||
| 285 | * this payment method type's settings form later in the request |
||
| 286 | * @param EE_Payment_Method_Form $form |
||
| 287 | */ |
||
| 288 | public function set_settings_form($form) |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * @return boolean |
||
| 296 | */ |
||
| 297 | public function has_billing_form() |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets the form for displaying to attendees where they can enter their billing info |
||
| 305 | * which will be sent to teh gateway (can be null) |
||
| 306 | * |
||
| 307 | * @param EE_Transaction $transaction |
||
| 308 | * @param array $extra_args |
||
| 309 | * @return EE_Billing_Attendee_Info_Form|EE_Billing_Info_Form|null |
||
| 310 | * @throws EE_Error |
||
| 311 | */ |
||
| 312 | public function billing_form(EE_Transaction $transaction = NULL, $extra_args = array()) |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Creates the billing form for this payment method type |
||
| 341 | * @param EE_Transaction $transaction |
||
| 342 | * @return EE_Billing_Info_Form |
||
| 343 | */ |
||
| 344 | abstract function generate_new_billing_form(EE_Transaction $transaction = NULL); |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * apply_billing_form_debug_settings |
||
| 349 | * applies debug data to the form |
||
| 350 | * |
||
| 351 | * @param EE_Billing_Info_Form $billing_form |
||
| 352 | * @return EE_Billing_Info_Form |
||
| 353 | */ |
||
| 354 | public function apply_billing_form_debug_settings(EE_Billing_Info_Form $billing_form) |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Sets the billing form for this payment method type. You may want to use this |
||
| 362 | * if you have form |
||
| 363 | * @param EE_Payment_Method $form |
||
| 364 | */ |
||
| 365 | public function set_billing_form($form) |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns whether or not this payment method requires HTTPS to be used |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | function requires_https() |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * |
||
| 383 | * @param EE_Transaction $transaction |
||
| 384 | * @param float $amount |
||
| 385 | * @param EE_Billing_Info_Form $billing_info |
||
| 386 | * @param string $return_url |
||
| 387 | * @param string $fail_url |
||
| 388 | * @param string $method |
||
| 389 | * @param bool $by_admin |
||
| 390 | * @return EE_Payment |
||
| 391 | * @throws InvalidArgumentException |
||
| 392 | * @throws InvalidInterfaceException |
||
| 393 | * @throws InvalidDataTypeException |
||
| 394 | * @throws EE_Error |
||
| 395 | */ |
||
| 396 | function process_payment(EE_Transaction $transaction, $amount = null, $billing_info = null, $return_url = null, $fail_url = '', $method = 'CART', $by_admin = false) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Gets the values we want to pass onto the gateway. Normally these |
||
| 489 | * are just the 'pretty' values, but there may be times the data may need |
||
| 490 | * a little massaging. Proper subsections will become arrays of inputs |
||
| 491 | * @param EE_Billing_Info_Form $billing_form |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | protected function _get_billing_values_from_form($billing_form) |
||
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * Handles an instant payment notification when the transaction is known (by default). |
||
| 506 | * @param array $req_data |
||
| 507 | * @param EE_Transaction $transaction |
||
| 508 | * @return EE_Payment |
||
| 509 | * @throws InvalidArgumentException |
||
| 510 | * @throws InvalidInterfaceException |
||
| 511 | * @throws InvalidDataTypeException |
||
| 512 | * @throws EE_Error |
||
| 513 | */ |
||
| 514 | public function handle_ipn($req_data, $transaction) |
||
| 524 | |||
| 525 | |||
| 526 | /** |
||
| 527 | * Saves the billing info onto the attendee of the primary registrant on this transaction, and |
||
| 528 | * cleans it first. |
||
| 529 | * @param EE_Billing_Attendee_Info_Form $billing_form |
||
| 530 | * @param EE_Transaction $transaction |
||
| 531 | * @return boolean success |
||
| 532 | * @throws EE_Error |
||
| 533 | */ |
||
| 534 | protected function _save_billing_info_to_attendee($billing_form, $transaction) |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * Gets the payment this IPN is for. Children may often want to |
||
| 557 | * override this to inspect the request |
||
| 558 | * @param EE_Transaction $transaction |
||
| 559 | * @param array $req_data |
||
| 560 | * @return EE_Payment |
||
| 561 | * @throws EE_Error |
||
| 562 | */ |
||
| 563 | protected function find_payment_for_ipn(EE_Transaction $transaction, $req_data = array()) |
||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * In case generic code cannot provide the payment processor with a specific payment method |
||
| 571 | * and transaction, it will try calling this method on each activate payment method. |
||
| 572 | * If the payment method is able to identify the request as being for it, it should fetch |
||
| 573 | * the payment its for and return it. If not, it should throw an EE_Error to indicate it cannot |
||
| 574 | * handle the IPN |
||
| 575 | * @param array $req_data |
||
| 576 | * @return EE_Payment only if this payment method can find the info its needs from $req_data |
||
| 577 | * and identifies the IPN as being for this payment method (not just fo ra payment method of this type) |
||
| 578 | * @throws EE_Error |
||
| 579 | */ |
||
| 580 | public function handle_unclaimed_ipn($req_data = array()) |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Logic to be accomplished when the payment attempt is complete. |
||
| 588 | * Most payment methods don't need to do anything at this point; but some, like Mijireh, do. |
||
| 589 | * (Mijireh is an offsite gateway which doesn't send an IPN. So when the user returns to EE from |
||
| 590 | * mijireh, this method needs to be called so the Mijireh PM can ping Mijireh to know the status |
||
| 591 | * of the payment). Fed a transaction because it's always assumed to be the last payment that |
||
| 592 | * we're dealing with. Returns that last payment (if there is one) |
||
| 593 | * |
||
| 594 | * @param EE_Transaction $transaction |
||
| 595 | * @return EE_Payment |
||
| 596 | * @throws EE_Error |
||
| 597 | */ |
||
| 598 | public function finalize_payment_for($transaction) |
||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * Whether or not this payment method's gateway supports sending refund requests |
||
| 606 | * @return boolean |
||
| 607 | */ |
||
| 608 | public function supports_sending_refunds() |
||
| 616 | |||
| 617 | |||
| 618 | /** |
||
| 619 | * |
||
| 620 | * @param EE_Payment $payment |
||
| 621 | * @param array $refund_info |
||
| 622 | * @throws EE_Error |
||
| 623 | * @return EE_Payment |
||
| 624 | */ |
||
| 625 | public function process_refund(EE_Payment $payment, $refund_info = array()) |
||
| 638 | |||
| 639 | |||
| 640 | /** |
||
| 641 | * Returns one the class's constants onsite,offsite, or offline, depending on this |
||
| 642 | * payment method's gateway. |
||
| 643 | * @return string |
||
| 644 | * @throws EE_Error |
||
| 645 | */ |
||
| 646 | public function payment_occurs() |
||
| 658 | |||
| 659 | |||
| 660 | /** |
||
| 661 | * For adding any html output ab ove the payment overview. |
||
| 662 | * Many gateways won't want ot display anything, so this function just returns an empty string. |
||
| 663 | * Other gateways may want to override this, such as offline gateways. |
||
| 664 | * @param EE_Payment $payment |
||
| 665 | * @return string |
||
| 666 | * @throws \DomainException |
||
| 667 | */ |
||
| 668 | public function payment_overview_content(EE_Payment $payment) |
||
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * @return array where keys are the help tab name, |
||
| 676 | * values are: array { |
||
| 677 | * @type string $title i18n name for the help tab |
||
| 678 | * @type string $filename name of the file located in ./help_tabs/ (ie, in a folder next to this file) |
||
| 679 | * @type array $template_args any arguments you want passed to the template file while rendering. |
||
| 680 | * Keys will be variable names and values with be their values. |
||
| 681 | */ |
||
| 682 | public function help_tabs_config() |
||
| 686 | |||
| 687 | |||
| 688 | /** |
||
| 689 | * The system name for this PMT (eg AIM, Paypal_Pro, Invoice... what gets put into |
||
| 690 | * the payment method's table's PMT_type column) |
||
| 691 | * @return string |
||
| 692 | */ |
||
| 693 | public function system_name() |
||
| 698 | |||
| 699 | |||
| 700 | /** |
||
| 701 | * A pretty i18n version of the PMT name |
||
| 702 | * @return string |
||
| 703 | */ |
||
| 704 | public function pretty_name() |
||
| 708 | |||
| 709 | |||
| 710 | /** |
||
| 711 | * Gets the default absolute URL to the payment method type's button |
||
| 712 | * @return string |
||
| 713 | */ |
||
| 714 | public function default_button_url() |
||
| 718 | |||
| 719 | |||
| 720 | /** |
||
| 721 | * Gets the gateway used by this payment method (if any) |
||
| 722 | * @return EE_Gateway |
||
| 723 | */ |
||
| 724 | public function get_gateway() |
||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * @return string html for the link to a help tab |
||
| 732 | */ |
||
| 733 | public function get_help_tab_link() |
||
| 737 | |||
| 738 | |||
| 739 | /** |
||
| 740 | * Returns the name of the help tab for this PMT |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | public function get_help_tab_name() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * The name of the wp capability that should be associated with the usage of |
||
| 750 | * this PMT by an admin |
||
| 751 | * @return string |
||
| 752 | */ |
||
| 753 | public function cap_name() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Called by client code to tell the gateway that if it wants to change |
||
| 760 | * the transaction or line items or registrations related to teh payment it already |
||
| 761 | * processed (we think, but possibly not) that now's the time to do it. |
||
| 762 | * It is expected that gateways will store any info they need for this on the PAY_details, |
||
| 763 | * or maybe an extra meta value |
||
| 764 | * @param EE_Payment $payment |
||
| 765 | * @return void |
||
| 766 | */ |
||
| 767 | public function update_txn_based_on_payment($payment) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Returns a string of HTML describing this payment method type for an admin, |
||
| 776 | * primarily intended for them to read before activating it. |
||
| 777 | * The easiest way to set this is to create a folder 'templates' alongside |
||
| 778 | * your EE_PMT_{System_Name} file, and in it create a file named "{system_name}_intro.template.php". |
||
| 779 | * Eg, if your payment method file is named "EE_PMT_Foo_Bar.pm.php", |
||
| 780 | * then you'd create a file named "templates" in the same folder as it, and name the file |
||
| 781 | * "foo_bar_intro.template.php", and its content will be returned by this method |
||
| 782 | * @return string |
||
| 783 | */ |
||
| 784 | public function introductory_html() |
||
| 788 | |||
| 789 | |||
| 790 | } |
||
| 791 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: