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() |
||
273 | |||
274 | /** |
||
275 | * Used in getCSMFields. |
||
276 | * |
||
277 | * @return GridField |
||
278 | **/ |
||
279 | protected function getProductGroupsTableField() |
||
290 | |||
291 | /** |
||
292 | * Used in getCSMFields. |
||
293 | * |
||
294 | * @return LiteralField |
||
295 | **/ |
||
296 | protected function getAdditionalImagesMessage() |
||
322 | |||
323 | /** |
||
324 | * Used in getCSMFields. |
||
325 | * |
||
326 | * @return GridField |
||
327 | **/ |
||
328 | protected function getAdditionalImagesField() |
||
337 | |||
338 | /** |
||
339 | * Used in getCSMFields. |
||
340 | * |
||
341 | * @return GridField |
||
342 | **/ |
||
343 | protected function getAdditionalFilesField() |
||
352 | |||
353 | /** |
||
354 | * How to view using AJAX |
||
355 | * e.g. if you want to load the produyct in a list - using AJAX |
||
356 | * then use this link |
||
357 | * Opening the link will return a HTML snippet. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function AjaxLink() |
||
365 | |||
366 | /** |
||
367 | * Adds keywords to the MetaKeyword |
||
368 | * Standard SS Method. |
||
369 | */ |
||
370 | public function onBeforeWrite() |
||
405 | |||
406 | /** |
||
407 | * standard SS Method |
||
408 | * Make sure that the image is a product image. |
||
409 | */ |
||
410 | public function onAfterWrite() |
||
420 | |||
421 | |||
422 | /** |
||
423 | * sets the FullName and FullSiteTreeField to the latest values |
||
424 | * This can be useful as you can compare it to the ones saved in the database. |
||
425 | * Returns true if the value is different from the one in the database. |
||
426 | * |
||
427 | * @return bool |
||
428 | */ |
||
429 | public function prepareFullFields() |
||
464 | |||
465 | //GROUPS AND SIBLINGS |
||
466 | |||
467 | /** |
||
468 | * Returns all the parent groups for the product. |
||
469 | * |
||
470 | *@return DataList (ProductGroups) |
||
471 | **/ |
||
472 | public function AllParentGroups() |
||
482 | |||
483 | /** |
||
484 | * Returns all the parent groups for the product, |
||
485 | * including the parents and parents and so on. |
||
486 | * |
||
487 | * @return DataList (ProductGroups) |
||
488 | */ |
||
489 | public function AllParentGroupsIncludingParents() |
||
508 | |||
509 | /** |
||
510 | * @return Product ... |
||
511 | * we have this so that Variations can link to products |
||
512 | * and products link to themselves... |
||
513 | */ |
||
514 | public function getProduct() |
||
515 | { |
||
516 | return $this; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Returns the direct parent group for the product. |
||
521 | * |
||
522 | * @return ProductGroup | NULL |
||
523 | **/ |
||
524 | public function MainParentGroup() |
||
528 | |||
529 | /** |
||
530 | * Returns the top parent group of the product (in the hierarchy). |
||
531 | * |
||
532 | * @return ProductGroup | NULL |
||
533 | **/ |
||
534 | public function TopParentGroup() |
||
549 | |||
550 | /** |
||
551 | * Returns products in the same group. |
||
552 | * |
||
553 | * @return DataList (Products) |
||
554 | **/ |
||
555 | public function Siblings() |
||
571 | |||
572 | //IMAGE |
||
573 | /** |
||
574 | * returns a "BestAvailable" image if the current one is not available |
||
575 | * In some cases this is appropriate and in some cases this is not. |
||
576 | * For example, consider the following setup |
||
577 | * - product A with three variations |
||
578 | * - Product A has an image, but the variations have no images |
||
579 | * With this scenario, you want to show ONLY the product image |
||
580 | * on the product page, but if one of the variations is added to the |
||
581 | * cart, then you want to show the product image. |
||
582 | * This can be achieved bu using the BestAvailable image. |
||
583 | * |
||
584 | * @return Image | Null |
||
585 | */ |
||
586 | public function BestAvailableImage() |
||
601 | |||
602 | /** |
||
603 | * Little hack to show thumbnail in summary fields in modeladmin in CMS. |
||
604 | * |
||
605 | * @return string (HTML = formatted image) |
||
606 | */ |
||
607 | public function CMSThumbnail() |
||
617 | |||
618 | /** |
||
619 | * Returns a link to a default image. |
||
620 | * If a default image is set in the site config then this link is returned |
||
621 | * Otherwise, a standard link is returned. |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | public function DefaultImageLink() |
||
629 | |||
630 | /** |
||
631 | * returns the default image of the product. |
||
632 | * |
||
633 | * @return Image | Null |
||
634 | */ |
||
635 | public function DefaultImage() |
||
639 | |||
640 | /** |
||
641 | * returns a product image for use in templates |
||
642 | * e.g. $DummyImage.Width();. |
||
643 | * |
||
644 | * @return Product_Image |
||
645 | */ |
||
646 | public function DummyImage() |
||
650 | |||
651 | // VERSIONING |
||
652 | |||
653 | /** |
||
654 | * Conditions for whether a product can be purchased. |
||
655 | * |
||
656 | * If it has the checkbox for 'Allow this product to be purchased', |
||
657 | * as well as having a price, it can be purchased. Otherwise a user |
||
658 | * can't buy it. |
||
659 | * |
||
660 | * Other conditions may be added by decorating with the canPurcahse function |
||
661 | * |
||
662 | * @return bool |
||
663 | */ |
||
664 | |||
665 | /** |
||
666 | * @TODO: complete |
||
667 | * |
||
668 | * @param string $compontent - the has many relationship you are looking at, e.g. OrderAttribute |
||
669 | * |
||
670 | * @return DataList (CHECK!) |
||
671 | */ |
||
672 | public function getVersionedComponents($component = 'ProductVariations') |
||
681 | |||
682 | /** |
||
683 | * Action to return specific version of a specific product. |
||
684 | * This can be any product to enable the retrieval of deleted products. |
||
685 | * This is really useful for sold products where you want to retrieve the actual version that you sold. |
||
686 | * If the version can not be found then we retrieve the current one. |
||
687 | * |
||
688 | * @param int $id |
||
689 | * @param int $version |
||
690 | * |
||
691 | * @return DataObject | Null |
||
692 | */ |
||
693 | public function getVersionOfBuyable($id = 0, $version = 0) |
||
710 | |||
711 | //ORDER ITEM |
||
712 | |||
713 | /** |
||
714 | * returns the order item associated with the buyable. |
||
715 | * ALWAYS returns one, even if there is none in the cart. |
||
716 | * Does not write to database. |
||
717 | * |
||
718 | * @return OrderItem (no kidding) |
||
719 | **/ |
||
720 | public function OrderItem() |
||
734 | |||
735 | /** |
||
736 | * @var string |
||
737 | */ |
||
738 | protected $defaultClassNameForOrderItem = 'Product_OrderItem'; |
||
739 | |||
740 | /** |
||
741 | * you can overwrite this function in your buyable items (such as Product). |
||
742 | * |
||
743 | * @return string |
||
744 | **/ |
||
745 | public function classNameForOrderItem() |
||
755 | |||
756 | /** |
||
757 | * You can set an alternative class name for order item using this method. |
||
758 | * |
||
759 | * @param string $ClassName |
||
760 | **/ |
||
761 | public function setAlternativeClassNameForOrderItem($className) |
||
765 | |||
766 | /** |
||
767 | * This is used when you add a product to your cart |
||
768 | * if you set it to 1 then you can add 0.1 product to cart. |
||
769 | * If you set it to -1 then you can add 10, 20, 30, etc.. products to cart. |
||
770 | * |
||
771 | * @return int |
||
772 | **/ |
||
773 | public function QuantityDecimals() |
||
777 | |||
778 | /** |
||
779 | * Number of items sold. |
||
780 | * |
||
781 | * @return int |
||
782 | */ |
||
783 | public function HasBeenSold() |
||
801 | |||
802 | //LINKS |
||
803 | |||
804 | /** |
||
805 | * Tells us the link to select variations |
||
806 | * If ajaxified, this controller method (selectvariation) |
||
807 | * Will return a html snippet for selecting the variation. |
||
808 | * This is useful in the Product Group where you can both |
||
809 | * non-variation and variation products to have the same |
||
810 | * "add to cart" button. Using this link you can provide a |
||
811 | * pop-up select system for selecting a variation. |
||
812 | * |
||
813 | * @return string |
||
814 | */ |
||
815 | public function AddVariationsLink() |
||
819 | |||
820 | /** |
||
821 | * passing on shopping cart links ...is this necessary?? ...why not just pass the cart? |
||
822 | * |
||
823 | * @return string |
||
824 | */ |
||
825 | public function AddLink() |
||
829 | |||
830 | /** |
||
831 | * link use to add (one) to cart. |
||
832 | * |
||
833 | *@return string |
||
834 | */ |
||
835 | public function IncrementLink() |
||
840 | |||
841 | /** |
||
842 | * Link used to remove one from cart |
||
843 | * we can do this, because by default remove link removes one. |
||
844 | * |
||
845 | * @return string |
||
846 | */ |
||
847 | public function DecrementLink() |
||
851 | |||
852 | /** |
||
853 | * remove one buyable's orderitem from cart. |
||
854 | * |
||
855 | * @return string (Link) |
||
856 | */ |
||
857 | public function RemoveLink() |
||
861 | |||
862 | /** |
||
863 | * remove all of this buyable's orderitem from cart. |
||
864 | * |
||
865 | * @return string (Link) |
||
866 | */ |
||
867 | public function RemoveAllLink() |
||
871 | |||
872 | /** |
||
873 | * remove all of this buyable's orderitem from cart and go through to this buyble to add alternative selection. |
||
874 | * |
||
875 | * @return string (Link) |
||
876 | */ |
||
877 | public function RemoveAllAndEditLink() |
||
881 | |||
882 | /** |
||
883 | * set new specific new quantity for buyable's orderitem. |
||
884 | * |
||
885 | * @param float |
||
886 | * |
||
887 | * @return string (Link) |
||
888 | */ |
||
889 | public function SetSpecificQuantityItemLink($quantity) |
||
893 | |||
894 | /** |
||
895 | * @return string |
||
896 | */ |
||
897 | public function AddToCartAndGoToCheckoutLink() |
||
904 | |||
905 | /** |
||
906 | * |
||
907 | * |
||
908 | * @return string |
||
909 | */ |
||
910 | public function VersionedLink() |
||
921 | |||
922 | public function RemoveFromSaleLink() |
||
926 | |||
927 | /** |
||
928 | * Here you can add additional information to your product |
||
929 | * links such as the AddLink and the RemoveLink. |
||
930 | * One useful parameter you can add is the BackURL link. |
||
931 | * |
||
932 | * Usage would be by means of |
||
933 | * 1. decorating product |
||
934 | * 2. adding a updateLinkParameters method |
||
935 | * 3. adding items to the array. |
||
936 | * |
||
937 | * You can also extend Product and override this method... |
||
938 | * |
||
939 | * @return array |
||
940 | **/ |
||
941 | protected function linkParameters($type = '') |
||
953 | |||
954 | //TEMPLATE STUFF |
||
955 | |||
956 | /** |
||
957 | * @return bool |
||
958 | */ |
||
959 | public function IsInCart() |
||
963 | |||
964 | /** |
||
965 | * @return EcomQuantityField |
||
966 | */ |
||
967 | public function EcomQuantityField() |
||
971 | |||
972 | /** |
||
973 | * returns the instance of EcommerceConfigAjax for use in templates. |
||
974 | * In templates, it is used like this: |
||
975 | * $EcommerceConfigAjax.TableID. |
||
976 | * |
||
977 | * @return EcommerceConfigAjax |
||
978 | **/ |
||
979 | public function AJAXDefinitions() |
||
983 | |||
984 | /** |
||
985 | * @return EcommerceDBConfig |
||
986 | **/ |
||
987 | public function EcomConfig() |
||
991 | |||
992 | /** |
||
993 | * Is it a variation? |
||
994 | * |
||
995 | * @return bool |
||
996 | */ |
||
997 | public function IsProductVariation() |
||
1001 | |||
1002 | /** |
||
1003 | * tells us if the current page is part of e-commerce. |
||
1004 | * |
||
1005 | * @return bool |
||
1006 | */ |
||
1007 | public function IsEcommercePage() |
||
1011 | |||
1012 | public function AllowPurchaseNice() |
||
1016 | |||
1017 | /** |
||
1018 | * Products have a standard price, but for specific situations they have a calculated price. |
||
1019 | * The Price can be changed for specific member discounts, etc... |
||
1020 | * |
||
1021 | * @return float |
||
1022 | */ |
||
1023 | public function CalculatedPrice() |
||
1027 | |||
1028 | private static $_calculated_price_cache = array(); |
||
1029 | |||
1030 | /** |
||
1031 | * Products have a standard price, but for specific situations they have a calculated price. |
||
1032 | * The Price can be changed for specific member discounts, etc... |
||
1033 | * |
||
1034 | * We add three "hooks" / "extensions" here... so that you can update prices |
||
1035 | * in a logical order (e.g. firstly change to forex and then apply discount) |
||
1036 | * |
||
1037 | * @return float |
||
1038 | */ |
||
1039 | public function getCalculatedPrice($forceRecalculation = false) |
||
1059 | |||
1060 | /** |
||
1061 | * How do we display the price? |
||
1062 | * |
||
1063 | * @return Money |
||
1064 | */ |
||
1065 | public function CalculatedPriceAsMoney() |
||
1073 | |||
1074 | //CRUD SETTINGS |
||
1075 | |||
1076 | /** |
||
1077 | * Is the product for sale? |
||
1078 | * |
||
1079 | * @param Member $member |
||
1080 | * @param bool $checkPrice |
||
1081 | * |
||
1082 | * @return bool |
||
1083 | */ |
||
1084 | public function canPurchase(Member $member = null, $checkPrice = true) |
||
1120 | |||
1121 | public function canCreate($member = null) |
||
1136 | |||
1137 | /** |
||
1138 | * Shop Admins can edit. |
||
1139 | * |
||
1140 | * @param Member $member |
||
1141 | * |
||
1142 | * @return bool |
||
1143 | */ |
||
1144 | public function canEdit($member = null) |
||
1159 | |||
1160 | /** |
||
1161 | * Standard SS method. |
||
1162 | * |
||
1163 | * @param Member $member |
||
1164 | * |
||
1165 | * @return bool |
||
1166 | */ |
||
1167 | public function canDelete($member = null) |
||
1182 | |||
1183 | /** |
||
1184 | * Standard SS method. |
||
1185 | * |
||
1186 | * @param Member $member |
||
1187 | * |
||
1188 | * @return bool |
||
1189 | */ |
||
1190 | public function canPublish($member = null) |
||
1194 | |||
1195 | |||
1196 | public function debug() |
||
1246 | |||
1247 | /** |
||
1248 | * |
||
1249 | * @int |
||
1250 | */ |
||
1251 | public function IDForSearchResults() |
||
1255 | |||
1256 | /** |
||
1257 | * |
||
1258 | * @string |
||
1259 | */ |
||
1260 | public function InternalItemIDForSearchResults() |
||
1264 | } |
||
1265 | |||
1497 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.