Total Complexity | 60 |
Total Lines | 416 |
Duplicated Lines | 0 % |
Changes | 9 | ||
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 |
||
35 | class Commerce extends Component |
||
36 | { |
||
37 | // Public Methods |
||
38 | // ========================================================================= |
||
39 | |||
40 | /** |
||
41 | * Enqueue analytics information for the completed order |
||
42 | * |
||
43 | * @param ?Order $order the Product or Variant |
||
44 | */ |
||
45 | public function triggerOrderCompleteEvent(Order $order = null) |
||
57 | ); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Enqueue analytics information for a new checkout flow |
||
63 | * |
||
64 | * @param ?Order $order |
||
65 | */ |
||
66 | public function triggerBeginCheckoutEvent(Order $order = null) |
||
67 | { |
||
68 | if ($order) { |
||
69 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->BeginCheckoutEvent(); |
||
70 | // First, include the transaction data |
||
71 | $event->setCurrency($order->getPaymentCurrency()) |
||
72 | ->setValue($order->getTotalPrice()); |
||
73 | |||
74 | // Add each line item in the cart |
||
75 | $index = 1; |
||
76 | foreach ($order->lineItems as $lineItem) { |
||
77 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
78 | $index++; |
||
79 | } |
||
80 | |||
81 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
82 | |||
83 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
84 | 'Adding `Commerce - Begin Checkout event``', |
||
85 | [], |
||
86 | __METHOD__ |
||
87 | ); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Send analytics information for the item added to the cart |
||
93 | * |
||
94 | * @param LineItem $lineItem the line item that was added |
||
95 | */ |
||
96 | public function triggerAddToCartEvent(LineItem $lineItem): void |
||
97 | { |
||
98 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->AddToCartEvent(); |
||
99 | $this->addProductDataFromLineItem($event, $lineItem); |
||
100 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
101 | |||
102 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
103 | 'Adding `Commerce - Add to Cart event`: `{title}` => `{quantity}`', |
||
104 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
105 | __METHOD__ |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Send analytics information for the item removed from the cart |
||
111 | * |
||
112 | * @param LineItem $lineItem |
||
113 | */ |
||
114 | public function triggerRemoveFromCartEvent(LineItem $lineItem) |
||
115 | { |
||
116 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->RemoveFromCartEvent(); |
||
117 | $this->addProductDataFromLineItem($event, $lineItem); |
||
118 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
119 | |||
120 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
121 | 'Adding `Commerce - Remove from Cart event`: `{title}` => `{quantity}`', |
||
122 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
123 | __METHOD__ |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | |||
128 | /** |
||
129 | * Add a Craft Commerce OrderModel to a Purchase Event |
||
130 | * |
||
131 | * @param PurchaseEvent $event The PurchaseEvent |
||
132 | * @param Order $order |
||
133 | */ |
||
134 | protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Add a Craft Commerce LineItem to an Analytics object |
||
160 | * |
||
161 | * @param ItemBaseEvent $event |
||
162 | * @param LineItem $lineItem |
||
163 | * @param int $index |
||
164 | * @param string $listName |
||
165 | * |
||
166 | * @return string the title of the product |
||
167 | * @throws \yii\base\InvalidConfigException |
||
168 | */ |
||
169 | protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
||
170 | { |
||
171 | $eventItem = $this->getNewItemParameter(); |
||
172 | |||
173 | $product = null; |
||
174 | $purchasable = $lineItem->purchasable; |
||
175 | |||
176 | if ($purchasable === null) { |
||
177 | $eventItem->setItemName($lineItem->getDescription()); |
||
178 | $eventItem->setItemId($lineItem->getSku()); |
||
179 | } else { |
||
180 | $eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
||
181 | $eventItem->setItemId($purchasable->getSku() ?? $lineItem->getSku()); |
||
182 | } |
||
183 | $eventItem->setPrice($lineItem->salePrice); |
||
184 | $eventItem->setQuantity($lineItem->qty); |
||
185 | |||
186 | // Handle this purchasable being a Variant |
||
187 | if (is_a($purchasable, Variant::class)) { |
||
188 | /** @var Variant $purchasable */ |
||
189 | $product = $purchasable->getProduct(); |
||
190 | $variant = $purchasable; |
||
191 | // Product with variants |
||
192 | $eventItem->setItemName($product->title); |
||
193 | $eventItem->setItemVariant($variant->title); |
||
194 | $eventItem->setItemCategory($product->getType()); |
||
195 | } |
||
196 | |||
197 | // Handle this purchasable being a Product |
||
198 | if (is_a($purchasable, Product::class)) { |
||
199 | /** @var Product $purchasable */ |
||
200 | $product = $purchasable; |
||
201 | $eventItem->setItemName($product->title); |
||
202 | $eventItem->setItemVariant($product->title); |
||
203 | $eventItem->setItemCategory($product->getType()); |
||
204 | } |
||
205 | |||
206 | // Handle product lists |
||
207 | if ($index) { |
||
208 | $eventItem->setIndex($index); |
||
209 | } |
||
210 | |||
211 | if ($listName) { |
||
212 | $eventItem->setItemListName($listName); |
||
213 | } |
||
214 | |||
215 | // Add in any custom categories/brands that might be set |
||
216 | if (InstantAnalytics::$settings && $product) { |
||
217 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
218 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
219 | $category = $this->pullDataFromField( |
||
220 | $product, |
||
221 | InstantAnalytics::$settings['productCategoryField'] |
||
222 | ); |
||
223 | $eventItem->setItemCategory($category); |
||
224 | } |
||
225 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
226 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
227 | $brand = $this->pullDataFromField( |
||
228 | $product, |
||
229 | InstantAnalytics::$settings['productBrandField'] |
||
230 | ); |
||
231 | |||
232 | $eventItem->setItemBrand($brand); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | //Add each product to the hit to be sent |
||
237 | $event->addItem($eventItem); |
||
238 | |||
239 | return $eventItem->getItemName(); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Add a product impression from a Craft Commerce Product or Variant |
||
244 | * |
||
245 | * @param Product|Variant $productVariant the Product or Variant |
||
246 | * @throws \yii\base\InvalidConfigException |
||
247 | */ |
||
248 | public function addCommerceProductImpression(Variant|Product $productVariant): void |
||
249 | { |
||
250 | if ($productVariant) { |
||
251 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
||
252 | $this->addProductDataFromProductOrVariant($event, $productVariant); |
||
253 | |||
254 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
255 | |||
256 | $sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
||
257 | $name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
||
258 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
259 | 'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
||
260 | ['sku' => $sku, 'name' => $name], |
||
261 | __METHOD__ |
||
262 | ); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Add a product list impression from a Craft Commerce Product or Variant list |
||
268 | * |
||
269 | * @param Product[]|Variant[] $products |
||
270 | * @param string $listName |
||
271 | */ |
||
272 | public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
||
286 | ); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Extract product data from a Craft Commerce Product or Variant |
||
292 | * |
||
293 | * @param Product|Variant|null $productVariant the Product or Variant |
||
294 | * |
||
295 | * @throws \yii\base\InvalidConfigException |
||
296 | */ |
||
297 | protected function addProductDataFromProductOrVariant(ItemBaseEvent $event, $productVariant = null, $index = null, $listName = ''): void |
||
298 | { |
||
299 | if ($productVariant === null) { |
||
300 | return; |
||
301 | } |
||
302 | |||
303 | $eventItem = $this->getNewItemParameter(); |
||
304 | |||
305 | $isVariant = $productVariant instanceof Variant; |
||
306 | $variant = $isVariant ? $productVariant : $productVariant->getDefaultVariant(); |
||
307 | |||
308 | if (!$variant) { |
||
309 | return; |
||
310 | } |
||
311 | |||
312 | $eventItem->setItemId($variant->sku); |
||
313 | $eventItem->setItemName($variant->title); |
||
314 | $eventItem->setPrice(number_format($variant->price, 2, '.', '')); |
||
315 | |||
316 | $category = ($isVariant ? $variant->getProduct() : $productVariant)->getType()['name']; |
||
317 | |||
318 | if (InstantAnalytics::$settings) { |
||
319 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
320 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
321 | $category = $this->pullDataFromField( |
||
322 | $productVariant, |
||
323 | InstantAnalytics::$settings['productCategoryField'] |
||
324 | ); |
||
325 | if (empty($productData['category']) && $isVariant) { |
||
326 | $category = $this->pullDataFromField( |
||
327 | $productVariant->product, |
||
328 | InstantAnalytics::$settings['productCategoryField'] |
||
329 | ); |
||
330 | } |
||
331 | } |
||
332 | $eventItem->setItemCategory($category); |
||
333 | |||
334 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
335 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
336 | $brand = $this->pullDataFromField( |
||
337 | $productVariant, |
||
338 | InstantAnalytics::$settings['productBrandField'], |
||
339 | true |
||
340 | ); |
||
341 | |||
342 | if (empty($productData['brand']) && $isVariant) { |
||
343 | $brand = $this->pullDataFromField( |
||
344 | $productVariant, |
||
345 | InstantAnalytics::$settings['productBrandField'], |
||
346 | true |
||
347 | ); |
||
348 | } |
||
349 | $eventItem->setItemBrand($brand); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | if ($index !== null) { |
||
354 | $eventItem->setIndex($index); |
||
355 | } |
||
356 | |||
357 | if (!empty($listName)) { |
||
358 | $eventItem->setItemListName($listName); |
||
359 | } |
||
360 | |||
361 | // Add item info to the event |
||
362 | $event->addItem($eventItem); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @param Product|Variant|null $productVariant |
||
367 | * @param string $fieldHandle |
||
368 | * @param bool $isBrand |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @param bool $isBrand |
||
406 | * @param array $elements |
||
407 | * @return string |
||
408 | */ |
||
409 | protected function getDataFromElements(bool $isBrand, array $elements): string |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Create an item parameter and set affiliation on it, if any exists. |
||
441 | * |
||
442 | * @return ItemParameter |
||
443 | */ |
||
444 | protected function getNewItemParameter(): ItemParameter |
||
451 | } |
||
452 | } |
||
453 |