| Total Complexity | 73 |
| Total Lines | 477 |
| Duplicated Lines | 0 % |
| Changes | 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 | * Send analytics information for the completed order |
||
| 42 | * |
||
| 43 | * @param Order $order the Product or Variant |
||
| 44 | */ |
||
| 45 | public function orderComplete($order = null) |
||
| 46 | { |
||
| 47 | if ($order) { |
||
| 48 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
| 49 | 'Commerce', |
||
| 50 | 'Purchase', |
||
| 51 | $order->number, |
||
| 52 | $order->totalPrice |
||
| 53 | ); |
||
| 54 | |||
| 55 | if ($analytics) { |
||
| 56 | $this->addCommerceOrderToAnalytics($analytics, $order); |
||
| 57 | // Don't forget to set the product action, in this case to PURCHASE |
||
| 58 | $analytics->setProductActionToPurchase(); |
||
| 59 | $analytics->sendEvent(); |
||
| 60 | |||
| 61 | Craft::info(Craft::t( |
||
| 62 | 'instant-analytics', |
||
| 63 | 'orderComplete for `Commerce` - `Purchase` - `{number}` - `{price}`', |
||
| 64 | [ 'number' => $order->number, 'price' => $order->totalPrice ] |
||
| 65 | ), __METHOD__); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Send analytics information for the item added to the cart |
||
| 72 | * @param Order $order the Product or Variant |
||
| 73 | * @param LineItem $lineItem the line item that was added |
||
| 74 | */ |
||
| 75 | public function addToCart(/** @noinspection PhpUnusedParameterInspection */ $order = null, $lineItem = null) |
||
| 76 | { |
||
| 77 | if ($lineItem) { |
||
| 78 | $title = $lineItem->purchasable->title; |
||
| 79 | $quantity = $lineItem->qty; |
||
| 80 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics('Commerce', 'Add to Cart', $title, $quantity); |
||
| 81 | |||
| 82 | if ($analytics) { |
||
| 83 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
| 84 | $analytics->setEventLabel($title); |
||
| 85 | // Don't forget to set the product action, in this case to ADD |
||
| 86 | $analytics->setProductActionToAdd(); |
||
| 87 | $analytics->sendEvent(); |
||
| 88 | |||
| 89 | Craft::info(Craft::t( |
||
| 90 | 'instant-analytics', |
||
| 91 | 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', |
||
| 92 | [ 'title' => $title, 'quantity' => $quantity ] |
||
| 93 | ), __METHOD__); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Send analytics information for the item removed from the cart |
||
| 100 | * |
||
| 101 | * @param Order|null $order |
||
| 102 | * @param LineItem|null $lineItem |
||
| 103 | */ |
||
| 104 | public function removeFromCart(/** @noinspection PhpUnusedParameterInspection */ $order = null, $lineItem = null) |
||
| 105 | { |
||
| 106 | if ($lineItem) { |
||
| 107 | $title = $lineItem->purchasable->title; |
||
| 108 | $quantity = $lineItem->qty; |
||
| 109 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
| 110 | 'Commerce', |
||
| 111 | 'Remove from Cart', |
||
| 112 | $title, |
||
| 113 | $quantity |
||
| 114 | ); |
||
| 115 | |||
| 116 | if ($analytics) { |
||
| 117 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
| 118 | $analytics->setEventLabel($title); |
||
| 119 | // Don't forget to set the product action, in this case to ADD |
||
| 120 | $analytics->setProductActionToRemove(); |
||
| 121 | $analytics->sendEvent(); |
||
| 122 | |||
| 123 | Craft::info(Craft::t( |
||
| 124 | 'instant-analytics', |
||
| 125 | 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', |
||
| 126 | [ 'title' => $title, 'quantity' => $quantity ] |
||
| 127 | ), __METHOD__); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Add a Craft Commerce OrderModel to an Analytics object |
||
| 135 | * |
||
| 136 | * @param IAnalytics $analytics the Analytics object |
||
| 137 | * @param Order $order the Product or Variant |
||
| 138 | */ |
||
| 139 | public function addCommerceOrderToAnalytics($analytics = null, $order = null) |
||
| 140 | { |
||
| 141 | if ($order && $analytics) { |
||
| 142 | // First, include the transaction data |
||
| 143 | $analytics->setTransactionId($order->number) |
||
| 144 | ->setRevenue($order->totalPrice) |
||
| 145 | ->setTax($order->getAdjustmentsTotalByType('tax', true)) |
||
| 146 | ->setShipping($order->getAdjustmentsTotalByType('shipping', true)); |
||
| 147 | |||
| 148 | // Coupon code? |
||
| 149 | if ($order->couponCode) { |
||
| 150 | $analytics->setCouponCode($order->couponCode); |
||
| 151 | } |
||
| 152 | |||
| 153 | // Add each line item in the transaction |
||
| 154 | // Two cases - variant and non variant products |
||
| 155 | $index = 1; |
||
| 156 | |||
| 157 | foreach ($order->lineItems as $key => $lineItem) { |
||
| 158 | $this->addProductDataFromLineItem($analytics, $lineItem, $index, ''); |
||
| 159 | $index++; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Add a Craft Commerce LineItem to an Analytics object |
||
| 166 | * |
||
| 167 | * @param IAnalytics|null $analytics |
||
| 168 | * @param LineItem|null $lineItem |
||
| 169 | * @param int $index |
||
| 170 | * @param string $listName |
||
| 171 | * |
||
| 172 | * @return string the title of the product |
||
| 173 | */ |
||
| 174 | public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = ''): string |
||
| 175 | { |
||
| 176 | $result = ''; |
||
| 177 | if ($lineItem && $analytics) { |
||
| 178 | //This is the same for both variant and non variant products |
||
| 179 | $productData = [ |
||
| 180 | 'sku' => $lineItem->purchasable->sku, |
||
| 181 | 'price' => $lineItem->salePrice, |
||
| 182 | 'quantity' => $lineItem->qty, |
||
| 183 | ]; |
||
| 184 | |||
| 185 | if (isset($lineItem->purchasable->product)) { |
||
| 186 | $productVariant = $lineItem->purchasable->product; |
||
| 187 | $hasVariants = $lineItem->purchasable->product->type->hasVariants ?? null; |
||
| 188 | if (!$hasVariants) { |
||
| 189 | //No variants (i.e. default variant) |
||
| 190 | $productData['name'] = $lineItem->purchasable->title; |
||
| 191 | $productData['category'] = $lineItem->purchasable->product->type['name']; |
||
| 192 | } else { |
||
| 193 | // Product with variants |
||
| 194 | $productData['name'] = $lineItem->purchasable->product->title; |
||
| 195 | $productData['category'] = $lineItem->purchasable->product->type['name']; |
||
| 196 | $productData['variant'] = $lineItem->purchasable->title; |
||
| 197 | } |
||
| 198 | } else { |
||
| 199 | $productVariant = $lineItem->purchasable; |
||
| 200 | $productData['name'] = $lineItem->purchasable->title; |
||
| 201 | } |
||
| 202 | |||
| 203 | $result = $productData['name']; |
||
| 204 | |||
| 205 | if ($index) { |
||
| 206 | $productData['position'] = $index; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($listName) { |
||
| 210 | $productData['list'] = $listName; |
||
| 211 | } |
||
| 212 | |||
| 213 | $settings = InstantAnalytics::$plugin->getSettings(); |
||
| 214 | |||
| 215 | if ($settings) { |
||
| 216 | if (isset($settings['productCategoryField']) && !empty($settings['productCategoryField'])) { |
||
| 217 | $productData['category'] = $this->pullDataFromField( |
||
| 218 | $productVariant, |
||
| 219 | $settings['productCategoryField'] |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | |||
| 223 | if (isset($settings['productBrandField']) && !empty($settings['productBrandField'])) { |
||
| 224 | $productData['brand'] = $this->pullDataFromField( |
||
| 225 | $productVariant, |
||
| 226 | $settings['productBrandField'] |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | //Add each product to the hit to be sent |
||
| 232 | $analytics->addProduct($productData); |
||
| 233 | } |
||
| 234 | |||
| 235 | return $result; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Add a product impression from a Craft Commerce Product or Variant |
||
| 240 | * |
||
| 241 | * @param IAnalytics $analytics the Analytics object |
||
| 242 | * @param Product|Variant $productVariant the Product or Variant |
||
| 243 | * @param int $index Where the product appears in the list |
||
| 244 | * @param string $listName |
||
| 245 | * @param int $listIndex |
||
| 246 | * |
||
| 247 | * @throws \yii\base\InvalidConfigException |
||
| 248 | */ |
||
| 249 | public function addCommerceProductImpression( |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Add a product detail view from a Craft Commerce Product or Variant |
||
| 283 | * |
||
| 284 | * @param IAnalytics $analytics the Analytics object |
||
| 285 | * @param Product|Variant $productVariant the Product or Variant |
||
| 286 | * |
||
| 287 | * @throws \yii\base\InvalidConfigException |
||
| 288 | */ |
||
| 289 | public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Add a checkout step and option to an Analytics object |
||
| 310 | * |
||
| 311 | * @param IAnalytics $analytics the Analytics object |
||
| 312 | * @param Order $order the Product or Variant |
||
| 313 | * @param int $step the checkout step |
||
| 314 | * @param string $option the checkout option |
||
| 315 | */ |
||
| 316 | public function addCommerceCheckoutStep($analytics = null, $order = null, $step = 1, $option = '') |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Extract product data from a Craft Commerce Product or Variant |
||
| 347 | * |
||
| 348 | * @param Product|Variant $productVariant the Product or Variant |
||
| 349 | * |
||
| 350 | * @return array the product data |
||
| 351 | * @throws \yii\base\InvalidConfigException |
||
| 352 | */ |
||
| 353 | public function getProductDataFromProduct($productVariant = null): array |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param Product|Variant|null $productVariant |
||
| 454 | * @param string $fieldHandle |
||
| 455 | * @param bool $isBrand |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | private function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
| 514 |