Complex classes like EcommerceCurrency 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 EcommerceCurrency, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class EcommerceCurrency extends DataObject implements EditableEcommerceObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * standard SS variable. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private static $db = array( |
||
|
|
|||
| 19 | 'Code' => 'Varchar(3)', |
||
| 20 | 'Name' => 'Varchar(100)', |
||
| 21 | 'InUse' => 'Boolean', |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * standard SS variable. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $indexes = array( |
||
| 30 | 'Code' => true, |
||
| 31 | 'InUse' => true, |
||
| 32 | 'Name' => true |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * standard SS variable. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private static $casting = array( |
||
| 41 | 'IsDefault' => 'Boolean', |
||
| 42 | 'IsDefaultNice' => 'Varchar', |
||
| 43 | 'InUseNice' => 'Varchar', |
||
| 44 | 'ExchangeRate' => 'Double', |
||
| 45 | 'DefaultSymbol' => 'Varchar', |
||
| 46 | 'ShortSymbol' => 'Varchar', |
||
| 47 | 'LongSymbol' => 'Varchar', |
||
| 48 | ); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * standard SS variable. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private static $searchable_fields = array( |
||
| 56 | 'Code' => 'PartialMatchFilter', |
||
| 57 | 'Name' => 'PartialMatchFilter', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * standard SS variable. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private static $field_labels = array( |
||
| 66 | 'Code' => 'Short Code', |
||
| 67 | 'Name' => 'Name', |
||
| 68 | 'InUse' => 'It is available for use?', |
||
| 69 | 'ExchangeRate' => 'Exchange Rate', |
||
| 70 | 'ExchangeRateExplanation' => 'Exchange Rate explanation', |
||
| 71 | 'IsDefaultNice' => 'Is default currency for site', |
||
| 72 | 'DefaultSymbol' => 'Default symbol', |
||
| 73 | 'ShortSymbol' => 'Short symbol', |
||
| 74 | 'LongSymbol' => 'Long symbol', |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * standard SS variable. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private static $summary_fields = array( |
||
| 83 | 'Code' => 'Code', |
||
| 84 | 'Name' => 'Name', |
||
| 85 | 'InUseNice' => 'Available', |
||
| 86 | 'IsDefaultNice' => 'Default Currency', |
||
| 87 | 'ExchangeRate' => 'Exchange Rate', |
||
| 88 | ); //note no => for relational fields |
||
| 89 | |||
| 90 | /** |
||
| 91 | * standard SS variable. |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private static $singular_name = 'Currency'; |
||
| 96 | public function i18n_singular_name() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * standard SS variable. |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private static $plural_name = 'Currencies'; |
||
| 107 | public function i18n_plural_name() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * standard SS variable. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private static $default_sort = [ |
||
| 118 | 'InUse' => 'DESC', |
||
| 119 | 'Name' => 'ASC', |
||
| 120 | 'Code' => 'ASC', |
||
| 121 | 'ID' => 'DESC' |
||
| 122 | ]; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * standard SS variable. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private static $defaults = array( |
||
| 130 | 'InUse' => true |
||
| 131 | ); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Standard SS Method. |
||
| 135 | * |
||
| 136 | * @param Member $member |
||
| 137 | * |
||
| 138 | * @var bool |
||
| 139 | */ |
||
| 140 | public function canCreate($member = null) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Standard SS Method. |
||
| 158 | * |
||
| 159 | * @param Member $member |
||
| 160 | * |
||
| 161 | * @var bool |
||
| 162 | */ |
||
| 163 | public function canView($member = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Standard SS Method. |
||
| 181 | * |
||
| 182 | * @param Member $member |
||
| 183 | * |
||
| 184 | * @var bool |
||
| 185 | */ |
||
| 186 | public function canEdit($member = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Standard SS method. |
||
| 204 | * |
||
| 205 | * @param Member $member |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function canDelete($member = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * NOTE: when there is only one currency we return an empty DataList |
||
| 231 | * as one currency is meaningless. |
||
| 232 | * |
||
| 233 | * @return DataList | null |
||
| 234 | */ |
||
| 235 | public static function ecommerce_currency_list() |
||
| 252 | |||
| 253 | public static function get_list() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param float | Currency $price |
||
| 268 | * @param Order $order |
||
| 269 | * |
||
| 270 | * @return Money |
||
| 271 | */ |
||
| 272 | public static function get_money_object_from_order_currency($price, Order $order = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * returns the default currency. |
||
| 307 | */ |
||
| 308 | public static function default_currency() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * returns the default currency as Code. |
||
| 321 | * |
||
| 322 | * @return string - e.g. NZD |
||
| 323 | */ |
||
| 324 | public static function default_currency_code() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | public static function default_currency_id() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Only returns a currency when it is a valid currency. |
||
| 352 | * |
||
| 353 | * @param string $currencyCode - the code of the currency, e.g. nzd |
||
| 354 | * |
||
| 355 | * @return EcommerceCurrency | Null |
||
| 356 | */ |
||
| 357 | public static function get_one_from_code($currencyCode) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * STANDARD SILVERSTRIPE STUFF. |
||
| 370 | **/ |
||
| 371 | public function getCMSFields() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * link to edit the record. |
||
| 396 | * |
||
| 397 | * @param string | Null $action - e.g. edit |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function CMSEditLink($action = null) |
||
| 405 | |||
| 406 | public function DefaultSymbol() |
||
| 414 | |||
| 415 | public function ShortSymbol() |
||
| 423 | |||
| 424 | public function LongSymbol() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * casted variable method. |
||
| 435 | * |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function IsDefault() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * casted variable method. |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function IsDefaultNice() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * casted variable method. |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function InUseNice() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * casted variable. |
||
| 492 | * @alias for getExchangeRate |
||
| 493 | * |
||
| 494 | * @return float |
||
| 495 | */ |
||
| 496 | public function ExchangeRate() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * |
||
| 503 | * @return float |
||
| 504 | */ |
||
| 505 | public function getExchangeRate() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * casted variable. |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function ExchangeRateExplanation() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function getExchangeRateExplanation() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @return bool |
||
| 542 | */ |
||
| 543 | public function IsCurrent() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Returns the link that can be used in the shopping cart to |
||
| 552 | * set the preferred currency to this one. |
||
| 553 | * For example: /shoppingcart/setcurrency/nzd/ |
||
| 554 | * Dont be fooled by the set_ part in the set_currency_link.... |
||
| 555 | * |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | public function Link() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * returns the link type. |
||
| 565 | * |
||
| 566 | * @return string (link | default | current) |
||
| 567 | */ |
||
| 568 | public function LinkingMode() |
||
| 582 | |||
| 583 | public function validate() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Standard SS Method |
||
| 611 | * Adds the default currency. |
||
| 612 | */ |
||
| 613 | public function populateDefaults() |
||
| 618 | |||
| 619 | public function onBeforeWrite() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Standard SS Method |
||
| 634 | * Adds the default currency. |
||
| 635 | */ |
||
| 636 | public function requireDefaultRecords() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * checks if a currency exists, creates it and returns it. |
||
| 646 | * |
||
| 647 | * @param string $code |
||
| 648 | * @param string $name OPTIONAL |
||
| 649 | */ |
||
| 650 | public static function create_new($code, $name = '') |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Debug helper method. |
||
| 687 | * Can be called from /shoppingcart/debug/. |
||
| 688 | * |
||
| 689 | * @return string |
||
| 690 | */ |
||
| 691 | public function debug() |
||
| 695 | |||
| 696 | private static $currencies = array( |
||
| 697 | 'AFA' => 'afghanistan afghanis', |
||
| 698 | 'ALL' => 'albania leke', |
||
| 699 | 'DZD' => 'algeria dinars', |
||
| 700 | 'ARS' => 'argentina pesos', |
||
| 701 | 'AUD' => 'australia dollars', |
||
| 702 | 'ATS' => 'austria schillings*', |
||
| 703 | 'BSD' => 'bahamas dollars', |
||
| 704 | 'BHD' => 'bahrain dinars', |
||
| 705 | 'BDT' => 'bangladesh taka', |
||
| 706 | 'BBD' => 'barbados dollars', |
||
| 707 | 'BEF' => 'belgium francs*', |
||
| 708 | 'BMD' => 'bermuda dollars', |
||
| 709 | 'BRL' => 'brazil reais', |
||
| 710 | 'BGN' => 'bulgaria leva', |
||
| 711 | 'CAD' => 'canada dollars', |
||
| 712 | 'XOF' => 'cfa bceao francs', |
||
| 713 | 'XAF' => 'cfa beac francs', |
||
| 714 | 'CLP' => 'chile pesos', |
||
| 715 | 'CNY' => 'china yuan renminbi', |
||
| 716 | 'COP' => 'colombia pesos', |
||
| 717 | 'CRC' => 'costa rica colones', |
||
| 718 | 'HRK' => 'croatia kuna', |
||
| 719 | 'CYP' => 'cyprus pounds', |
||
| 720 | 'CZK' => 'czech republic koruny', |
||
| 721 | 'DKK' => 'denmark kroner', |
||
| 722 | 'DOP' => 'dominican republic pesos', |
||
| 723 | 'XCD' => 'eastern caribbean dollars', |
||
| 724 | 'EGP' => 'egypt pounds', |
||
| 725 | 'EEK' => 'estonia krooni', |
||
| 726 | 'EUR' => 'euro', |
||
| 727 | 'FJD' => 'fiji dollars', |
||
| 728 | 'DEM' => 'germany deutsche marks*', |
||
| 729 | 'XAU' => 'gold ounces', |
||
| 730 | 'NLG' => 'holland (netherlands) guilders*', |
||
| 731 | 'HKD' => 'hong kong dollars', |
||
| 732 | 'HUF' => 'hungary forint', |
||
| 733 | 'ISK' => 'iceland kronur', |
||
| 734 | 'XDR' => 'imf special drawing right', |
||
| 735 | 'INR' => 'india rupees', |
||
| 736 | 'IDR' => 'indonesia rupiahs', |
||
| 737 | 'IRR' => 'iran rials', |
||
| 738 | 'IQD' => 'iraq dinars', |
||
| 739 | 'ILS' => 'israel new shekels', |
||
| 740 | 'JMD' => 'jamaica dollars', |
||
| 741 | 'JPY' => 'japan yen', |
||
| 742 | 'JOD' => 'jordan dinars', |
||
| 743 | 'KES' => 'kenya shillings', |
||
| 744 | 'KRW' => 'korea (south) won', |
||
| 745 | 'KWD' => 'kuwait dinars', |
||
| 746 | 'LBP' => 'lebanon pounds', |
||
| 747 | 'MYR' => 'malaysia ringgits', |
||
| 748 | 'MTL' => 'malta liri', |
||
| 749 | 'MUR' => 'mauritius rupees', |
||
| 750 | 'MXN' => 'mexico pesos', |
||
| 751 | 'MAD' => 'morocco dirhams', |
||
| 752 | 'NZD' => 'new zealand dollars', |
||
| 753 | 'NOK' => 'norway kroner', |
||
| 754 | 'OMR' => 'oman rials', |
||
| 755 | 'PKR' => 'pakistan rupees', |
||
| 756 | 'XPD' => 'palladium ounces', |
||
| 757 | 'PEN' => 'peru nuevos soles', |
||
| 758 | 'PHP' => 'philippines pesos', |
||
| 759 | 'PLN' => 'poland zlotych', |
||
| 760 | 'QAR' => 'qatar riyals', |
||
| 761 | 'ROL' => 'romania lei', |
||
| 762 | 'RUB' => 'russia rubles', |
||
| 763 | 'SAR' => 'saudi arabia riyals', |
||
| 764 | 'XAG' => 'silver ounces', |
||
| 765 | 'SGD' => 'singapore dollars', |
||
| 766 | 'SKK' => 'slovakia koruny', |
||
| 767 | 'SIT' => 'slovenia tolars', |
||
| 768 | 'ZAR' => 'south africa rand', |
||
| 769 | 'KRW' => 'south korea won', |
||
| 770 | 'LKR' => 'sri lanka rupees', |
||
| 771 | 'SDD' => 'sudan dinars', |
||
| 772 | 'SEK' => 'sweden kronor', |
||
| 773 | 'CHF' => 'switzerland francs', |
||
| 774 | 'TWD' => 'taiwan new dollars', |
||
| 775 | 'THB' => 'thailand baht', |
||
| 776 | 'TTD' => 'trinidad and tobago dollars', |
||
| 777 | 'TND' => 'tunisia dinars', |
||
| 778 | 'TRY' => 'turkey new lira', |
||
| 779 | 'AED' => 'united arab emirates dirhams', |
||
| 780 | 'gbp' => 'united kingdom pounds', |
||
| 781 | 'USD' => 'united states dollars', |
||
| 782 | 'VEB' => 'venezuela bolivares', |
||
| 783 | 'VND' => 'vietnam dong', |
||
| 784 | 'ZMK' => 'zambia kwacha', |
||
| 785 | ); |
||
| 786 | } |
||
| 787 |