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