Total Complexity | 73 |
Total Lines | 483 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
Complex classes like Commerce 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 Commerce, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Commerce extends Component |
||
36 | { |
||
37 | // Public Methods |
||
38 | // ========================================================================= |
||
39 | |||
40 | /** |
||
41 | * Send analytics information for the completed order |
||
42 | * |
||
43 | * @param Order $order the Product or Variant |
||
44 | */ |
||
45 | public function orderComplete($order = null) |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Send analytics information for the item added to the cart |
||
72 | * |
||
73 | * @param Order $order the Product or Variant |
||
74 | * @param LineItem $lineItem the line item that was added |
||
75 | */ |
||
76 | public function addToCart( |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Send analytics information for the item removed from the cart |
||
103 | * |
||
104 | * @param Order|null $order |
||
105 | * @param LineItem|null $lineItem |
||
106 | */ |
||
107 | public function removeFromCart( |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Add a Craft Commerce OrderModel to an Analytics object |
||
140 | * |
||
141 | * @param IAnalytics $analytics the Analytics object |
||
142 | * @param Order $order the Product or Variant |
||
143 | */ |
||
144 | public function addCommerceOrderToAnalytics($analytics = null, $order = null) |
||
145 | { |
||
146 | if ($order && $analytics) { |
||
147 | // First, include the transaction data |
||
148 | $analytics->setTransactionId($order->reference) |
||
149 | ->setCurrencyCode($order->paymentCurrency) |
||
150 | ->setRevenue($order->totalPrice) |
||
151 | ->setTax($order->getTotalTax()) |
||
152 | ->setShipping($order->getTotalShippingCost()); |
||
153 | |||
154 | // Coupon code? |
||
155 | if ($order->couponCode) { |
||
156 | $analytics->setCouponCode($order->couponCode); |
||
157 | } |
||
158 | |||
159 | // Add each line item in the transaction |
||
160 | // Two cases - variant and non variant products |
||
161 | $index = 1; |
||
162 | |||
163 | foreach ($order->lineItems as $key => $lineItem) { |
||
164 | $this->addProductDataFromLineItem($analytics, $lineItem, $index, ''); |
||
165 | $index++; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Add a Craft Commerce LineItem to an Analytics object |
||
172 | * |
||
173 | * @param IAnalytics|null $analytics |
||
174 | * @param LineItem|null $lineItem |
||
175 | * @param int $index |
||
176 | * @param string $listName |
||
177 | * |
||
178 | * @return string the title of the product |
||
179 | * @throws \yii\base\InvalidConfigException |
||
180 | */ |
||
181 | public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = ''): string |
||
182 | { |
||
183 | $result = ''; |
||
184 | if ($lineItem && $analytics) { |
||
185 | $product = null; |
||
186 | $purchasable = $lineItem->purchasable; |
||
187 | //This is the same for both variant and non variant products |
||
188 | $productData = [ |
||
189 | 'name' => $purchasable->title, |
||
190 | 'sku' => $purchasable->sku, |
||
191 | 'price' => $lineItem->salePrice, |
||
192 | 'quantity' => $lineItem->qty, |
||
193 | ]; |
||
194 | // Handle this purchasable being a Variant |
||
195 | if (is_a($purchasable, Variant::class)) { |
||
196 | /** @var Variant $purchasable */ |
||
197 | $product = $purchasable->getProduct(); |
||
198 | $variant = $purchasable; |
||
199 | // Product with variants |
||
200 | $productData['name'] = $product->title; |
||
201 | $productData['variant'] = $variant->title; |
||
202 | $productData['category'] = $product->getType(); |
||
203 | } |
||
204 | // Handle this purchasable being a Product |
||
205 | if (is_a($purchasable, Product::class)) { |
||
206 | /** @var Product $purchasable */ |
||
207 | $product = $purchasable; |
||
208 | $productData['name'] = $product->title; |
||
209 | $productData['variant'] = $product->title; |
||
210 | $productData['category'] = $product->getType(); |
||
211 | } |
||
212 | // Handle product lists |
||
213 | if ($index) { |
||
214 | $productData['position'] = $index; |
||
215 | } |
||
216 | if ($listName) { |
||
217 | $productData['list'] = $listName; |
||
218 | } |
||
219 | // Add in any custom categories/brands that might be set |
||
220 | if (InstantAnalytics::$settings && $product) { |
||
221 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
222 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
223 | $productData['category'] = $this->pullDataFromField( |
||
224 | $product, |
||
225 | InstantAnalytics::$settings['productCategoryField'] |
||
226 | ); |
||
227 | } |
||
228 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
229 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
230 | $productData['brand'] = $this->pullDataFromField( |
||
231 | $product, |
||
232 | InstantAnalytics::$settings['productBrandField'] |
||
233 | ); |
||
234 | } |
||
235 | } |
||
236 | $result = $productData['name']; |
||
237 | //Add each product to the hit to be sent |
||
238 | $analytics->addProduct($productData); |
||
239 | } |
||
240 | |||
241 | return $result; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Add a product impression from a Craft Commerce Product or Variant |
||
246 | * |
||
247 | * @param IAnalytics $analytics the Analytics object |
||
248 | * @param Product|Variant $productVariant the Product or Variant |
||
249 | * @param int $index Where the product appears in the |
||
250 | * list |
||
251 | * @param string $listName |
||
252 | * @param int $listIndex |
||
253 | * |
||
254 | * @throws \yii\base\InvalidConfigException |
||
255 | */ |
||
256 | public function addCommerceProductImpression( |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Add a product detail view from a Craft Commerce Product or Variant |
||
290 | * |
||
291 | * @param IAnalytics $analytics the Analytics object |
||
292 | * @param Product|Variant $productVariant the Product or Variant |
||
293 | * |
||
294 | * @throws \yii\base\InvalidConfigException |
||
295 | */ |
||
296 | public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Add a checkout step and option to an Analytics object |
||
317 | * |
||
318 | * @param IAnalytics $analytics the Analytics object |
||
319 | * @param Order $order the Product or Variant |
||
320 | * @param int $step the checkout step |
||
321 | * @param string $option the checkout option |
||
322 | */ |
||
323 | public function addCommerceCheckoutStep($analytics = null, $order = null, $step = 1, $option = '') |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Extract product data from a Craft Commerce Product or Variant |
||
354 | * |
||
355 | * @param Product|Variant $productVariant the Product or Variant |
||
356 | * |
||
357 | * @return array the product data |
||
358 | * @throws \yii\base\InvalidConfigException |
||
359 | */ |
||
360 | public function getProductDataFromProduct($productVariant = null): array |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * @param Product|Variant|null $productVariant |
||
462 | * @param string $fieldHandle |
||
463 | * @param bool $isBrand |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | private function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
518 | } |
||
519 | } |
||
520 |