Total Complexity | 55 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 10 | ||
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 |
||
36 | class Commerce extends Component |
||
37 | { |
||
38 | // Public Methods |
||
39 | // ========================================================================= |
||
40 | |||
41 | /** |
||
42 | * Enqueue analytics information for the completed order |
||
43 | * |
||
44 | * @param ?Order $order the Product or Variant |
||
45 | */ |
||
46 | public function triggerOrderCompleteEvent(Order $order = null) |
||
47 | { |
||
48 | if ($order) { |
||
49 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->PurchaseEvent(); |
||
50 | $this->addCommerceOrderToEvent($event, $order); |
||
51 | |||
52 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
53 | |||
54 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
55 | 'Adding `Commerce - Order Complete event`: `{reference}` => `{price}`', |
||
56 | ['reference' => $order->reference, 'price' => $order->totalPrice], |
||
57 | __METHOD__ |
||
58 | ); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Enqueue analytics information for a new checkout flow |
||
64 | * |
||
65 | * @param ?Order $order |
||
66 | */ |
||
67 | public function triggerBeginCheckoutEvent(Order $order = null) |
||
88 | ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Send analytics information for the item added to the cart |
||
94 | * |
||
95 | * @param LineItem $lineItem the line item that was added |
||
96 | */ |
||
97 | public function triggerAddToCartEvent(LineItem $lineItem): void |
||
98 | { |
||
99 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->AddToCartEvent(); |
||
100 | $this->addProductDataFromLineItem($event, $lineItem); |
||
101 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
102 | |||
103 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
104 | 'Adding `Commerce - Add to Cart event`: `{title}` => `{quantity}`', |
||
105 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
106 | __METHOD__ |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Send analytics information for the item removed from the cart |
||
112 | * |
||
113 | * @param LineItem $lineItem |
||
114 | */ |
||
115 | public function triggerRemoveFromCartEvent(LineItem $lineItem) |
||
116 | { |
||
117 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->RemoveFromCartEvent(); |
||
118 | $this->addProductDataFromLineItem($event, $lineItem); |
||
119 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
120 | |||
121 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
122 | 'Adding `Commerce - Remove from Cart event`: `{title}` => `{quantity}`', |
||
123 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
124 | __METHOD__ |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Add a product impression from a Craft Commerce Product or Variant |
||
130 | * |
||
131 | * @param Product|Variant|null $productVariant the Product or Variant |
||
132 | * @throws InvalidConfigException |
||
133 | */ |
||
134 | public function addCommerceProductImpression(Variant|Product|null $productVariant): void |
||
135 | { |
||
136 | if ($productVariant) { |
||
137 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
||
138 | $this->addProductDataFromProductOrVariant($event, $productVariant); |
||
139 | |||
140 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
141 | |||
142 | $sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
||
143 | $name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
||
144 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
145 | 'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
||
146 | ['sku' => $sku, 'name' => $name], |
||
147 | __METHOD__ |
||
148 | ); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Add a product list impression from a Craft Commerce Product or Variant list |
||
154 | * |
||
155 | * @param Product[]|Variant[] $products |
||
156 | * @param string $listName |
||
157 | */ |
||
158 | public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
||
172 | ); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Add a Craft Commerce OrderModel to a Purchase Event |
||
178 | * |
||
179 | * @param PurchaseEvent $event The PurchaseEvent |
||
180 | * @param Order $order |
||
181 | */ |
||
182 | protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
||
183 | { |
||
184 | // First, include the transaction data |
||
185 | $event->setCurrency($order->getPaymentCurrency()) |
||
186 | ->setTransactionId($order->reference) |
||
187 | ->setValue($order->getTotalPrice()) |
||
188 | ->setTax($order->getTotalTax()) |
||
189 | ->setShipping($order->getTotalShippingCost()); |
||
190 | |||
191 | // Coupon code |
||
192 | if ($order->couponCode) { |
||
193 | $event->setCoupon($order->couponCode); |
||
194 | } |
||
195 | |||
196 | // Add each line item in the transaction |
||
197 | // Two cases - variant and non variant products |
||
198 | $index = 1; |
||
199 | |||
200 | foreach ($order->lineItems as $lineItem) { |
||
201 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
202 | $index++; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Add a Craft Commerce LineItem to an Analytics object |
||
208 | * |
||
209 | * @param ItemBaseEvent $event |
||
210 | * @param LineItem $lineItem |
||
211 | * @param int $index |
||
212 | * @param string $listName |
||
213 | * |
||
214 | * @return string the title of the product |
||
215 | * @throws InvalidConfigException |
||
216 | */ |
||
217 | protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
||
218 | { |
||
219 | $eventItem = $this->getNewItemParameter(); |
||
220 | |||
221 | $product = null; |
||
222 | $purchasable = $lineItem->purchasable; |
||
223 | |||
224 | /** @phpstan-ignore-next-line */ |
||
225 | if ($purchasable === null) { |
||
226 | $eventItem->setItemName($lineItem->getDescription()); |
||
227 | $eventItem->setItemId($lineItem->getSku()); |
||
228 | } else { |
||
229 | $eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
||
230 | $eventItem->setItemId($purchasable->getSku()); |
||
231 | } |
||
232 | $eventItem->setPrice($lineItem->salePrice); |
||
233 | $eventItem->setQuantity($lineItem->qty); |
||
234 | |||
235 | // Handle this purchasable being a Variant |
||
236 | if (is_a($purchasable, Variant::class)) { |
||
237 | /** @var Variant $purchasable */ |
||
238 | $product = $purchasable->getProduct(); |
||
239 | $variant = $purchasable; |
||
240 | // Product with variants |
||
241 | $eventItem->setItemName($product->title); |
||
242 | $eventItem->setItemVariant($variant->title); |
||
243 | $eventItem->setItemCategory($product->getType()); |
||
244 | } |
||
245 | |||
246 | // Handle this purchasable being a Product |
||
247 | if (is_a($purchasable, Product::class)) { |
||
248 | /** @var Product $purchasable */ |
||
249 | $product = $purchasable; |
||
250 | $eventItem->setItemName($product->title); |
||
251 | $eventItem->setItemVariant($product->title); |
||
252 | $eventItem->setItemCategory($product->getType()); |
||
253 | } |
||
254 | |||
255 | // Handle product lists |
||
256 | if ($index) { |
||
257 | $eventItem->setIndex($index); |
||
258 | } |
||
259 | |||
260 | if ($listName) { |
||
261 | $eventItem->setItemListName($listName); |
||
262 | } |
||
263 | |||
264 | // Add in any custom categories/brands that might be set |
||
265 | if (InstantAnalytics::$settings && $product) { |
||
266 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
267 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
268 | $category = $this->pullDataFromField( |
||
269 | $product, |
||
270 | InstantAnalytics::$settings['productCategoryField'] |
||
271 | ); |
||
272 | $eventItem->setItemCategory($category); |
||
273 | } |
||
274 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
275 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
276 | $brand = $this->pullDataFromField( |
||
277 | $product, |
||
278 | InstantAnalytics::$settings['productBrandField'] |
||
279 | ); |
||
280 | |||
281 | $eventItem->setItemBrand($brand); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | //Add each product to the hit to be sent |
||
286 | $event->addItem($eventItem); |
||
287 | |||
288 | return $eventItem->getItemName(); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Extract product data from a Craft Commerce Product or Variant |
||
293 | * |
||
294 | * @param Product|Variant|null $productVariant the Product or Variant |
||
295 | * |
||
296 | * @throws InvalidConfigException |
||
297 | */ |
||
298 | protected function addProductDataFromProductOrVariant(ItemBaseEvent $event, $productVariant = null, $index = null, $listName = ''): void |
||
299 | { |
||
300 | if ($productVariant === null) { |
||
301 | return; |
||
302 | } |
||
303 | |||
304 | $eventItem = $this->getNewItemParameter(); |
||
305 | |||
306 | $isVariant = $productVariant instanceof Variant; |
||
307 | $variant = $isVariant ? $productVariant : $productVariant->getDefaultVariant(); |
||
308 | |||
309 | if (!$variant) { |
||
310 | return; |
||
311 | } |
||
312 | |||
313 | $eventItem->setItemId($variant->sku); |
||
314 | $eventItem->setItemName($variant->title); |
||
315 | $eventItem->setPrice((float)number_format($variant->price, 2, '.', '')); |
||
316 | |||
317 | $category = ($isVariant ? $variant->getProduct() : $productVariant)->getType()['name']; |
||
318 | |||
319 | if (InstantAnalytics::$settings) { |
||
320 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
321 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
322 | $category = $this->pullDataFromField( |
||
323 | $productVariant, |
||
324 | InstantAnalytics::$settings['productCategoryField'] |
||
325 | ); |
||
326 | /* @TODO not sure what this even does |
||
327 | * if (empty($productData['category']) && $isVariant) { |
||
328 | * $category = $this->pullDataFromField( |
||
329 | * $productVariant->product, |
||
330 | * InstantAnalytics::$settings['productCategoryField'] |
||
331 | * ); |
||
332 | * } |
||
333 | */ |
||
334 | } |
||
335 | $eventItem->setItemCategory($category); |
||
336 | |||
337 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
338 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
339 | $brand = $this->pullDataFromField( |
||
340 | $productVariant, |
||
341 | InstantAnalytics::$settings['productBrandField'], |
||
342 | true |
||
343 | ); |
||
344 | /* @TODO not sure what this even does |
||
345 | * if (empty($productData['brand']) && $isVariant) { |
||
346 | * $brand = $this->pullDataFromField( |
||
347 | * $productVariant, |
||
348 | * InstantAnalytics::$settings['productBrandField'], |
||
349 | * true |
||
350 | * ); |
||
351 | * } |
||
352 | */ |
||
353 | $eventItem->setItemBrand($brand); |
||
354 | } |
||
355 | } |
||
356 | |||
357 | if ($index !== null) { |
||
358 | $eventItem->setIndex($index); |
||
359 | } |
||
360 | |||
361 | if (!empty($listName)) { |
||
362 | $eventItem->setItemListName($listName); |
||
363 | } |
||
364 | |||
365 | // Add item info to the event |
||
366 | $event->addItem($eventItem); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @param Product|Variant|null $productVariant |
||
371 | * @param string $fieldHandle |
||
372 | * @param bool $isBrand |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | 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 |