Total Complexity | 75 |
Total Lines | 500 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 1 | 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 |
||
33 | class Commerce extends Component |
||
34 | { |
||
35 | // Public Methods |
||
36 | // ========================================================================= |
||
37 | |||
38 | /** |
||
39 | * Send analytics information for the completed order |
||
40 | * |
||
41 | * @param Order $order the Product or Variant |
||
42 | */ |
||
43 | public function orderComplete($order = null) |
||
44 | { |
||
45 | if ($order) { |
||
46 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
47 | 'Commerce', |
||
48 | 'Purchase', |
||
49 | $order->reference, |
||
50 | $order->totalPrice |
||
51 | ); |
||
52 | |||
53 | if ($analytics) { |
||
54 | $this->addCommerceOrderToAnalytics($analytics, $order); |
||
55 | // Don't forget to set the product action, in this case to PURCHASE |
||
56 | $analytics->setProductActionToPurchase(); |
||
57 | $analytics->sendEvent(); |
||
58 | |||
59 | Craft::info(Craft::t( |
||
60 | 'instant-analytics', |
||
61 | 'orderComplete for `Commerce` - `Purchase` - `{reference}` - `{price}`', |
||
62 | ['reference' => $order->reference, 'price' => $order->totalPrice] |
||
63 | ), __METHOD__); |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Send analytics information for the item added to the cart |
||
70 | * |
||
71 | * @param Order $order the Product or Variant |
||
72 | * @param LineItem $lineItem the line item that was added |
||
73 | */ |
||
74 | public function addToCart( |
||
75 | /** @noinspection PhpUnusedParameterInspection */ |
||
76 | $order = null, $lineItem = null |
||
77 | ) |
||
78 | { |
||
79 | if ($lineItem) { |
||
80 | $title = $lineItem->purchasable->title ?? $lineItem->description; |
||
81 | $quantity = $lineItem->qty; |
||
82 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics('Commerce', 'Add to Cart', $title, $quantity); |
||
83 | |||
84 | if ($analytics) { |
||
85 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
86 | $analytics->setEventLabel($title); |
||
87 | // Don't forget to set the product action, in this case to ADD |
||
88 | $analytics->setProductActionToAdd(); |
||
89 | $analytics->sendEvent(); |
||
90 | |||
91 | Craft::info(Craft::t( |
||
92 | 'instant-analytics', |
||
93 | 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', |
||
94 | ['title' => $title, 'quantity' => $quantity] |
||
95 | ), __METHOD__); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Send analytics information for the item removed from the cart |
||
102 | * |
||
103 | * @param Order|null $order |
||
104 | * @param LineItem|null $lineItem |
||
105 | */ |
||
106 | public function removeFromCart( |
||
107 | /** @noinspection PhpUnusedParameterInspection */ |
||
108 | $order = null, $lineItem = null |
||
109 | ) |
||
110 | { |
||
111 | if ($lineItem) { |
||
112 | $title = $lineItem->purchasable->title ?? $lineItem->description; |
||
113 | $quantity = $lineItem->qty; |
||
114 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
115 | 'Commerce', |
||
116 | 'Remove from Cart', |
||
117 | $title, |
||
118 | $quantity |
||
119 | ); |
||
120 | |||
121 | if ($analytics) { |
||
122 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
123 | $analytics->setEventLabel($title); |
||
124 | // Don't forget to set the product action, in this case to ADD |
||
125 | $analytics->setProductActionToRemove(); |
||
126 | $analytics->sendEvent(); |
||
127 | |||
128 | Craft::info(Craft::t( |
||
129 | 'instant-analytics', |
||
130 | 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', |
||
131 | ['title' => $title, 'quantity' => $quantity] |
||
132 | ), __METHOD__); |
||
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 |
||
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( |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Add a product detail view from a Craft Commerce Product or Variant |
||
291 | * |
||
292 | * @param IAnalytics $analytics the Analytics object |
||
293 | * @param Product|Variant $productVariant the Product or Variant |
||
294 | * |
||
295 | * @throws \yii\base\InvalidConfigException |
||
296 | */ |
||
297 | public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Add a checkout step and option to an Analytics object |
||
318 | * |
||
319 | * @param IAnalytics $analytics the Analytics object |
||
320 | * @param Order $order the Product or Variant |
||
321 | * @param int $step the checkout step |
||
322 | * @param string $option the checkout option |
||
323 | */ |
||
324 | public function addCommerceCheckoutStep($analytics = null, $order = null, $step = 1, $option = '') |
||
350 | } |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Extract product data from a Craft Commerce Product or Variant |
||
355 | * |
||
356 | * @param Product|Variant $productVariant the Product or Variant |
||
357 | * |
||
358 | * @return array the product data |
||
359 | * @throws \yii\base\InvalidConfigException |
||
360 | */ |
||
361 | public function getProductDataFromProduct($productVariant = null): array |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @param Product|Variant|null $productVariant |
||
463 | * @param string $fieldHandle |
||
464 | * @param bool $isBrand |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
469 | { |
||
470 | $result = ''; |
||
471 | if ($productVariant && $fieldHandle) { |
||
472 | $srcField = $productVariant[$fieldHandle] ?? $productVariant->product[$fieldHandle] ?? null; |
||
473 | // Handle eager loaded elements |
||
474 | if (is_array($srcField)) { |
||
475 | return $this->getDataFromElements($isBrand, $srcField); |
||
476 | } |
||
477 | // If the source field isn't an object, return nothing |
||
478 | if (!is_object($srcField)) { |
||
479 | return $result; |
||
480 | } |
||
481 | switch (\get_class($srcField)) { |
||
482 | case MatrixBlockQuery::class: |
||
483 | break; |
||
484 | case TagQuery::class: |
||
485 | break; |
||
486 | case CategoryQuery::class: |
||
487 | $result = $this->getDataFromElements($isBrand, $srcField->all()); |
||
488 | break; |
||
489 | |||
490 | |||
491 | default: |
||
492 | $result = strip_tags($srcField); |
||
493 | break; |
||
494 | } |
||
495 | } |
||
496 | |||
497 | return $result; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @param bool $isBrand |
||
502 | * @param array $elements |
||
503 | * @return string |
||
504 | */ |
||
505 | protected function getDataFromElements(bool $isBrand, array $elements): string |
||
533 | } |
||
534 | } |
||
535 |