| Total Complexity | 75 |
| Total Lines | 412 |
| 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 | * @param IAnalytics $analytics the Analytics object |
||
| 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("Commerce", "Purchase", $order->number, $order->totalPrice); |
||
| 49 | |||
| 50 | if ($analytics) { |
||
| 51 | $this->addCommerceOrderToAnalytics($analytics, $order); |
||
| 52 | // Don't forget to set the product action, in this case to PURCHASE |
||
| 53 | $analytics->setProductActionToPurchase(); |
||
| 54 | $analytics->sendEvent(); |
||
| 55 | |||
| 56 | Craft::info(Craft::t('instant-analytics', 'orderComplete for `Commerce` - `Purchase` - `{number}` - `{price}`', [ 'number' => $order->number, 'price' => $order->totalPrice ]), __METHOD__); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Send analytics information for the item added to the cart |
||
| 63 | * @param Order $order the Product or Variant |
||
| 64 | * @param LineItem $lineItem the line item that was added |
||
| 65 | */ |
||
| 66 | public function addToCart($order = null, $lineItem = null) |
||
| 67 | { |
||
| 68 | if ($lineItem) { |
||
| 69 | $title = $lineItem->purchasable->title; |
||
| 70 | $quantity = $lineItem->qty; |
||
| 71 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Add to Cart", $title, $quantity); |
||
| 72 | |||
| 73 | if ($analytics) { |
||
| 74 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
| 75 | $analytics->setEventLabel($title); |
||
| 76 | // Don't forget to set the product action, in this case to ADD |
||
| 77 | $analytics->setProductActionToAdd(); |
||
| 78 | $analytics->sendEvent(); |
||
| 79 | |||
| 80 | Craft::info(Craft::t('instant-analytics', 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', [ 'title' => $title, 'quantity' => $quantity ]), __METHOD__); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Send analytics information for the item removed from the cart |
||
| 87 | */ |
||
| 88 | public function removeFromCart($order = null, $lineItem = null) |
||
| 89 | { |
||
| 90 | if ($lineItem) { |
||
| 91 | $title = $lineItem->purchasable->title; |
||
| 92 | $quantity = $lineItem->qty; |
||
| 93 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Remove from Cart", $title, $quantity); |
||
| 94 | |||
| 95 | if ($analytics) { |
||
| 96 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
| 97 | $analytics->setEventLabel($title); |
||
| 98 | // Don't forget to set the product action, in this case to ADD |
||
| 99 | $analytics->setProductActionToRemove(); |
||
| 100 | $analytics->sendEvent(); |
||
| 101 | |||
| 102 | Craft::info(Craft::t('instant-analytics', 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', [ 'title' => $title, 'quantity' => $quantity ]), __METHOD__); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Add a Craft Commerce OrderModel to an Analytics object |
||
| 110 | * @param IAnalytics $analytics the Analytics object |
||
| 111 | * @param Order $orderModel the Product or Variant |
||
| 112 | */ |
||
| 113 | public function addCommerceOrderToAnalytics($analytics = null, $order = null) |
||
| 114 | { |
||
| 115 | if ($order && $analytics) { |
||
| 116 | // First, include the transaction data |
||
| 117 | $analytics->setTransactionId($order->number) |
||
| 118 | ->setRevenue($order->totalPrice) |
||
| 119 | ->setTax($order->totalTax) |
||
| 120 | ->setShipping($order->totalShippingCost); |
||
| 121 | |||
| 122 | // Coupon code? |
||
| 123 | if ($order->couponCode) { |
||
| 124 | $analytics->setCouponCode($order->couponCode); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Add each line item in the transaction |
||
| 128 | // Two cases - variant and non variant products |
||
| 129 | $index = 1; |
||
| 130 | |||
| 131 | foreach ($order->lineItems as $key => $lineItem) { |
||
| 132 | $this->addProductDataFromLineItem($analytics, $lineItem, $index, ""); |
||
| 133 | $index++; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Add a Craft Commerce LineItem to an Analytics object |
||
| 140 | * @return string the title of the product |
||
| 141 | */ |
||
| 142 | public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = "") |
||
| 143 | { |
||
| 144 | $result = ""; |
||
| 145 | if ($lineItem) { |
||
| 146 | if ($analytics) { |
||
| 147 | //This is the same for both variant and non variant products |
||
| 148 | $productData = [ |
||
| 149 | 'sku' => $lineItem->purchasable->sku, |
||
| 150 | 'price' => $lineItem->salePrice, |
||
| 151 | 'quantity' => $lineItem->qty, |
||
| 152 | ]; |
||
| 153 | |||
| 154 | if (isset($lineItem->purchasable->product)) { |
||
| 155 | $productVariant = $lineItem->purchasable->product; |
||
| 156 | |||
| 157 | if (!$lineItem->purchasable->product->type->hasVariants) { |
||
| 158 | //No variants (i.e. default variant) |
||
| 159 | $productData['name'] = $lineItem->purchasable->title; |
||
| 160 | $productData['category'] = $lineItem->purchasable->product->type['name']; |
||
| 161 | } else { |
||
| 162 | // Product with variants |
||
| 163 | $productData['name'] = $lineItem->purchasable->product->title; |
||
| 164 | $productData['category'] = $lineItem->purchasable->product->type['name']; |
||
| 165 | $productData['variant'] = $lineItem->purchasable->title; |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | $productVariant = $lineItem->purchasable; |
||
| 169 | $productData['name'] = $lineItem->purchasable->title; |
||
| 170 | $productData['category'] = $lineItem->purchasable->type->name; |
||
| 171 | } |
||
| 172 | |||
| 173 | $result = $productData['name']; |
||
| 174 | |||
| 175 | if ($index) { |
||
| 176 | $productData['position'] = $index; |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($listName) { |
||
| 180 | $productData['list'] = $listName; |
||
| 181 | } |
||
| 182 | |||
| 183 | $settings = InstantAnalytics::$plugin->getSettings(); |
||
| 184 | |||
| 185 | if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") { |
||
| 186 | $productData['category'] = $this->_pullDataFromField($productVariant, $settings['productCategoryField']); |
||
| 187 | } |
||
| 188 | |||
| 189 | if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") { |
||
| 190 | $productData['brand'] = $this->_pullDataFromField($productVariant, $settings['productBrandField']); |
||
| 191 | } |
||
| 192 | |||
| 193 | //Add each product to the hit to be sent |
||
| 194 | $analytics->addProduct($productData); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return $result; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add a product impression from a Craft Commerce Product or Variant |
||
| 203 | * @param IAnalytics $analytics the Analytics object |
||
| 204 | * @param Commerce_ProductModel or Commerce_VariantModel $productVariant the Product or Variant |
||
| 205 | * @param int $index Where the product appears in the list |
||
| 206 | */ |
||
| 207 | public function addCommerceProductImpression($analytics = null, $productVariant = null, $index = 0, $listName = "default", $listIndex = 1) |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Add a product detail view from a Craft Commerce Product or Variant |
||
| 232 | * @param IAnalytics $analytics the Analytics object |
||
| 233 | * @param Product or Variant $productVariant the Product or Variant |
||
| 234 | */ |
||
| 235 | public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Add a checkout step and option to an Analytics object |
||
| 252 | * @param IAnalytics $analytics the Analytics object |
||
| 253 | * @param Commerce_OrderModel $orderModel the Product or Variant |
||
| 254 | * @param int $step the checkout step |
||
| 255 | * @param string $option the checkout option |
||
| 256 | */ |
||
| 257 | public function addCommerceCheckoutStep($analytics = null, $orderModel = null, $step = 1, $option = "") |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Extract product data from a Craft Commerce Product or Variant |
||
| 284 | * @param Commerce_ProductModel|Commerce_VariantModel $productVariant the Product or Variant |
||
| 285 | * @return array the product data |
||
| 286 | */ |
||
| 287 | public function getProductDataFromProduct($productVariant = null) |
||
| 376 | } |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Extract the value of a field |
||
| 381 | * @param Commerce_OrderModel $orderModel the Product or Variant |
||
| 382 | * @param Commerce_LineItemModel $lineItem the line item that was added |
||
| 383 | * @param boolean $isBrand Are we getting the brand? |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | private function _pullDataFromField($productVariant, $fieldHandle, $isBrand = false) |
||
| 450 |