Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Gateway 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_Gateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class EE_Gateway{ |
||
| 24 | /** |
||
| 25 | * a constant used as a possible value for $_currencies_supported to indicate |
||
| 26 | * that ALL currencies are supported by this gateway |
||
| 27 | */ |
||
| 28 | const all_currencies_supported = 'all_currencies_supported'; |
||
| 29 | /** |
||
| 30 | * Where values are 3-letter currency codes |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $_currencies_supported = array(); |
||
| 34 | /** |
||
| 35 | * Whether or not this gateway can support SENDING a refund request (ie, initiated by |
||
| 36 | * admin in EE's wp-admin page) |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | protected $_supports_sending_refunds = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Whether or not this gateway can support RECEIVING a refund request from the payment |
||
| 43 | * provider (ie, initiated by admin on the payment prover's website who sends an IPN to EE) |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | protected $_supports_receiving_refunds = false; |
||
| 47 | /** |
||
| 48 | * Model for querying for existing payments |
||
| 49 | * @var EEMI_Payment |
||
| 50 | */ |
||
| 51 | protected $_pay_model; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Model used for adding to the payments log |
||
| 55 | * @var EEMI_Payment_Log |
||
| 56 | */ |
||
| 57 | protected $_pay_log; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Used for formatting some input to gateways |
||
| 61 | * @var EEHI_Template |
||
| 62 | */ |
||
| 63 | protected $_template; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Concrete class that implements EEHI_Money, used by most gateways |
||
| 67 | * @var EEHI_Money |
||
| 68 | */ |
||
| 69 | protected $_money; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Concrete class that implements EEHI_Line_Item, used for manipulating the line item tree |
||
| 73 | * @var EEHI_Line_Item |
||
| 74 | */ |
||
| 75 | protected $_line_item; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var GatewayDataFormatterInterface |
||
| 79 | */ |
||
| 80 | protected $_gateway_data_formatter; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var FormatterInterface |
||
| 84 | */ |
||
| 85 | protected $_unsupported_character_remover; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The ID of the payment method using this gateway |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | protected $_ID; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var $_debug_mode boolean whether to send requests to teh sandbox site or not |
||
| 95 | */ |
||
| 96 | protected $_debug_mode; |
||
| 97 | /** |
||
| 98 | * |
||
| 99 | * @var string $_name name to show for this payment method |
||
| 100 | */ |
||
| 101 | protected $_name; |
||
| 102 | /** |
||
| 103 | * |
||
| 104 | * @var string name to show fir this payment method to admin-type users |
||
| 105 | */ |
||
| 106 | protected $_admin_name; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return EE_Gateway |
||
|
|
|||
| 110 | */ |
||
| 111 | public function __construct(){ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * We don't want to serialize models as they often have circular structures |
||
| 116 | * (eg a payment model has a reference to each payment model object; and most |
||
| 117 | * payments have a transaction, most transactions have a payment method; |
||
| 118 | * most payment methods have a payment method type; most payment method types |
||
| 119 | * have a gateway. And if a gateway serializes its models, we start at the |
||
| 120 | * beginning again) |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function __sleep(){ |
||
| 128 | /** |
||
| 129 | * Returns whether or not this gateway should support SENDING refunds |
||
| 130 | * see $_supports_sending_refunds |
||
| 131 | * @return boolean |
||
| 132 | */ |
||
| 133 | public function supports_sending_refunds(){ |
||
| 136 | /** |
||
| 137 | * Returns whether or not this gateway should support RECEIVING refunds |
||
| 138 | * see $_supports_receiving_refunds |
||
| 139 | * @return boolean |
||
| 140 | */ |
||
| 141 | public function supports_receiving_refunds(){ |
||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Tries to refund the payment specified, taking into account the extra |
||
| 149 | * refund info. Note that if the gateway's _supports_sending_refunds is false, |
||
| 150 | * this should just throw an exception. |
||
| 151 | * @param EE_Payment $payment |
||
| 152 | * @param array $refund_info |
||
| 153 | * @return EE_Payment for the refund |
||
| 154 | * @throws EE_Error |
||
| 155 | */ |
||
| 156 | public function do_direct_refund( EE_Payment $payment, $refund_info = null ) { |
||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets the payment method's settings so the gateway knows where to send the request |
||
| 164 | * etc |
||
| 165 | * @param array $settings_array |
||
| 166 | */ |
||
| 167 | public function set_settings($settings_array){ |
||
| 173 | /** |
||
| 174 | * See this class description |
||
| 175 | * @param EEMI_Payment $payment_model |
||
| 176 | */ |
||
| 177 | public function set_payment_model($payment_model){ |
||
| 180 | /** |
||
| 181 | * See this class description |
||
| 182 | * @param EEMI_Payment_Log $payment_log_model |
||
| 183 | */ |
||
| 184 | public function set_payment_log($payment_log_model){ |
||
| 187 | |||
| 188 | /** |
||
| 189 | * See this class description |
||
| 190 | * @param EEHI_Template $template_helper |
||
| 191 | */ |
||
| 192 | public function set_template_helper($template_helper){ |
||
| 195 | |||
| 196 | /** |
||
| 197 | * See this class description |
||
| 198 | * @param EEHI_Line_Item $line_item_helper |
||
| 199 | */ |
||
| 200 | public function set_line_item_helper( $line_item_helper ){ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * See this class description |
||
| 206 | * @param EEHI_Money $money_helper |
||
| 207 | */ |
||
| 208 | public function set_money_helper( $money_helper ){ |
||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Sets the gateway data formatter helper |
||
| 216 | * @param GatewayDataFormatterInterface $gateway_data_formatter |
||
| 217 | * @throws InvalidEntityException if it's not set properly |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function set_gateway_data_formatter( GatewayDataFormatterInterface $gateway_data_formatter){ |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Gets the gateway data formatter |
||
| 233 | * @return GatewayDataFormatterInterface |
||
| 234 | * @throws InvalidEntityException if it's not set properly |
||
| 235 | */ |
||
| 236 | View Code Duplication | protected function _get_gateway_formatter(){ |
|
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Sets the helper which will remove unsupported characters for most gateways |
||
| 252 | * @param FormatterInterface $formatter |
||
| 253 | * @return FormatterInterface |
||
| 254 | * @throws InvalidEntityException |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function set_unsupported_character_remover( FormatterInterface $formatter){ |
|
| 267 | /** |
||
| 268 | * Gets the helper which removes characters which gateways might not support, like emojis etc. |
||
| 269 | * @return FormatterInterface |
||
| 270 | * @throws InvalidEntityException |
||
| 271 | */ |
||
| 272 | View Code Duplication | protected function _get_unsupported_character_remover(){ |
|
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @param $message |
||
| 287 | * @param $payment |
||
| 288 | */ |
||
| 289 | public function log($message,$payment){ |
||
| 299 | /** |
||
| 300 | * Formats the amount so it can generally be sent to gateways |
||
| 301 | * @param float $amount |
||
| 302 | * @return string |
||
| 303 | * @deprecated since 4.9.31 insetad use EventEspresso\core\services\payment_methods\gateways\GatewayDataFormatter::format_currency() |
||
| 304 | */ |
||
| 305 | public function format_currency($amount){ |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns either an array of all the currency codes supported, |
||
| 311 | * or a string indicating they're all supported (EE_gateway::all_currencies_supported) |
||
| 312 | * @return mixed array or string |
||
| 313 | */ |
||
| 314 | public function currencies_supported(){ |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns what a simple summing of items and taxes for this transaction. This |
||
| 320 | * can be used to determine if some more complex line items, like promotions, |
||
| 321 | * surcharges, or cancellations occurred (in which case we might want to forget |
||
| 322 | * about creating an itemized list of purchases and instead only send the total due) |
||
| 323 | * @param EE_Transaction $transaction |
||
| 324 | * @return float |
||
| 325 | */ |
||
| 326 | protected function _sum_items_and_taxes( EE_Transaction $transaction){ |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Determines whether or not we can easily itemize the transaction using only |
||
| 340 | * items and taxes (ie, no promotions or surcharges or cancellations needed) |
||
| 341 | * @param EEI_Payment $payment |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | protected function _can_easily_itemize_transaction_for( EEI_Payment $payment ){ |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Handles updating the transaction and any other related data based on the payment. |
||
| 355 | * You may be tempted to do this as part of do_direct_payment or handle_payment_update, |
||
| 356 | * but doing so on those functions might be too early. It's possible that the changes |
||
| 357 | * you make to teh transaction or registration or line items may just get overwritten |
||
| 358 | * at that point. Instead, you should store any info you need on the payment during those |
||
| 359 | * functions, and use that information at this step, which client code will decide |
||
| 360 | * for you when it should be called. |
||
| 361 | * @param EE_Payment $payment |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | public function update_txn_based_on_payment( $payment ){ |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets the first event for this payment (it's possible that it could be for multiple) |
||
| 371 | * @param EEI_Payment $payment |
||
| 372 | * @return EEI_Event|null |
||
| 373 | * @deprecated since 4.9.31 instead use EEI_Payment::get_first_event() |
||
| 374 | */ |
||
| 375 | protected function _get_first_event_for_payment( EEI_Payment $payment ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Gets the name of the first event for which is being paid |
||
| 381 | * @param EEI_Payment $payment |
||
| 382 | * @return string |
||
| 383 | * @deprecated since 4.9.31 instead use EEI_Payment::get_first_event_name() |
||
| 384 | */ |
||
| 385 | protected function _get_first_event_name_for_payment( EEI_Payment $payment ) { |
||
| 388 | /** |
||
| 389 | * Gets the text to use for a gateway's line item name when this is a partial payment |
||
| 390 | * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatPartialPaymentLineItemName($payment) |
||
| 391 | * @param EE_Payment $payment |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function _format_partial_payment_line_item_name( EEI_Payment $payment ){ |
||
| 397 | /** |
||
| 398 | * Gets the text to use for a gateway's line item description when this is a partial payment |
||
| 399 | * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatPartialPaymentLineItemDesc() |
||
| 400 | * @param EEI_Payment $payment |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function _format_partial_payment_line_item_desc( EEI_Payment $payment ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Gets the name to use for a line item when sending line items to the gateway |
||
| 409 | * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatLineItemName($line_item,$payment) |
||
| 410 | * @param EEI_Line_Item $line_item |
||
| 411 | * @param EEI_Payment $payment |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | protected function _format_line_item_name( EEI_Line_Item $line_item, EEI_Payment $payment ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Gets the description to use for a line item when sending line items to the gateway |
||
| 420 | * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatLineItemDesc($line_item, $payment)) |
||
| 421 | * @param EEI_Line_Item $line_item |
||
| 422 | * @param EEI_Payment $payment |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | protected function _format_line_item_desc( EEI_Line_Item $line_item, EEI_Payment $payment ) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Gets the order description that should generlly be sent to gateways |
||
| 431 | * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatOrderDescription($payment) |
||
| 432 | * @param EEI_Payment $payment |
||
| 433 | * @return type |
||
| 434 | */ |
||
| 435 | protected function _format_order_description( EEI_Payment $payment ) { |
||
| 438 | } |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.