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