| Total Complexity | 53 |
| Total Lines | 372 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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) |
||
| 46 | { |
||
| 47 | if ($order) { |
||
| 48 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->PurchaseEvent(); |
||
| 49 | $this->addCommerceOrderToEvent($event, $order); |
||
| 50 | |||
| 51 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 52 | |||
| 53 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 54 | 'Adding `Commerce - Order Complete event`: `{reference}` => `{price}`', |
||
| 55 | ['reference' => $order->reference, 'price' => $order->totalPrice], |
||
| 56 | __METHOD__ |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Send analytics information for the item added to the cart |
||
| 63 | * |
||
| 64 | * @param LineItem $lineItem the line item that was added |
||
| 65 | */ |
||
| 66 | public function triggerAddToCartEvent(LineItem $lineItem): void |
||
| 67 | { |
||
| 68 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->AddToCartEvent(); |
||
| 69 | $this->addProductDataFromLineItem($event, $lineItem); |
||
| 70 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 71 | |||
| 72 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 73 | 'Adding `Commerce - Add to Cart event`: `{title}` => `{quantity}`', |
||
| 74 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
| 75 | __METHOD__ |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Send analytics information for the item removed from the cart |
||
| 81 | * |
||
| 82 | * @param LineItem $lineItem |
||
| 83 | */ |
||
| 84 | public function triggerRemoveFromCartEvent(LineItem $lineItem) |
||
| 85 | { |
||
| 86 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->RemoveFromCartEvent(); |
||
| 87 | $this->addProductDataFromLineItem($event, $lineItem); |
||
| 88 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 89 | |||
| 90 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 91 | 'Adding `Commerce - Remove from Cart event`: `{title}` => `{quantity}`', |
||
| 92 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
| 93 | __METHOD__ |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Add a Craft Commerce OrderModel to a Purchase Event |
||
| 100 | * |
||
| 101 | * @param PurchaseEvent $event The PurchaseEvent |
||
| 102 | * @param Order $order |
||
| 103 | */ |
||
| 104 | protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
||
| 105 | { |
||
| 106 | // First, include the transaction data |
||
| 107 | $event->setCurrency($order->getPaymentCurrency()) |
||
| 108 | ->setTransactionId($order->reference) |
||
| 109 | ->setValue($order->getTotalPrice()) |
||
| 110 | ->setTax($order->getTotalTax()) |
||
| 111 | ->setShipping($order->getTotalShippingCost()); |
||
| 112 | |||
| 113 | // Coupon code |
||
| 114 | if ($order->couponCode) { |
||
| 115 | $event->setCoupon($order->couponCode); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Add each line item in the transaction |
||
| 119 | // Two cases - variant and non variant products |
||
| 120 | $index = 1; |
||
| 121 | |||
| 122 | foreach ($order->lineItems as $lineItem) { |
||
| 123 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
| 124 | $index++; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Add a Craft Commerce LineItem to an Analytics object |
||
| 130 | * |
||
| 131 | * @param ItemBaseEvent $event |
||
| 132 | * @param LineItem $lineItem |
||
| 133 | * @param int $index |
||
| 134 | * @param string $listName |
||
| 135 | * |
||
| 136 | * @return string the title of the product |
||
| 137 | * @throws \yii\base\InvalidConfigException |
||
| 138 | */ |
||
| 139 | protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
||
| 140 | { |
||
| 141 | $eventItem = $this->getNewItemParameter(); |
||
| 142 | |||
| 143 | $product = null; |
||
| 144 | $purchasable = $lineItem->purchasable; |
||
| 145 | |||
| 146 | $eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
||
| 147 | $eventItem->setItemId($purchasable->getSku() ?? $lineItem->getSku()); |
||
| 148 | $eventItem->setPrice($lineItem->salePrice); |
||
| 149 | $eventItem->setQuantity($lineItem->qty); |
||
| 150 | |||
| 151 | // Handle this purchasable being a Variant |
||
| 152 | if (is_a($purchasable, Variant::class)) { |
||
| 153 | /** @var Variant $purchasable */ |
||
| 154 | $product = $purchasable->getProduct(); |
||
| 155 | $variant = $purchasable; |
||
| 156 | // Product with variants |
||
| 157 | $eventItem->setItemName($product->title); |
||
| 158 | $eventItem->setItemVariant($variant->title); |
||
| 159 | $eventItem->setItemCategory($product->getType()); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Handle this purchasable being a Product |
||
| 163 | if (is_a($purchasable, Product::class)) { |
||
| 164 | /** @var Product $purchasable */ |
||
| 165 | $product = $purchasable; |
||
| 166 | $eventItem->setItemName($product->title); |
||
| 167 | $eventItem->setItemVariant($product->title); |
||
| 168 | $eventItem->setItemCategory($product->getType()); |
||
| 169 | } |
||
| 170 | |||
| 171 | // Handle product lists |
||
| 172 | if ($index) { |
||
| 173 | $eventItem->setIndex($index); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($listName) { |
||
| 177 | $eventItem->setItemListName($listName); |
||
| 178 | } |
||
| 179 | |||
| 180 | // Add in any custom categories/brands that might be set |
||
| 181 | if (InstantAnalytics::$settings && $product) { |
||
| 182 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
| 183 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
| 184 | $category = $this->pullDataFromField( |
||
| 185 | $product, |
||
| 186 | InstantAnalytics::$settings['productCategoryField'] |
||
| 187 | ); |
||
| 188 | $eventItem->setItemCategory($category); |
||
| 189 | } |
||
| 190 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
| 191 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
| 192 | $brand = $this->pullDataFromField( |
||
| 193 | $product, |
||
| 194 | InstantAnalytics::$settings['productBrandField'] |
||
| 195 | ); |
||
| 196 | |||
| 197 | $eventItem->setItemBrand($brand); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | //Add each product to the hit to be sent |
||
| 202 | $event->addItem($eventItem); |
||
| 203 | |||
| 204 | return $eventItem->getItemName(); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Add a product impression from a Craft Commerce Product or Variant |
||
| 209 | * |
||
| 210 | * @param Product|Variant $productVariant the Product or Variant |
||
| 211 | * @param int $index |
||
| 212 | * @param string $listName |
||
| 213 | * @throws \yii\base\InvalidConfigException |
||
| 214 | */ |
||
| 215 | public function addCommerceProductImpression(Variant|Product $productVariant, int $index = 0, string $listName = 'default'): void |
||
| 216 | { |
||
| 217 | if ($productVariant) { |
||
| 218 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
||
| 219 | $this->addProductDataFromProductOrVariant($event, $productVariant, $index, $listName); |
||
| 220 | |||
| 221 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 222 | |||
| 223 | $sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
||
| 224 | $name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
||
| 225 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 226 | 'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
||
| 227 | ['sku' => $sku, 'name' => $name, 'index' => $index], |
||
| 228 | __METHOD__ |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Add a product list impression from a Craft Commerce Product or Variant list |
||
| 235 | * |
||
| 236 | * @param Product[]|Variant[] $products |
||
| 237 | * @param string $listName |
||
| 238 | */ |
||
| 239 | public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Extract product data from a Craft Commerce Product or Variant |
||
| 259 | * |
||
| 260 | * @param Product|Variant $productVariant the Product or Variant |
||
| 261 | * |
||
| 262 | * @throws \yii\base\InvalidConfigException |
||
| 263 | */ |
||
| 264 | protected function addProductDataFromProductOrVariant(ItemBaseEvent $event, $productVariant = null, $index = 0, $listName = 'default'): void |
||
| 265 | { |
||
| 266 | $eventItem = $this->getNewItemParameter(); |
||
| 267 | |||
| 268 | $isVariant = $productVariant instanceof Variant; |
||
| 269 | $variant = $isVariant ? $productVariant : $productVariant->getDefaultVariant(); |
||
| 270 | |||
| 271 | if (!$variant) { |
||
| 272 | return; |
||
| 273 | } |
||
| 274 | |||
| 275 | $eventItem->setItemId($variant->sku); |
||
| 276 | $eventItem->setItemName($variant->title); |
||
| 277 | $eventItem->setPrice(number_format($variant->price, 2, '.', '')); |
||
| 278 | |||
| 279 | $category = ($isVariant ? $variant->getProduct() : $productVariant)->getType()['name']; |
||
| 280 | |||
| 281 | if (InstantAnalytics::$settings) { |
||
| 282 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
| 283 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
| 284 | $category = $this->pullDataFromField( |
||
| 285 | $productVariant, |
||
| 286 | InstantAnalytics::$settings['productCategoryField'] |
||
| 287 | ); |
||
| 288 | if (empty($productData['category']) && $isVariant) { |
||
| 289 | $category = $this->pullDataFromField( |
||
| 290 | $productVariant->product, |
||
| 291 | InstantAnalytics::$settings['productCategoryField'] |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | $eventItem->setItemCategory($category); |
||
| 296 | |||
| 297 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
| 298 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
| 299 | $brand = $this->pullDataFromField( |
||
| 300 | $productVariant, |
||
| 301 | InstantAnalytics::$settings['productBrandField'], |
||
| 302 | true |
||
| 303 | ); |
||
| 304 | |||
| 305 | if (empty($productData['brand']) && $isVariant) { |
||
| 306 | $brand = $this->pullDataFromField( |
||
| 307 | $productVariant, |
||
| 308 | InstantAnalytics::$settings['productBrandField'], |
||
| 309 | true |
||
| 310 | ); |
||
| 311 | } |
||
| 312 | $eventItem->setItemBrand($brand); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | $eventItem->setIndex($index); |
||
| 317 | $eventItem->setItemListName($listName); |
||
| 318 | |||
| 319 | // Add item info to the event |
||
| 320 | $event->addItem($eventItem); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param Product|Variant|null $productVariant |
||
| 325 | * @param string $fieldHandle |
||
| 326 | * @param bool $isBrand |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param bool $isBrand |
||
| 364 | * @param array $elements |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | protected function getDataFromElements(bool $isBrand, array $elements): string |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Create an item parameter and set affiliation on it, if any exists. |
||
| 399 | * |
||
| 400 | * @return ItemParameter |
||
| 401 | */ |
||
| 402 | protected function getNewItemParameter(): ItemParameter |
||
| 407 | } |
||
| 408 | } |
||
| 409 |