Complex classes like Product 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 Product, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Product extends Page implements BuyableModel |
||
|
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Standard SS variable. |
||
| 24 | */ |
||
| 25 | private static $api_access = array( |
||
| 26 | 'view' => array( |
||
| 27 | 'Title', |
||
| 28 | 'Price', |
||
| 29 | 'Weight', |
||
| 30 | 'Model', |
||
| 31 | 'Quantifier', |
||
| 32 | 'FeaturedProduct', |
||
| 33 | 'AllowPurchase', |
||
| 34 | 'InternalItemID', //ie SKU, ProductID etc (internal / existing recognition of product) |
||
| 35 | 'NumberSold', //store number sold, so it doesn't have to be computed on the fly. Used for determining popularity. |
||
| 36 | 'Version', |
||
| 37 | ), |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Standard SS variable. |
||
| 42 | */ |
||
| 43 | private static $db = array( |
||
| 44 | 'Price' => 'Currency', |
||
| 45 | 'Weight' => 'Float', |
||
| 46 | 'Model' => 'Varchar(30)', |
||
| 47 | 'Quantifier' => 'Varchar(30)', |
||
| 48 | 'FeaturedProduct' => 'Boolean', |
||
| 49 | 'AllowPurchase' => 'Boolean', |
||
| 50 | 'InternalItemID' => 'Varchar(30)', //ie SKU, ProductID etc (internal / existing recognition of product) |
||
| 51 | 'NumberSold' => 'Int', //store number sold, so it doesn't have to be computed on the fly. Used for determining popularity. |
||
| 52 | 'FullSiteTreeSort' => 'Decimal(64, 0)', //store the complete sort numbers from current page up to level 1 page, for sitetree sorting |
||
| 53 | 'FullName' => 'Varchar(255)', //Name for look-up lists |
||
| 54 | 'ShortDescription' => 'Varchar(255)', //For use in lists. |
||
| 55 | ); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Standard SS variable. |
||
| 59 | */ |
||
| 60 | private static $has_one = array( |
||
| 61 | 'Image' => 'Product_Image', |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Standard SS variable. |
||
| 66 | */ |
||
| 67 | private static $many_many = array( |
||
| 68 | 'ProductGroups' => 'ProductGroup', |
||
| 69 | 'AdditionalImages' => 'Image', |
||
| 70 | 'AdditionalFiles' => 'File', |
||
| 71 | ); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Standard SS variable. |
||
| 75 | */ |
||
| 76 | private static $casting = array( |
||
| 77 | 'CalculatedPrice' => 'Currency', |
||
| 78 | 'CalculatedPriceAsMoney' => 'Money', |
||
| 79 | 'AllowPurchaseNice' => 'Varchar', |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Standard SS variable. |
||
| 84 | */ |
||
| 85 | private static $indexes = array( |
||
| 86 | 'FullSiteTreeSort' => true, |
||
| 87 | 'FullName' => true, |
||
| 88 | 'InternalItemID' => true, |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Standard SS variable. |
||
| 93 | */ |
||
| 94 | private static $defaults = array( |
||
| 95 | 'AllowPurchase' => 1, |
||
| 96 | ); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Standard SS variable. |
||
| 100 | */ |
||
| 101 | //private static $default_sort = "\"FullSiteTreeSort\" ASC, \"Sort\" ASC, \"InternalItemID\" ASC, \"Price\" ASC"; |
||
| 102 | //private static $default_sort = "\"Sort\" ASC, \"InternalItemID\" ASC, \"Price\" ASC"; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Standard SS variable. |
||
| 106 | */ |
||
| 107 | private static $summary_fields = array( |
||
| 108 | 'Image.CMSThumbnail' => 'Image', |
||
| 109 | 'FullName' => 'Description', |
||
| 110 | 'Price' => 'Price', |
||
| 111 | 'AllowPurchaseNice' => 'For Sale', |
||
| 112 | ); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Standard SS variable. |
||
| 116 | */ |
||
| 117 | private static $searchable_fields = array( |
||
| 118 | 'FullName' => array( |
||
| 119 | 'title' => 'Keyword', |
||
| 120 | 'field' => 'TextField', |
||
| 121 | ), |
||
| 122 | 'Price' => array( |
||
| 123 | 'title' => 'Price', |
||
| 124 | 'field' => 'NumericField', |
||
| 125 | ), |
||
| 126 | 'InternalItemID' => array( |
||
| 127 | 'title' => 'Internal Item ID', |
||
| 128 | 'filter' => 'PartialMatchFilter', |
||
| 129 | ), |
||
| 130 | 'AllowPurchase', |
||
| 131 | 'ShowInSearch', |
||
| 132 | 'ShowInMenus', |
||
| 133 | 'FeaturedProduct', |
||
| 134 | ); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * By default we search for products that are allowed to be purchased only |
||
| 138 | * standard SS method. |
||
| 139 | * |
||
| 140 | * @return FieldList |
||
| 141 | */ |
||
| 142 | public function scaffoldSearchFields($_params = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Standard SS variable. |
||
| 152 | */ |
||
| 153 | private static $singular_name = 'Product'; |
||
| 154 | public function i18n_singular_name() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Standard SS variable. |
||
| 161 | */ |
||
| 162 | private static $plural_name = 'Products'; |
||
| 163 | public function i18n_plural_name() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Standard SS variable. |
||
| 170 | * |
||
| 171 | * @var string |
||
| 172 | */ |
||
| 173 | private static $description = 'A product that is for sale in the shop.'; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Standard SS variable. |
||
| 177 | */ |
||
| 178 | private static $default_parent = 'ProductGroup'; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Standard SS variable. |
||
| 182 | */ |
||
| 183 | private static $icon = 'ecommerce/images/icons/product'; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Standard SS Method. |
||
| 187 | */ |
||
| 188 | public function getCMSFields() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Used in getCSMFields. |
||
| 275 | * |
||
| 276 | * @return GridField |
||
| 277 | **/ |
||
| 278 | protected function getProductGroupsTableField() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Used in getCSMFields. |
||
| 292 | * |
||
| 293 | * @return LiteralField |
||
| 294 | **/ |
||
| 295 | protected function getAdditionalImagesMessage() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Used in getCSMFields. |
||
| 324 | * |
||
| 325 | * @return GridField |
||
| 326 | **/ |
||
| 327 | protected function getAdditionalImagesField() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Used in getCSMFields. |
||
| 339 | * |
||
| 340 | * @return GridField |
||
| 341 | **/ |
||
| 342 | protected function getAdditionalFilesField() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * How to view using AJAX |
||
| 354 | * e.g. if you want to load the produyct in a list - using AJAX |
||
| 355 | * then use this link |
||
| 356 | * Opening the link will return a HTML snippet. |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function AjaxLink() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Adds keywords to the MetaKeyword |
||
| 367 | * Standard SS Method. |
||
| 368 | */ |
||
| 369 | public function onBeforeWrite() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * standard SS Method |
||
| 407 | * Make sure that the image is a product image. |
||
| 408 | */ |
||
| 409 | public function onAfterWrite() |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * sets the FullName and FullSiteTreeField to the latest values |
||
| 423 | * This can be useful as you can compare it to the ones saved in the database. |
||
| 424 | * Returns true if the value is different from the one in the database. |
||
| 425 | * |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function prepareFullFields() |
||
| 463 | |||
| 464 | //GROUPS AND SIBLINGS |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns all the parent groups for the product. |
||
| 468 | * |
||
| 469 | *@return DataList (ProductGroups) |
||
| 470 | **/ |
||
| 471 | public function AllParentGroups() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns all the parent groups for the product, |
||
| 484 | * including the parents and parents and so on. |
||
| 485 | * |
||
| 486 | * @return DataList (ProductGroups) |
||
| 487 | */ |
||
| 488 | public function AllParentGroupsIncludingParents() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return Product ... |
||
| 510 | * we have this so that Variations can link to products |
||
| 511 | * and products link to themselves... |
||
| 512 | */ |
||
| 513 | public function getProduct() |
||
| 514 | { |
||
| 515 | return $this; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns the direct parent group for the product. |
||
| 520 | * |
||
| 521 | * @return ProductGroup | NULL |
||
| 522 | **/ |
||
| 523 | public function MainParentGroup() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns the top parent group of the product (in the hierarchy). |
||
| 530 | * |
||
| 531 | * @return ProductGroup | NULL |
||
| 532 | **/ |
||
| 533 | public function TopParentGroup() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns products in the same group. |
||
| 548 | * |
||
| 549 | * @return DataList (Products) |
||
| 550 | **/ |
||
| 551 | public function Siblings() |
||
| 567 | |||
| 568 | //IMAGE |
||
| 569 | /** |
||
| 570 | * returns a "BestAvailable" image if the current one is not available |
||
| 571 | * In some cases this is appropriate and in some cases this is not. |
||
| 572 | * For example, consider the following setup |
||
| 573 | * - product A with three variations |
||
| 574 | * - Product A has an image, but the variations have no images |
||
| 575 | * With this scenario, you want to show ONLY the product image |
||
| 576 | * on the product page, but if one of the variations is added to the |
||
| 577 | * cart, then you want to show the product image. |
||
| 578 | * This can be achieved bu using the BestAvailable image. |
||
| 579 | * |
||
| 580 | * @return Image | Null |
||
| 581 | */ |
||
| 582 | public function BestAvailableImage() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Little hack to show thumbnail in summary fields in modeladmin in CMS. |
||
| 600 | * |
||
| 601 | * @return string (HTML = formatted image) |
||
| 602 | */ |
||
| 603 | public function CMSThumbnail() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Returns a link to a default image. |
||
| 616 | * If a default image is set in the site config then this link is returned |
||
| 617 | * Otherwise, a standard link is returned. |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | public function DefaultImageLink() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * returns the default image of the product. |
||
| 628 | * |
||
| 629 | * @return Image | Null |
||
| 630 | */ |
||
| 631 | public function DefaultImage() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * returns a product image for use in templates |
||
| 638 | * e.g. $DummyImage.Width();. |
||
| 639 | * |
||
| 640 | * @return Product_Image |
||
| 641 | */ |
||
| 642 | public function DummyImage() |
||
| 646 | |||
| 647 | // VERSIONING |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Conditions for whether a product can be purchased. |
||
| 651 | * |
||
| 652 | * If it has the checkbox for 'Allow this product to be purchased', |
||
| 653 | * as well as having a price, it can be purchased. Otherwise a user |
||
| 654 | * can't buy it. |
||
| 655 | * |
||
| 656 | * Other conditions may be added by decorating with the canPurcahse function |
||
| 657 | * |
||
| 658 | * @return bool |
||
| 659 | */ |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @TODO: complete |
||
| 663 | * |
||
| 664 | * @param string $compontent - the has many relationship you are looking at, e.g. OrderAttribute |
||
| 665 | * |
||
| 666 | * @return DataList (CHECK!) |
||
| 667 | */ |
||
| 668 | public function getVersionedComponents($component = 'ProductVariations') |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Action to return specific version of a specific product. |
||
| 680 | * This can be any product to enable the retrieval of deleted products. |
||
| 681 | * This is really useful for sold products where you want to retrieve the actual version that you sold. |
||
| 682 | * If the version can not be found then we retrieve the current one. |
||
| 683 | * |
||
| 684 | * @param int $id |
||
| 685 | * @param int $version |
||
| 686 | * |
||
| 687 | * @return DataObject | Null |
||
| 688 | */ |
||
| 689 | public function getVersionOfBuyable($id = 0, $version = 0) |
||
| 706 | |||
| 707 | //ORDER ITEM |
||
| 708 | |||
| 709 | /** |
||
| 710 | * returns the order item associated with the buyable. |
||
| 711 | * ALWAYS returns one, even if there is none in the cart. |
||
| 712 | * Does not write to database. |
||
| 713 | * |
||
| 714 | * @return OrderItem (no kidding) |
||
| 715 | **/ |
||
| 716 | public function OrderItem() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @var string |
||
| 733 | */ |
||
| 734 | protected $defaultClassNameForOrderItem = 'Product_OrderItem'; |
||
| 735 | |||
| 736 | /** |
||
| 737 | * you can overwrite this function in your buyable items (such as Product). |
||
| 738 | * |
||
| 739 | * @return string |
||
| 740 | **/ |
||
| 741 | public function classNameForOrderItem() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * You can set an alternative class name for order item using this method. |
||
| 754 | * |
||
| 755 | * @param string $ClassName |
||
| 756 | **/ |
||
| 757 | public function setAlternativeClassNameForOrderItem($className) |
||
| 761 | |||
| 762 | /** |
||
| 763 | * This is used when you add a product to your cart |
||
| 764 | * if you set it to 1 then you can add 0.1 product to cart. |
||
| 765 | * If you set it to -1 then you can add 10, 20, 30, etc.. products to cart. |
||
| 766 | * |
||
| 767 | * @return int |
||
| 768 | **/ |
||
| 769 | public function QuantityDecimals() |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Number of items sold. |
||
| 776 | * |
||
| 777 | * @return int |
||
| 778 | */ |
||
| 779 | public function HasBeenSold() |
||
| 797 | |||
| 798 | //LINKS |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Tells us the link to select variations |
||
| 802 | * If ajaxified, this controller method (selectvariation) |
||
| 803 | * Will return a html snippet for selecting the variation. |
||
| 804 | * This is useful in the Product Group where you can both |
||
| 805 | * non-variation and variation products to have the same |
||
| 806 | * "add to cart" button. Using this link you can provide a |
||
| 807 | * pop-up select system for selecting a variation. |
||
| 808 | * |
||
| 809 | * @return string |
||
| 810 | */ |
||
| 811 | public function AddVariationsLink() |
||
| 815 | |||
| 816 | /** |
||
| 817 | * passing on shopping cart links ...is this necessary?? ...why not just pass the cart? |
||
| 818 | * |
||
| 819 | * @return string |
||
| 820 | */ |
||
| 821 | public function AddLink() |
||
| 825 | |||
| 826 | /** |
||
| 827 | * link use to add (one) to cart. |
||
| 828 | * |
||
| 829 | *@return string |
||
| 830 | */ |
||
| 831 | public function IncrementLink() |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Link used to remove one from cart |
||
| 839 | * we can do this, because by default remove link removes one. |
||
| 840 | * |
||
| 841 | * @return string |
||
| 842 | */ |
||
| 843 | public function DecrementLink() |
||
| 847 | |||
| 848 | /** |
||
| 849 | * remove one buyable's orderitem from cart. |
||
| 850 | * |
||
| 851 | * @return string (Link) |
||
| 852 | */ |
||
| 853 | public function RemoveLink() |
||
| 857 | |||
| 858 | /** |
||
| 859 | * remove all of this buyable's orderitem from cart. |
||
| 860 | * |
||
| 861 | * @return string (Link) |
||
| 862 | */ |
||
| 863 | public function RemoveAllLink() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * remove all of this buyable's orderitem from cart and go through to this buyble to add alternative selection. |
||
| 870 | * |
||
| 871 | * @return string (Link) |
||
| 872 | */ |
||
| 873 | public function RemoveAllAndEditLink() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * set new specific new quantity for buyable's orderitem. |
||
| 880 | * |
||
| 881 | * @param float |
||
| 882 | * |
||
| 883 | * @return string (Link) |
||
| 884 | */ |
||
| 885 | public function SetSpecificQuantityItemLink($quantity) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * @return string |
||
| 892 | */ |
||
| 893 | public function AddToCartAndGoToCheckoutLink() |
||
| 900 | |||
| 901 | /** |
||
| 902 | * |
||
| 903 | * |
||
| 904 | * @return string |
||
| 905 | */ |
||
| 906 | public function VersionedLink() |
||
| 917 | |||
| 918 | public function RemoveFromSaleLink() |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Here you can add additional information to your product |
||
| 925 | * links such as the AddLink and the RemoveLink. |
||
| 926 | * One useful parameter you can add is the BackURL link. |
||
| 927 | * |
||
| 928 | * Usage would be by means of |
||
| 929 | * 1. decorating product |
||
| 930 | * 2. adding a updateLinkParameters method |
||
| 931 | * 3. adding items to the array. |
||
| 932 | * |
||
| 933 | * You can also extend Product and override this method... |
||
| 934 | * |
||
| 935 | * @return array |
||
| 936 | **/ |
||
| 937 | protected function linkParameters($type = '') |
||
| 949 | |||
| 950 | //TEMPLATE STUFF |
||
| 951 | |||
| 952 | /** |
||
| 953 | * @return bool |
||
| 954 | */ |
||
| 955 | public function IsInCart() |
||
| 959 | |||
| 960 | /** |
||
| 961 | * @return EcomQuantityField |
||
| 962 | */ |
||
| 963 | public function EcomQuantityField() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * returns the instance of EcommerceConfigAjax for use in templates. |
||
| 970 | * In templates, it is used like this: |
||
| 971 | * $EcommerceConfigAjax.TableID. |
||
| 972 | * |
||
| 973 | * @return EcommerceConfigAjax |
||
| 974 | **/ |
||
| 975 | public function AJAXDefinitions() |
||
| 979 | |||
| 980 | /** |
||
| 981 | * @return EcommerceDBConfig |
||
| 982 | **/ |
||
| 983 | public function EcomConfig() |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Is it a variation? |
||
| 990 | * |
||
| 991 | * @return bool |
||
| 992 | */ |
||
| 993 | public function IsProductVariation() |
||
| 997 | |||
| 998 | /** |
||
| 999 | * tells us if the current page is part of e-commerce. |
||
| 1000 | * |
||
| 1001 | * @return bool |
||
| 1002 | */ |
||
| 1003 | public function IsEcommercePage() |
||
| 1007 | |||
| 1008 | public function AllowPurchaseNice() |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Products have a standard price, but for specific situations they have a calculated price. |
||
| 1015 | * The Price can be changed for specific member discounts, etc... |
||
| 1016 | * |
||
| 1017 | * @return float |
||
| 1018 | */ |
||
| 1019 | public function CalculatedPrice() |
||
| 1023 | |||
| 1024 | private static $_calculated_price_cache = array(); |
||
| 1025 | |||
| 1026 | public function getCalculatedPrice() |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * How do we display the price? |
||
| 1041 | * |
||
| 1042 | * @return Money |
||
| 1043 | */ |
||
| 1044 | public function CalculatedPriceAsMoney() |
||
| 1052 | |||
| 1053 | //CRUD SETTINGS |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Is the product for sale? |
||
| 1057 | * |
||
| 1058 | * @param Member $member |
||
| 1059 | * @param bool $checkPrice |
||
| 1060 | * |
||
| 1061 | * @return bool |
||
| 1062 | */ |
||
| 1063 | public function canPurchase(Member $member = null, $checkPrice = true) |
||
| 1099 | |||
| 1100 | public function canCreate($member = null) |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Shop Admins can edit. |
||
| 1118 | * |
||
| 1119 | * @param Member $member |
||
| 1120 | * |
||
| 1121 | * @return bool |
||
| 1122 | */ |
||
| 1123 | public function canEdit($member = null) |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Standard SS method. |
||
| 1141 | * |
||
| 1142 | * @param Member $member |
||
| 1143 | * |
||
| 1144 | * @return bool |
||
| 1145 | */ |
||
| 1146 | public function canDelete($member = null) |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Standard SS method. |
||
| 1164 | * |
||
| 1165 | * @param Member $member |
||
| 1166 | * |
||
| 1167 | * @return bool |
||
| 1168 | */ |
||
| 1169 | public function canPublish($member = null) |
||
| 1173 | |||
| 1174 | |||
| 1175 | public function debug() |
||
| 1225 | } |
||
| 1226 | |||
| 1458 |
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.