Total Complexity | 59 |
Total Lines | 409 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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) |
||
68 | { |
||
69 | if ($order) { |
||
70 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->BeginCheckoutEvent(); |
||
71 | // First, include the transaction data |
||
72 | $event->setCurrency($order->getPaymentCurrency()) |
||
73 | ->setValue($order->getTotalPrice()); |
||
74 | |||
75 | // Add each line item in the cart |
||
76 | $index = 1; |
||
77 | foreach ($order->lineItems as $lineItem) { |
||
78 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
79 | $index++; |
||
80 | } |
||
81 | |||
82 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
83 | |||
84 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
85 | 'Adding `Commerce - Begin Checkout event``', |
||
86 | [], |
||
87 | __METHOD__ |
||
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 | /** |
||
130 | * Add a Craft Commerce OrderModel to a Purchase Event |
||
131 | * |
||
132 | * @param PurchaseEvent $event The PurchaseEvent |
||
133 | * @param Order $order |
||
134 | */ |
||
135 | protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
||
136 | { |
||
137 | // First, include the transaction data |
||
138 | $event->setCurrency($order->getPaymentCurrency()) |
||
139 | ->setTransactionId($order->reference) |
||
140 | ->setValue($order->getTotalPrice()) |
||
141 | ->setTax($order->getTotalTax()) |
||
142 | ->setShipping($order->getTotalShippingCost()); |
||
143 | |||
144 | // Coupon code |
||
145 | if ($order->couponCode) { |
||
146 | $event->setCoupon($order->couponCode); |
||
147 | } |
||
148 | |||
149 | // Add each line item in the transaction |
||
150 | // Two cases - variant and non variant products |
||
151 | $index = 1; |
||
152 | |||
153 | foreach ($order->lineItems as $lineItem) { |
||
154 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
155 | $index++; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Add a Craft Commerce LineItem to an Analytics object |
||
161 | * |
||
162 | * @param ItemBaseEvent $event |
||
163 | * @param LineItem $lineItem |
||
164 | * @param int $index |
||
165 | * @param string $listName |
||
166 | * |
||
167 | * @return string the title of the product |
||
168 | * @throws InvalidConfigException |
||
169 | */ |
||
170 | protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
||
171 | { |
||
172 | $eventItem = $this->getNewItemParameter(); |
||
173 | |||
174 | $product = null; |
||
175 | $purchasable = $lineItem->purchasable; |
||
176 | |||
177 | $eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
||
178 | $eventItem->setItemId($purchasable->getSku() ?? $lineItem->getSku()); |
||
179 | $eventItem->setPrice($lineItem->salePrice); |
||
180 | $eventItem->setQuantity($lineItem->qty); |
||
181 | |||
182 | // Handle this purchasable being a Variant |
||
183 | if (is_a($purchasable, Variant::class)) { |
||
184 | /** @var Variant $purchasable */ |
||
185 | $product = $purchasable->getProduct(); |
||
186 | $variant = $purchasable; |
||
187 | // Product with variants |
||
188 | $eventItem->setItemName($product->title); |
||
189 | $eventItem->setItemVariant($variant->title); |
||
190 | $eventItem->setItemCategory($product->getType()); |
||
191 | } |
||
192 | |||
193 | // Handle this purchasable being a Product |
||
194 | if (is_a($purchasable, Product::class)) { |
||
195 | /** @var Product $purchasable */ |
||
196 | $product = $purchasable; |
||
197 | $eventItem->setItemName($product->title); |
||
198 | $eventItem->setItemVariant($product->title); |
||
199 | $eventItem->setItemCategory($product->getType()); |
||
200 | } |
||
201 | |||
202 | // Handle product lists |
||
203 | if ($index) { |
||
204 | $eventItem->setIndex($index); |
||
205 | } |
||
206 | |||
207 | if ($listName) { |
||
208 | $eventItem->setItemListName($listName); |
||
209 | } |
||
210 | |||
211 | // Add in any custom categories/brands that might be set |
||
212 | if (InstantAnalytics::$settings && $product) { |
||
213 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
214 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
215 | $category = $this->pullDataFromField( |
||
216 | $product, |
||
217 | InstantAnalytics::$settings['productCategoryField'] |
||
218 | ); |
||
219 | $eventItem->setItemCategory($category); |
||
220 | } |
||
221 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
222 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
223 | $brand = $this->pullDataFromField( |
||
224 | $product, |
||
225 | InstantAnalytics::$settings['productBrandField'] |
||
226 | ); |
||
227 | |||
228 | $eventItem->setItemBrand($brand); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | //Add each product to the hit to be sent |
||
233 | $event->addItem($eventItem); |
||
234 | |||
235 | return $eventItem->getItemName(); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Add a product impression from a Craft Commerce Product or Variant |
||
240 | * |
||
241 | * @param Product|Variant $productVariant the Product or Variant |
||
242 | * @throws InvalidConfigException |
||
243 | */ |
||
244 | public function addCommerceProductImpression($productVariant): void |
||
245 | { |
||
246 | if ($productVariant) { |
||
247 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
||
248 | $this->addProductDataFromProductOrVariant($event, $productVariant); |
||
249 | |||
250 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
251 | |||
252 | $sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
||
253 | $name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
||
254 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
255 | 'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
||
256 | ['sku' => $sku, 'name' => $name], |
||
257 | __METHOD__ |
||
258 | ); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Add a product list impression from a Craft Commerce Product or Variant list |
||
264 | * |
||
265 | * @param Product[]|Variant[] $products |
||
266 | * @param string $listName |
||
267 | */ |
||
268 | public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
||
282 | ); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Extract product data from a Craft Commerce Product or Variant |
||
288 | * |
||
289 | * @param Product|Variant|null $productVariant the Product or Variant |
||
290 | * |
||
291 | * @throws InvalidConfigException |
||
292 | */ |
||
293 | protected function addProductDataFromProductOrVariant(ItemBaseEvent $event, $productVariant = null, $index = null, $listName = ''): void |
||
294 | { |
||
295 | if ($productVariant === null) { |
||
296 | return; |
||
297 | } |
||
298 | |||
299 | $eventItem = $this->getNewItemParameter(); |
||
300 | |||
301 | $isVariant = $productVariant instanceof Variant; |
||
302 | $variant = $isVariant ? $productVariant : $productVariant->getDefaultVariant(); |
||
303 | |||
304 | if (!$variant) { |
||
305 | return; |
||
306 | } |
||
307 | |||
308 | $eventItem->setItemId($variant->sku); |
||
309 | $eventItem->setItemName($variant->title); |
||
310 | $eventItem->setPrice(number_format($variant->price, 2, '.', '')); |
||
311 | |||
312 | $category = ($isVariant ? $variant->getProduct() : $productVariant)->getType()['name']; |
||
313 | |||
314 | if (InstantAnalytics::$settings) { |
||
315 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
316 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
317 | $category = $this->pullDataFromField( |
||
318 | $productVariant, |
||
319 | InstantAnalytics::$settings['productCategoryField'] |
||
320 | ); |
||
321 | if (empty($productData['category']) && $isVariant) { |
||
322 | $category = $this->pullDataFromField( |
||
323 | $productVariant->product, |
||
324 | InstantAnalytics::$settings['productCategoryField'] |
||
325 | ); |
||
326 | } |
||
327 | } |
||
328 | $eventItem->setItemCategory($category); |
||
329 | |||
330 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
331 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
332 | $brand = $this->pullDataFromField( |
||
333 | $productVariant, |
||
334 | InstantAnalytics::$settings['productBrandField'], |
||
335 | true |
||
336 | ); |
||
337 | |||
338 | if (empty($productData['brand']) && $isVariant) { |
||
339 | $brand = $this->pullDataFromField( |
||
340 | $productVariant, |
||
341 | InstantAnalytics::$settings['productBrandField'], |
||
342 | true |
||
343 | ); |
||
344 | } |
||
345 | $eventItem->setItemBrand($brand); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | if ($index !== null) { |
||
350 | $eventItem->setIndex($index); |
||
351 | } |
||
352 | |||
353 | if (!empty($listName)) { |
||
354 | $eventItem->setItemListName($listName); |
||
355 | } |
||
356 | |||
357 | // Add item info to the event |
||
358 | $event->addItem($eventItem); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param Product|Variant|null $productVariant |
||
363 | * @param string $fieldHandle |
||
364 | * @param bool $isBrand |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @param bool $isBrand |
||
402 | * @param array $elements |
||
403 | * @return string |
||
404 | */ |
||
405 | protected function getDataFromElements(bool $isBrand, array $elements): string |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Create an item parameter and set affiliation on it, if any exists. |
||
437 | * |
||
438 | * @return ItemParameter |
||
439 | */ |
||
440 | protected function getNewItemParameter(): ItemParameter |
||
445 | } |
||
446 | } |
||
447 |