Complex classes like EcommercePayment 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 EcommercePayment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class EcommercePayment extends DataObject implements EditableEcommerceObject |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * standard SS Variable. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private static $dependencies = array( |
||
| 18 | 'supportedMethodsProvider' => '%$EcommercePaymentSupportedMethodsProvider', |
||
| 19 | ); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * automatically populated by the dependency manager. |
||
| 23 | * |
||
| 24 | * @var EcommercePaymentSupportedMethodsProvider |
||
| 25 | */ |
||
| 26 | public $supportedMethodsProvider = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Incomplete (default): Payment created but nothing confirmed as successful |
||
| 30 | * Success: Payment successful |
||
| 31 | * Failure: Payment failed during process |
||
| 32 | * Pending: Payment awaiting receipt/bank transfer etc. |
||
| 33 | */ |
||
| 34 | private static $db = array( |
||
|
|
|||
| 35 | 'Status' => "Enum('Incomplete,Success,Failure,Pending','Incomplete')", |
||
| 36 | 'Amount' => 'Money', |
||
| 37 | 'Message' => 'Text', |
||
| 38 | 'IP' => 'Varchar(45)', /* for IPv6 you have to make sure you have up to 45 characters */ |
||
| 39 | 'ProxyIP' => 'Varchar(45)', |
||
| 40 | 'ExceptionError' => 'Text', |
||
| 41 | 'AlternativeEndPoint' => 'Varchar(255)' |
||
| 42 | ); |
||
| 43 | |||
| 44 | private static $has_one = array( |
||
| 45 | 'PaidBy' => 'Member', |
||
| 46 | 'Order' => 'Order', |
||
| 47 | ); |
||
| 48 | |||
| 49 | private static $summary_fields = array( |
||
| 50 | 'Created' => 'Created', |
||
| 51 | 'Order.Title' => "Order", |
||
| 52 | 'Title' => 'Type', |
||
| 53 | 'AmountCurrency' => 'Amount', |
||
| 54 | 'Amount.Nice' => 'Amount', |
||
| 55 | 'Status' => 'Status', |
||
| 56 | ); |
||
| 57 | |||
| 58 | private static $casting = array( |
||
| 59 | 'Title' => 'Varchar', |
||
| 60 | 'AmountValue' => 'Currency', |
||
| 61 | 'AmountCurrency' => 'Varchar', |
||
| 62 | ); |
||
| 63 | |||
| 64 | private static $searchable_fields = array( |
||
| 65 | 'OrderID' => array( |
||
| 66 | 'field' => 'NumericField', |
||
| 67 | 'title' => 'Order Number', |
||
| 68 | ), |
||
| 69 | 'Created' => array( |
||
| 70 | 'title' => 'Date (e.g. today)', |
||
| 71 | 'field' => 'TextField', |
||
| 72 | 'filter' => 'EcommercePaymentFilters_AroundDateFilter', |
||
| 73 | ), |
||
| 74 | 'IP' => array( |
||
| 75 | 'title' => 'IP Address', |
||
| 76 | 'filter' => 'PartialMatchFilter', |
||
| 77 | ), |
||
| 78 | 'Status', |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * standard SS variable. |
||
| 83 | * |
||
| 84 | * @Var String |
||
| 85 | */ |
||
| 86 | private static $singular_name = 'Shop Payment'; |
||
| 87 | public function i18n_singular_name() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * standard SS variable. |
||
| 94 | * |
||
| 95 | * @Var String |
||
| 96 | */ |
||
| 97 | private static $plural_name = 'Shop Payments'; |
||
| 98 | public function i18n_plural_name() |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * CRUCIAL |
||
| 106 | * makes sure all the relevant payment methods are available ... |
||
| 107 | * |
||
| 108 | * @return this | EcommercePayment |
||
| 109 | */ |
||
| 110 | public function init() |
||
| 115 | |||
| 116 | private static $indexes = array( |
||
| 117 | 'Status' => true, |
||
| 118 | 'LastEdited' => true |
||
| 119 | ); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * standard SS variable. |
||
| 123 | * |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private static $default_sort = [ |
||
| 127 | 'LastEdited' => 'DESC', |
||
| 128 | 'ID' => 'DESC' |
||
| 129 | ]; |
||
| 130 | |||
| 131 | public function getCMSFields() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * link to edit the record. |
||
| 143 | * |
||
| 144 | * @param string | Null $action - e.g. edit |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function CMSEditLink($action = null) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Standard SS method. |
||
| 155 | * |
||
| 156 | * @param Member $member |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function canCreate($member = null) |
||
| 175 | |||
| 176 | public function canView($member = null) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Standard SS method. |
||
| 198 | * |
||
| 199 | * @param Member $member |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function canEdit($member = null) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Standard SS method |
||
| 225 | * set to false as a security measure... |
||
| 226 | * |
||
| 227 | * @param Member $member |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function canDelete($member = null) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * redirects to this link after order has been placed .... |
||
| 238 | * @param string $link |
||
| 239 | */ |
||
| 240 | public function addAlternativeEndPoint($link, $write = true) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * redirect to order action. |
||
| 250 | */ |
||
| 251 | public function redirectToOrder() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * alias |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function Title() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getTitle() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * alias for getAmountValue |
||
| 283 | * @return float |
||
| 284 | */ |
||
| 285 | public function AmountValue() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return float |
||
| 292 | */ |
||
| 293 | public function getAmountValue() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * alias for getAmountCurrency |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function AmountCurrency() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getAmountCurrency() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * standard SS method |
||
| 317 | * try to finalise order if payment has been made. |
||
| 318 | */ |
||
| 319 | public function onBeforeWrite() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * standard SS method |
||
| 327 | * try to finalise order if payment has been made. |
||
| 328 | */ |
||
| 329 | public function onAfterWrite() |
||
| 337 | |||
| 338 | /** |
||
| 339 | *@return string |
||
| 340 | **/ |
||
| 341 | public function Status() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Return the site currency in use. |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public static function site_currency() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set currency to default one. |
||
| 363 | * Set IP address. |
||
| 364 | */ |
||
| 365 | public function populateDefaults() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set the IP address of the user to this payment record. |
||
| 374 | * This isn't perfect - IP addresses can be hidden fairly easily. |
||
| 375 | */ |
||
| 376 | protected function setClientIP() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns the Payment type currently in use. |
||
| 400 | * |
||
| 401 | * @return string | null |
||
| 402 | */ |
||
| 403 | public function PaymentMethod() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Static method to quickly update the payment method on runtime |
||
| 413 | * associative array that goes like ClassName => Description ... |
||
| 414 | * |
||
| 415 | * e.g. MyPaymentClass => Best Payment Method Ever * @param array $array - |
||
| 416 | * @param array $array |
||
| 417 | */ |
||
| 418 | public static function set_supported_methods($array) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * returns the list of supported methods |
||
| 426 | * test methods are included if the site is in DEV mode OR |
||
| 427 | * the current user is a ShopAdmin. |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | * [Code] => "Description", |
||
| 431 | * [Code] => "Description", |
||
| 432 | * [Code] => "Description" |
||
| 433 | */ |
||
| 434 | public static function get_supported_methods($order = null) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Return the form requirements for all the payment methods. |
||
| 442 | * |
||
| 443 | * @param null | Array |
||
| 444 | * |
||
| 445 | * @return An array suitable for passing to CustomRequiredFields |
||
| 446 | */ |
||
| 447 | public static function combined_form_requirements($order = null) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Return a set of payment fields from all enabled |
||
| 454 | * payment methods for this site, given the . |
||
| 455 | * is used to define which methods are available. |
||
| 456 | * |
||
| 457 | * @param string $amount formatted amount (e.g. 12.30) without the currency |
||
| 458 | * @param null | Order $order |
||
| 459 | * |
||
| 460 | * @return FieldList |
||
| 461 | */ |
||
| 462 | public static function combined_form_fields($amount, $order = null) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Return the payment form fields that should |
||
| 492 | * be shown on the checkout order form for the |
||
| 493 | * payment type. Example: for {@link DPSPayment}, |
||
| 494 | * this would be a set of fields to enter your |
||
| 495 | * credit card details. |
||
| 496 | * |
||
| 497 | * @return FieldList |
||
| 498 | */ |
||
| 499 | public function getPaymentFormFields() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Define what fields defined in {@link Order->getPaymentFormFields()} |
||
| 506 | * should be required. |
||
| 507 | * |
||
| 508 | * @see DPSPayment->getPaymentFormRequirements() for an example on how |
||
| 509 | * this is implemented. |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | public function getPaymentFormRequirements() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Checks if all the data for payment is correct (e.g. credit card) |
||
| 520 | * By default it returns true, because lots of payments gatewawys |
||
| 521 | * do not have any fields required here. |
||
| 522 | * |
||
| 523 | * @param array $data The form request data - see OrderForm |
||
| 524 | * @param OrderForm $form The form object submitted on |
||
| 525 | */ |
||
| 526 | public function validatePayment($data, $form) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Perform payment processing for the type of |
||
| 533 | * payment. For example, if this was a credit card |
||
| 534 | * payment type, you would perform the data send |
||
| 535 | * off to the payment gateway on this function for |
||
| 536 | * your payment subclass. |
||
| 537 | * |
||
| 538 | * This is used by {@link OrderForm} when it is |
||
| 539 | * submitted. |
||
| 540 | * |
||
| 541 | * @param array $data The form request data - see OrderForm |
||
| 542 | * @param OrderForm $form The form object submitted on |
||
| 543 | * |
||
| 544 | * @return EcommercePayment_Result |
||
| 545 | */ |
||
| 546 | public function processPayment($data, $form) |
||
| 550 | |||
| 551 | protected function handleError($e) |
||
| 556 | |||
| 557 | public function PaidObject() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Debug helper method. |
||
| 564 | * Access through : /shoppingcart/debug/. |
||
| 565 | */ |
||
| 566 | public function debug() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * LEGACY METHOD |
||
| 575 | * Process payment form and return next step in the payment process. |
||
| 576 | * Steps taken are: |
||
| 577 | * 1. create new payment |
||
| 578 | * 2. save form into payment |
||
| 579 | * 3. return payment result. |
||
| 580 | * |
||
| 581 | * @param Order $order - the order that is being paid |
||
| 582 | * @param Form $form - the form that is being submitted |
||
| 583 | * @param array $data - Array of data that is submittted |
||
| 584 | * |
||
| 585 | * @return bool - if successful, this method will return TRUE |
||
| 586 | */ |
||
| 587 | public static function process_payment_form_and_return_next_step($order, $data, $form) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * LEGACY METHOD. |
||
| 596 | * |
||
| 597 | * @param Order $order - the order that is being paid |
||
| 598 | * @param array $data - Array of data that is submittted |
||
| 599 | * @param Form $form - the form that is being submitted |
||
| 600 | * |
||
| 601 | * @return bool - true if the data is valid |
||
| 602 | */ |
||
| 603 | public static function validate_payment($order, $data, $form) |
||
| 609 | |||
| 610 | private $ecommercePaymentFormSetupAndValidationObject = null; |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @return EcommercePaymentFormSetupAndValidation |
||
| 614 | */ |
||
| 615 | protected function ecommercePaymentFormSetupAndValidationObject() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @return EcommercePaymentFormSetupAndValidation |
||
| 626 | */ |
||
| 627 | public static function ecommerce_payment_form_setup_and_validation_object() |
||
| 631 | } |
||
| 632 |