Total Complexity | 56 |
Total Lines | 419 |
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 |
||
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) |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Add a product impression from a Craft Commerce Product or Variant |
||
131 | * |
||
132 | * @param Product|Variant|null $productVariant the Product or Variant |
||
133 | * @throws InvalidConfigException |
||
134 | */ |
||
135 | public function addCommerceProductImpression($productVariant): void |
||
136 | { |
||
137 | if ($productVariant) { |
||
138 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
||
139 | $this->addProductDataFromProductOrVariant($event, $productVariant); |
||
140 | |||
141 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
142 | |||
143 | $sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
||
144 | $name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
||
145 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
146 | 'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
||
147 | ['sku' => $sku, 'name' => $name], |
||
148 | __METHOD__ |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Add a product list impression from a Craft Commerce Product or Variant list |
||
155 | * |
||
156 | * @param Product[]|Variant[] $products |
||
157 | * @param string $listName |
||
158 | */ |
||
159 | public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
||
173 | ); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Add a Craft Commerce OrderModel to a Purchase Event |
||
179 | * |
||
180 | * @param PurchaseEvent $event The PurchaseEvent |
||
181 | * @param Order $order |
||
182 | */ |
||
183 | protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
||
184 | { |
||
185 | // First, include the transaction data |
||
186 | $event->setCurrency($order->getPaymentCurrency()) |
||
187 | ->setTransactionId($order->reference) |
||
188 | ->setValue($order->getTotalPrice()) |
||
189 | ->setTax($order->getTotalTax()) |
||
190 | ->setShipping($order->getTotalShippingCost()); |
||
191 | |||
192 | // Coupon code |
||
193 | if ($order->couponCode) { |
||
194 | $event->setCoupon($order->couponCode); |
||
195 | } |
||
196 | |||
197 | // Add each line item in the transaction |
||
198 | // Two cases - variant and non variant products |
||
199 | $index = 1; |
||
200 | |||
201 | foreach ($order->lineItems as $lineItem) { |
||
202 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
203 | $index++; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Add a Craft Commerce LineItem to an Analytics object |
||
209 | * |
||
210 | * @param ItemBaseEvent $event |
||
211 | * @param LineItem $lineItem |
||
212 | * @param int $index |
||
213 | * @param string $listName |
||
214 | * |
||
215 | * @return string the title of the product |
||
216 | * @throws InvalidConfigException |
||
217 | */ |
||
218 | protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
||
219 | { |
||
220 | $eventItem = $this->getNewItemParameter(); |
||
221 | |||
222 | $product = null; |
||
223 | $purchasable = $lineItem->purchasable; |
||
224 | |||
225 | /** @phpstan-ignore-next-line */ |
||
226 | if ($purchasable === null) { |
||
227 | $eventItem->setItemName($lineItem->getDescription()); |
||
228 | $eventItem->setItemId($lineItem->getSku()); |
||
229 | } else { |
||
230 | $eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
||
231 | $eventItem->setItemId($purchasable->getSku()); |
||
232 | } |
||
233 | $eventItem->setPrice($lineItem->salePrice); |
||
234 | $eventItem->setQuantity($lineItem->qty); |
||
235 | |||
236 | // Handle this purchasable being a Variant |
||
237 | if (is_a($purchasable, Variant::class)) { |
||
238 | /** @var Variant $purchasable */ |
||
239 | $product = $purchasable->getProduct(); |
||
240 | $variant = $purchasable; |
||
241 | // Product with variants |
||
242 | $eventItem->setItemName($product->title); |
||
243 | $eventItem->setItemVariant($variant->title); |
||
244 | $eventItem->setItemCategory($product->getType()); |
||
245 | } |
||
246 | |||
247 | // Handle this purchasable being a Product |
||
248 | if (is_a($purchasable, Product::class)) { |
||
249 | /** @var Product $purchasable */ |
||
250 | $product = $purchasable; |
||
251 | $eventItem->setItemName($product->title); |
||
252 | $eventItem->setItemVariant($product->title); |
||
253 | $eventItem->setItemCategory($product->getType()); |
||
254 | } |
||
255 | |||
256 | // Handle product lists |
||
257 | if ($index) { |
||
258 | $eventItem->setIndex($index); |
||
259 | } |
||
260 | |||
261 | if ($listName) { |
||
262 | $eventItem->setItemListName($listName); |
||
263 | } |
||
264 | |||
265 | // Add in any custom categories/brands that might be set |
||
266 | if (InstantAnalytics::$settings && $product) { |
||
267 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
268 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
269 | $category = $this->pullDataFromField( |
||
270 | $product, |
||
271 | InstantAnalytics::$settings['productCategoryField'] |
||
272 | ); |
||
273 | $eventItem->setItemCategory($category); |
||
274 | } |
||
275 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
276 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
277 | $brand = $this->pullDataFromField( |
||
278 | $product, |
||
279 | InstantAnalytics::$settings['productBrandField'] |
||
280 | ); |
||
281 | |||
282 | $eventItem->setItemBrand($brand); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | //Add each product to the hit to be sent |
||
287 | $event->addItem($eventItem); |
||
288 | |||
289 | return $eventItem->getItemName(); |
||
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((float)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 | /* -- @TODO unclear what this even does |
||
328 | if (empty($productData['category']) && $isVariant) { |
||
329 | $category = $this->pullDataFromField( |
||
330 | $productVariant->product, |
||
331 | InstantAnalytics::$settings['productCategoryField'] |
||
332 | ); |
||
333 | } |
||
334 | */ |
||
335 | } |
||
336 | $eventItem->setItemCategory($category); |
||
337 | |||
338 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
339 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
340 | $brand = $this->pullDataFromField( |
||
341 | $productVariant, |
||
342 | InstantAnalytics::$settings['productBrandField'], |
||
343 | true |
||
344 | ); |
||
345 | /* -- @TODO unclear what this even does |
||
346 | if (empty($productData['brand']) && $isVariant) { |
||
347 | $brand = $this->pullDataFromField( |
||
348 | $productVariant, |
||
349 | InstantAnalytics::$settings['productBrandField'], |
||
350 | true |
||
351 | ); |
||
352 | } |
||
353 | */ |
||
354 | $eventItem->setItemBrand($brand); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | if ($index !== null) { |
||
359 | $eventItem->setIndex($index); |
||
360 | } |
||
361 | |||
362 | if (!empty($listName)) { |
||
363 | $eventItem->setItemListName($listName); |
||
364 | } |
||
365 | |||
366 | // Add item info to the event |
||
367 | $event->addItem($eventItem); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param Product|Variant|null $productVariant |
||
372 | * @param string $fieldHandle |
||
373 | * @param bool $isBrand |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @param bool $isBrand |
||
411 | * @param array $elements |
||
412 | * @return string |
||
413 | */ |
||
414 | protected function getDataFromElements(bool $isBrand, array $elements): string |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Create an item parameter and set affiliation on it, if any exists. |
||
446 | * |
||
447 | * @return ItemParameter |
||
448 | */ |
||
449 | protected function getNewItemParameter(): ItemParameter |
||
456 | } |
||
457 | } |
||
458 |