Complex classes like EcommerceDBConfig 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 EcommerceDBConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class EcommerceDBConfig extends DataObject implements EditableEcommerceObject |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Standard SS Variable. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private static $db = array( |
||
|
|
|||
| 21 | 'Title' => 'Varchar(30)', |
||
| 22 | 'UseThisOne' => 'Boolean', |
||
| 23 | 'ShopClosed' => 'Boolean', |
||
| 24 | 'ShopPricesAreTaxExclusive' => 'Boolean', |
||
| 25 | 'InvoiceTitle' => 'Varchar(200)', |
||
| 26 | 'InvoiceMessage' => 'HTMLText', |
||
| 27 | 'PackingSlipTitle' => 'Varchar(200)', |
||
| 28 | 'PackingSlipNote' => 'HTMLText', |
||
| 29 | 'ShopPhysicalAddress' => 'HTMLText', |
||
| 30 | 'ReceiptEmail' => 'Varchar(255)', |
||
| 31 | 'PostalCodeURL' => 'Varchar(255)', |
||
| 32 | 'PostalCodeLabel' => 'Varchar(255)', |
||
| 33 | 'NumberOfProductsPerPage' => 'Int', |
||
| 34 | 'ProductsAlsoInOtherGroups' => 'Boolean', |
||
| 35 | 'OnlyShowProductsThatCanBePurchased' => 'Boolean', |
||
| 36 | 'NotForSaleMessage' => 'HTMLText', |
||
| 37 | 'ProductsHaveWeight' => 'Boolean', |
||
| 38 | 'ProductsHaveModelNames' => 'Boolean', |
||
| 39 | 'ProductsHaveQuantifiers' => 'Boolean', |
||
| 40 | //"ProductsHaveVariations" => "Boolean", |
||
| 41 | 'CurrenciesExplanation' => 'HTMLText', |
||
| 42 | 'AllowFreeProductPurchase' => 'Boolean', |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Standard SS Variable. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $has_one = array( |
||
| 51 | 'EmailLogo' => 'Image', |
||
| 52 | 'DefaultProductImage' => 'Product_Image', |
||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Standard SS Variable. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private static $indexes = array( |
||
| 61 | 'UseThisOne' => true, |
||
| 62 | 'ShopClosed' => true, |
||
| 63 | 'ShopPricesAreTaxExclusive' => true, |
||
| 64 | 'NumberOfProductsPerPage' => true, |
||
| 65 | 'OnlyShowProductsThatCanBePurchased' => true, |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Standard SS Variable. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private static $casting = array( |
||
| 74 | 'UseThisOneNice' => 'Varchar', |
||
| 75 | ); //adds computed fields that can also have a type (e.g. |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Standard SS Variable. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private static $searchable_fields = array( |
||
| 83 | 'Title' => 'PartialMatchFilter', |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Standard SS Variable. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private static $field_labels = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Standard SS Variable. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private static $summary_fields = array( |
||
| 99 | 'Title' => 'Title', |
||
| 100 | 'UseThisOneNice' => 'Use this configuration set', |
||
| 101 | ); //note no => for relational fields |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Standard SS Method. |
||
| 105 | * |
||
| 106 | * @param Member $member |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | public function canCreate($member = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Standard SS Method. |
||
| 127 | * |
||
| 128 | * @param Member $member |
||
| 129 | * |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | public function canView($member = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Standard SS Method. |
||
| 147 | * |
||
| 148 | * @param Member $member |
||
| 149 | * |
||
| 150 | * @var bool |
||
| 151 | */ |
||
| 152 | public function canEdit($member = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Standard SS Method. |
||
| 170 | * |
||
| 171 | * @param Member $member |
||
| 172 | * |
||
| 173 | * @var bool |
||
| 174 | */ |
||
| 175 | public function canDelete($member = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Standard SS variable. |
||
| 197 | * |
||
| 198 | * @var string |
||
| 199 | */ |
||
| 200 | private static $default_sort = [ |
||
| 201 | 'UseThisOne' => 'DESC', |
||
| 202 | 'ID' => 'ASC' |
||
| 203 | ]; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Standard SS variable. |
||
| 207 | * |
||
| 208 | * @var array |
||
| 209 | */ |
||
| 210 | private static $defaults = array( |
||
| 211 | 'Title' => 'Ecommerce Site Config', |
||
| 212 | 'UseThisOne' => true, |
||
| 213 | 'ShopClosed' => false, |
||
| 214 | 'ShopPricesAreTaxExclusive' => false, |
||
| 215 | 'InvoiceTitle' => 'Invoice', |
||
| 216 | 'InvoiceMessage' => '<p>Thank you for your order</p>', |
||
| 217 | 'PackingSlipTitle' => 'Package Contents', |
||
| 218 | 'PackingSlipNote' => 'Please make sure that all items are contained in this package.', |
||
| 219 | 'ShopPhysicalAddress' => '<p>Enter your shop address here.</p>', |
||
| 220 | //"ReceiptEmail" => "Varchar(255)", - see populate defaults |
||
| 221 | 'PostalCodeURL' => '', |
||
| 222 | 'PostalCodeLabel' => '', |
||
| 223 | 'NumberOfProductsPerPage' => 12, |
||
| 224 | 'ProductsAlsoInOtherGroups' => false, |
||
| 225 | 'OnlyShowProductsThatCanBePurchased' => false, |
||
| 226 | 'NotForSaleMessage' => '<p>Not for sale, please contact us for more information.</p>', |
||
| 227 | 'ProductsHaveWeight' => false, |
||
| 228 | 'ProductsHaveModelNames' => false, |
||
| 229 | 'ProductsHaveQuantifiers' => false, |
||
| 230 | //"ProductsHaveVariations" => false, |
||
| 231 | 'CurrenciesExplanation' => '<p>Apart from our main currency, you can view prices in a number of other currencies. The exchange rate is indicative only.</p>', |
||
| 232 | 'AllowFreeProductPurchase' => true, |
||
| 233 | ); |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Standard SS Method. |
||
| 237 | * |
||
| 238 | * @var array |
||
| 239 | */ |
||
| 240 | public function populateDefaults() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Standard SS variable. |
||
| 248 | * |
||
| 249 | * @var string |
||
| 250 | */ |
||
| 251 | private static $singular_name = 'Main E-commerce Configuration'; |
||
| 252 | public function i18n_singular_name() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Standard SS variable. |
||
| 259 | * |
||
| 260 | * @var string |
||
| 261 | */ |
||
| 262 | private static $plural_name = 'Main E-commerce Configurations'; |
||
| 263 | public function i18n_plural_name() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Standard SS variable. |
||
| 270 | * |
||
| 271 | * @var string |
||
| 272 | */ |
||
| 273 | private static $description = 'A set of configurations for the shop. Each shop needs to have one or more of these settings.'; |
||
| 274 | |||
| 275 | /** |
||
| 276 | * static holder for its own (or other EcommerceDBConfig) class. |
||
| 277 | * |
||
| 278 | * @var string | NULL |
||
| 279 | */ |
||
| 280 | private static $_my_current_one = null; |
||
| 281 | public static function reset_my_current_one() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * implements singleton pattern. |
||
| 288 | * Gets the current USE THIS ONE e-commerce option. |
||
| 289 | * |
||
| 290 | * @return EcommerceDBConfig | Object |
||
| 291 | */ |
||
| 292 | public static function current_ecommerce_db_config() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * standard SS method for decorators. |
||
| 314 | * |
||
| 315 | * @param bool $includerelations |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function fieldLabels($includerelations = true) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * definition of field lables |
||
| 336 | * TODO: is this a common SS method? |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | public function customFieldLabels() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * definition of field lables |
||
| 378 | * TODO: is this a common SS method? |
||
| 379 | * |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public function customDescriptionsForFields() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * standard SS method. |
||
| 402 | * |
||
| 403 | * @return FieldList |
||
| 404 | */ |
||
| 405 | public function getCMSFields() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * link to edit the record. |
||
| 558 | * |
||
| 559 | * @param string | Null $action - e.g. edit |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function CMSEditLink($action = null) |
||
| 567 | |||
| 568 | public function getOrderStepsField() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * tells us if a Class Name is a buyable. |
||
| 585 | * |
||
| 586 | * @todo: consider using Ecomerce Configuration instead? |
||
| 587 | * In EcomConfig we only list base classes. |
||
| 588 | * |
||
| 589 | * @param string $className - name of the class to be tested |
||
| 590 | * |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | public static function is_buyable($className) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Returns the Current Member. |
||
| 605 | */ |
||
| 606 | public function Customer() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Returns the Current Member. |
||
| 613 | */ |
||
| 614 | public function CustomerForOrder() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Return the currency being used on the site e.g. "NZD" or "USD". |
||
| 623 | * |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | public function Currency() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * return null if there is less than two currencies in use |
||
| 633 | * on the site. |
||
| 634 | * |
||
| 635 | * @return DataList | Null |
||
| 636 | */ |
||
| 637 | public function Currencies() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return string (URLSegment) |
||
| 647 | **/ |
||
| 648 | public function AccountPageLink() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @return string (URLSegment) |
||
| 655 | **/ |
||
| 656 | public function CheckoutLink() |
||
| 660 | |||
| 661 | /** |
||
| 662 | *@return string (URLSegment) |
||
| 663 | **/ |
||
| 664 | public function CartPageLink() |
||
| 668 | |||
| 669 | /** |
||
| 670 | *@return string (URLSegment) |
||
| 671 | **/ |
||
| 672 | public function OrderConfirmationPageLink() |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Returns a link to a default image. |
||
| 679 | * If a default image is set in the site config then this link is returned |
||
| 680 | * Otherwise, a standard link is returned. |
||
| 681 | * |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | public function DefaultImageLink() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Returns the default image or a dummy one if it does not exists. |
||
| 698 | * |
||
| 699 | * @return string |
||
| 700 | */ |
||
| 701 | public function DefaultImage() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * standard SS method. |
||
| 719 | */ |
||
| 720 | public function onAfterWrite() |
||
| 743 | |||
| 744 | /** |
||
| 745 | * standard SS Method. |
||
| 746 | */ |
||
| 747 | public function requireDefaultRecords() |
||
| 762 | |||
| 763 | /** |
||
| 764 | * returns site config. |
||
| 765 | * |
||
| 766 | * @return SiteConfig |
||
| 767 | */ |
||
| 768 | public function SiteConfig() |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Casted Variable. |
||
| 775 | * |
||
| 776 | * @return string |
||
| 777 | */ |
||
| 778 | public function UseThisOneNice() |
||
| 782 | } |
||
| 783 |