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 | ); |
||
42 | |||
43 | private static $has_one = array( |
||
44 | 'PaidBy' => 'Member', |
||
45 | 'Order' => 'Order', |
||
46 | ); |
||
47 | |||
48 | private static $summary_fields = array( |
||
49 | 'Created' => 'Created', |
||
50 | 'Order.Title' => "Order", |
||
51 | 'Title' => 'Type', |
||
52 | 'AmountCurrency' => 'Amount', |
||
53 | 'Amount.Nice' => 'Amount', |
||
54 | 'Status' => 'Status', |
||
55 | ); |
||
56 | |||
57 | private static $casting = array( |
||
58 | 'Title' => 'Varchar', |
||
59 | 'AmountValue' => 'Currency', |
||
60 | 'AmountCurrency' => 'Varchar', |
||
61 | ); |
||
62 | |||
63 | private static $searchable_fields = array( |
||
64 | 'OrderID' => array( |
||
65 | 'field' => 'NumericField', |
||
66 | 'title' => 'Order Number', |
||
67 | ), |
||
68 | 'Created' => array( |
||
69 | 'title' => 'Date (e.g. today)', |
||
70 | 'field' => 'TextField', |
||
71 | 'filter' => 'EcommercePaymentFilters_AroundDateFilter', |
||
72 | ), |
||
73 | 'IP' => array( |
||
74 | 'title' => 'IP Address', |
||
75 | 'filter' => 'PartialMatchFilter', |
||
76 | ), |
||
77 | 'Status', |
||
78 | ); |
||
79 | |||
80 | /** |
||
81 | * standard SS variable. |
||
82 | * |
||
83 | * @Var String |
||
84 | */ |
||
85 | private static $singular_name = 'Shop Payment'; |
||
86 | public function i18n_singular_name() |
||
90 | |||
91 | /** |
||
92 | * standard SS variable. |
||
93 | * |
||
94 | * @Var String |
||
95 | */ |
||
96 | private static $plural_name = 'Shop Payments'; |
||
97 | public function i18n_plural_name() |
||
101 | |||
102 | |||
103 | /** |
||
104 | * CRUCIAL |
||
105 | * makes sure all the relevant payment methods are available ... |
||
106 | * |
||
107 | * @return this | EcommercePayment |
||
108 | */ |
||
109 | public function init() |
||
114 | |||
115 | /** |
||
116 | * standard SS variable. |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | private static $default_sort = '"Created" DESC'; |
||
121 | |||
122 | public function getCMSFields() |
||
130 | |||
131 | /** |
||
132 | * link to edit the record. |
||
133 | * |
||
134 | * @param string | Null $action - e.g. edit |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function CMSEditLink($action = null) |
||
142 | |||
143 | /** |
||
144 | * Standard SS method. |
||
145 | * |
||
146 | * @param Member $member |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function canCreate($member = null) |
||
165 | |||
166 | public function canView($member = null) |
||
185 | |||
186 | /** |
||
187 | * Standard SS method. |
||
188 | * |
||
189 | * @param Member $member |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function canEdit($member = null) |
||
212 | |||
213 | /** |
||
214 | * Standard SS method |
||
215 | * set to false as a security measure... |
||
216 | * |
||
217 | * @param Member $member |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function canDelete($member = null) |
||
225 | |||
226 | /** |
||
227 | * redirect to order action. |
||
228 | */ |
||
229 | public function redirectToOrder() |
||
240 | |||
241 | /** |
||
242 | * alias |
||
243 | * @return string |
||
244 | */ |
||
245 | public function Title() |
||
249 | |||
250 | /** |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getTitle() |
||
257 | |||
258 | /** |
||
259 | * alias for getAmountValue |
||
260 | * @return float |
||
261 | */ |
||
262 | public function AmountValue() |
||
266 | |||
267 | /** |
||
268 | * @return float |
||
269 | */ |
||
270 | public function getAmountValue() |
||
274 | |||
275 | /** |
||
276 | * alias for getAmountCurrency |
||
277 | * @return string |
||
278 | */ |
||
279 | public function AmountCurrency() |
||
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getAmountCurrency() |
||
291 | |||
292 | /** |
||
293 | * standard SS method |
||
294 | * try to finalise order if payment has been made. |
||
295 | */ |
||
296 | public function onBeforeWrite() |
||
301 | |||
302 | /** |
||
303 | * standard SS method |
||
304 | * try to finalise order if payment has been made. |
||
305 | */ |
||
306 | public function onAfterWrite() |
||
314 | |||
315 | /** |
||
316 | *@return string |
||
317 | **/ |
||
318 | public function Status() |
||
322 | |||
323 | /** |
||
324 | * Return the site currency in use. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public static function site_currency() |
||
337 | |||
338 | /** |
||
339 | * Set currency to default one. |
||
340 | * Set IP address. |
||
341 | */ |
||
342 | public function populateDefaults() |
||
348 | |||
349 | /** |
||
350 | * Set the IP address of the user to this payment record. |
||
351 | * This isn't perfect - IP addresses can be hidden fairly easily. |
||
352 | */ |
||
353 | protected function setClientIP() |
||
374 | |||
375 | /** |
||
376 | * Returns the Payment type currently in use. |
||
377 | * |
||
378 | * @return string | null |
||
379 | */ |
||
380 | public function PaymentMethod() |
||
387 | |||
388 | /** |
||
389 | * Static method to quickly update the payment method on runtime |
||
390 | * associative array that goes like ClassName => Description ... |
||
391 | * |
||
392 | * e.g. MyPaymentClass => Best Payment Method Ever * @param array $array - |
||
393 | * @param array $array |
||
394 | */ |
||
395 | public static function set_supported_methods($array) |
||
400 | |||
401 | /** |
||
402 | * returns the list of supported methods |
||
403 | * test methods are included if the site is in DEV mode OR |
||
404 | * the current user is a ShopAdmin. |
||
405 | * |
||
406 | * @return array |
||
407 | * [Code] => "Description", |
||
408 | * [Code] => "Description", |
||
409 | * [Code] => "Description" |
||
410 | */ |
||
411 | public static function get_supported_methods($order = null) |
||
416 | |||
417 | /** |
||
418 | * Return the form requirements for all the payment methods. |
||
419 | * |
||
420 | * @param null | Array |
||
421 | * |
||
422 | * @return An array suitable for passing to CustomRequiredFields |
||
423 | */ |
||
424 | public static function combined_form_requirements($order = null) |
||
428 | |||
429 | /** |
||
430 | * Return a set of payment fields from all enabled |
||
431 | * payment methods for this site, given the . |
||
432 | * is used to define which methods are available. |
||
433 | * |
||
434 | * @param string $amount formatted amount (e.g. 12.30) without the currency |
||
435 | * @param null | Order $order |
||
436 | * |
||
437 | * @return FieldList |
||
438 | */ |
||
439 | public static function combined_form_fields($amount, $order = null) |
||
466 | |||
467 | /** |
||
468 | * Return the payment form fields that should |
||
469 | * be shown on the checkout order form for the |
||
470 | * payment type. Example: for {@link DPSPayment}, |
||
471 | * this would be a set of fields to enter your |
||
472 | * credit card details. |
||
473 | * |
||
474 | * @return FieldList |
||
475 | */ |
||
476 | public function getPaymentFormFields() |
||
480 | |||
481 | /** |
||
482 | * Define what fields defined in {@link Order->getPaymentFormFields()} |
||
483 | * should be required. |
||
484 | * |
||
485 | * @see DPSPayment->getPaymentFormRequirements() for an example on how |
||
486 | * this is implemented. |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | public function getPaymentFormRequirements() |
||
494 | |||
495 | /** |
||
496 | * Checks if all the data for payment is correct (e.g. credit card) |
||
497 | * By default it returns true, because lots of payments gatewawys |
||
498 | * do not have any fields required here. |
||
499 | * |
||
500 | * @param array $data The form request data - see OrderForm |
||
501 | * @param OrderForm $form The form object submitted on |
||
502 | */ |
||
503 | public function validatePayment($data, $form) |
||
507 | |||
508 | /** |
||
509 | * Perform payment processing for the type of |
||
510 | * payment. For example, if this was a credit card |
||
511 | * payment type, you would perform the data send |
||
512 | * off to the payment gateway on this function for |
||
513 | * your payment subclass. |
||
514 | * |
||
515 | * This is used by {@link OrderForm} when it is |
||
516 | * submitted. |
||
517 | * |
||
518 | * @param array $data The form request data - see OrderForm |
||
519 | * @param OrderForm $form The form object submitted on |
||
520 | * |
||
521 | * @return EcommercePayment_Result |
||
522 | */ |
||
523 | public function processPayment($data, $form) |
||
527 | |||
528 | protected function handleError($e) |
||
533 | |||
534 | public function PaidObject() |
||
538 | |||
539 | /** |
||
540 | * Debug helper method. |
||
541 | * Access through : /shoppingcart/debug/. |
||
542 | */ |
||
543 | public function debug() |
||
549 | |||
550 | /** |
||
551 | * LEGACY METHOD |
||
552 | * Process payment form and return next step in the payment process. |
||
553 | * Steps taken are: |
||
554 | * 1. create new payment |
||
555 | * 2. save form into payment |
||
556 | * 3. return payment result. |
||
557 | * |
||
558 | * @param Order $order - the order that is being paid |
||
559 | * @param Form $form - the form that is being submitted |
||
560 | * @param array $data - Array of data that is submittted |
||
561 | * |
||
562 | * @return bool - if successful, this method will return TRUE |
||
563 | */ |
||
564 | public static function process_payment_form_and_return_next_step($order, $data, $form) |
||
570 | |||
571 | /** |
||
572 | * LEGACY METHOD. |
||
573 | * |
||
574 | * @param Order $order - the order that is being paid |
||
575 | * @param array $data - Array of data that is submittted |
||
576 | * @param Form $form - the form that is being submitted |
||
577 | * |
||
578 | * @return bool - true if the data is valid |
||
579 | */ |
||
580 | public static function validate_payment($order, $data, $form) |
||
586 | |||
587 | private $ecommercePaymentFormSetupAndValidationObject = null; |
||
588 | |||
589 | /** |
||
590 | * @return EcommercePaymentFormSetupAndValidation |
||
591 | */ |
||
592 | protected function ecommercePaymentFormSetupAndValidationObject() |
||
600 | |||
601 | /** |
||
602 | * @return EcommercePaymentFormSetupAndValidation |
||
603 | */ |
||
604 | public static function ecommerce_payment_form_setup_and_validation_object() |
||
608 | } |
||
609 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.