Total Complexity | 44 |
Total Lines | 333 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like AddToCartForm 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.
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 AddToCartForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class AddToCartForm extends Form |
||
28 | { |
||
29 | /** |
||
30 | * @var FoxyHelper |
||
31 | */ |
||
32 | protected FoxyHelper $helper; |
||
33 | |||
34 | /** |
||
35 | * @var Product |
||
36 | */ |
||
37 | private Product $product; |
||
38 | |||
39 | /** |
||
40 | * @param $helper |
||
41 | * |
||
42 | * @return $this |
||
43 | */ |
||
44 | public function setFoxyHelper($helper): self |
||
45 | { |
||
46 | $helper = $helper === null ? FoxyHelper::create() : $helper; |
||
47 | if ($helper instanceof FoxyHelper) { |
||
48 | $this->helper = $helper; |
||
49 | |||
50 | return $this; |
||
51 | } |
||
52 | throw new \InvalidArgumentException('$helper needs to be an instance of FoxyHelper.'); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return FoxyHelper |
||
57 | */ |
||
58 | public function getFoxyHelper(): FoxyHelper |
||
59 | { |
||
60 | if (!$this->helper) { |
||
61 | $this->setFoxyHelper(FoxyHelper::create()); |
||
62 | } |
||
63 | |||
64 | return $this->helper; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param $product |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function setProduct($product): self |
||
73 | { |
||
74 | if ($product instanceof VirtualPage) { |
||
75 | if (!$product = Product::get_by_id(Product::class, $product->CopyContentFromID)) { |
||
76 | throw new \InvalidArgumentException(sprintf('$product needs to be a descendant of %s, or a Virtual Page copied from a %s descendant.', Product::class, Product::class)); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | $this->product = $product; |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return Product |
||
87 | */ |
||
88 | public function getProduct(): Product |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * AddToCartForm constructor. |
||
95 | * @param $controller |
||
96 | * @param $name |
||
97 | * @param FieldList|null $fields |
||
98 | * @param FieldList|null $actions |
||
99 | * @param null $validator |
||
|
|||
100 | * @param null $product |
||
101 | * @param null $helper |
||
102 | * @throws ValidationException |
||
103 | */ |
||
104 | public function __construct( |
||
105 | $controller, |
||
106 | $name, |
||
107 | FieldList $fields = null, |
||
108 | FieldList $actions = null, |
||
109 | $validator = null, |
||
110 | $product = null, |
||
111 | $helper = null |
||
112 | ) |
||
113 | { |
||
114 | $this->setProduct($product); |
||
115 | $this->setFoxyHelper($helper); |
||
116 | |||
117 | $fields = ($fields != null && $fields->exists()) ? |
||
118 | $this->getProductFields($fields) : |
||
119 | $this->getProductFields(FieldList::create()); |
||
120 | |||
121 | $actions = ($actions != null && $actions->exists()) ? |
||
122 | $this->getProductActions($actions) : |
||
123 | $this->getProductActions(FieldList::create()); |
||
124 | |||
125 | $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
||
126 | |||
127 | parent::__construct($controller, $name, $fields, $actions, $validator); |
||
128 | |||
129 | //have to call after parent::__construct() |
||
130 | $this->setAttribute('action', FoxyHelper::FormActionURL()); |
||
131 | $this->disableSecurityToken(); |
||
132 | $this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}"); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param FieldList $fields |
||
137 | * |
||
138 | * @return FieldList |
||
139 | */ |
||
140 | protected function getProductFields(FieldList $fields): FieldList |
||
141 | { |
||
142 | $hiddenTitle = ($this->product->ReceiptTitle) ? |
||
143 | htmlspecialchars($this->product->ReceiptTitle) : |
||
144 | htmlspecialchars($this->product->Title); |
||
145 | $code = $this->product->Code; |
||
146 | |||
147 | if ($this->product->getIsAvailable()) { |
||
148 | $fields->push( |
||
149 | HiddenField::create('name') |
||
150 | ->setValue( |
||
151 | self::getGeneratedValue($code, 'name', $hiddenTitle, 'value') |
||
152 | ) |
||
153 | ); |
||
154 | |||
155 | $fields->push( |
||
156 | HiddenField::create('category') |
||
157 | ->setValue( |
||
158 | self::getGeneratedValue($code, 'category', $this->product->FoxyCategory()->Code, 'value') |
||
159 | ) |
||
160 | ); |
||
161 | |||
162 | $fields->push( |
||
163 | HiddenField::create('code') |
||
164 | ->setValue( |
||
165 | self::getGeneratedValue($code, 'code', $this->product->Code, 'value') |
||
166 | ) |
||
167 | ); |
||
168 | |||
169 | $fields->push( |
||
170 | HiddenField::create('product_id') |
||
171 | ->setValue( |
||
172 | self::getGeneratedValue($code, 'product_id', $this->product->ID, 'value') |
||
173 | ) |
||
174 | ->setName('h:product_id') |
||
175 | ); |
||
176 | |||
177 | $fields->push( |
||
178 | HiddenField::create('price') |
||
179 | ->setValue( |
||
180 | self::getGeneratedValue($code, 'price', $this->product->Price, 'value') |
||
181 | ) |
||
182 | ); |
||
183 | |||
184 | if ($this->product->hasMethod('AbsoluteLink')) { |
||
185 | $fields->push( |
||
186 | HiddenField::create('url') |
||
187 | ->setValue( |
||
188 | self::getGeneratedValue($code, 'url', $this->product->AbsoluteLink(), 'value') |
||
189 | ) |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | if ($this->product instanceof ShippableProduct) { |
||
194 | if ($this->product->Weight > 0) { |
||
195 | $fields->push( |
||
196 | HiddenField::create('weight') |
||
197 | ->setValue( |
||
198 | self::getGeneratedValue($code, 'weight', $this->product->Weight, 'value') |
||
199 | ) |
||
200 | ); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | $image = null; |
||
205 | if ($this->product->hasMethod('getImage')) { |
||
206 | if ($this->product->getImage()) { |
||
207 | $image = $this->product->getImage()->CMSThumbnail()->absoluteURL; |
||
208 | } |
||
209 | if ($image) { |
||
210 | $fields->push( |
||
211 | HiddenField::create('image') |
||
212 | ->setValue( |
||
213 | self::getGeneratedValue($code, 'image', $image, 'value') |
||
214 | ) |
||
215 | ); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | if ($this->product->QuantityMax > 0) { |
||
220 | $fields->push( |
||
221 | HiddenField::create('quantity_max') |
||
222 | ->setValue(self::getGeneratedValue($code, 'quantity_max', $this->product->QuantityMax, 'value')) |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | if ($variationsField = $this->getProductVariations()) { |
||
227 | $fields->push($variationsField); |
||
228 | } |
||
229 | |||
230 | if ($this->product->QuantityMax != 1) { |
||
231 | $fields->push(QuantityField::create('x:visibleQuantity')->setTitle('Quantity')->setValue(1)); |
||
232 | } |
||
233 | $fields->push( |
||
234 | HiddenField::create('quantity') |
||
235 | ->setValue( |
||
236 | self::getGeneratedValue($code, 'quantity', 1, 'value') |
||
237 | ) |
||
238 | ); |
||
239 | |||
240 | $fields->push( |
||
241 | HeaderField::create('submitPrice', '$' . $this->product->Price, 4) |
||
242 | ->addExtraClass('submit-price') |
||
243 | ); |
||
244 | } |
||
245 | |||
246 | $this->extend('updateProductFields', $fields); |
||
247 | |||
248 | return $fields; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param FieldList $actions |
||
253 | * |
||
254 | * @return FieldList |
||
255 | */ |
||
256 | protected function getProductActions(FieldList $actions): FieldList |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param null $productCode |
||
276 | * @param null $optionName |
||
277 | * @param null $optionValue |
||
278 | * @param string $method |
||
279 | * @param bool $output |
||
280 | * @param bool $urlEncode |
||
281 | * |
||
282 | * @return null|string |
||
283 | */ |
||
284 | public static function getGeneratedValue( |
||
285 | $productCode = null, |
||
286 | $optionName = null, |
||
287 | $optionValue = null, |
||
288 | $method = 'name', |
||
289 | $output = false, |
||
290 | $urlEncode = false |
||
291 | ): string |
||
292 | { |
||
293 | $optionName = ($optionName !== null) ? preg_replace('/\s/', '_', $optionName) : $optionName; |
||
294 | $helper = FoxyHelper::create(); |
||
295 | |||
296 | return $helper::fc_hash_value($productCode, $optionName, $optionValue, $method, $output, $urlEncode); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @return CompositeField|FormField|DropdownField|bool |
||
301 | */ |
||
302 | protected function getProductVariations() |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param HasManyList $variations |
||
326 | * @param VariationType|null $type |
||
327 | * @return DropdownField |
||
328 | */ |
||
329 | protected function createVariationField(HasManyList $variations, VariationType $type = null): DropdownField |
||
362 |