| Total Complexity | 71 |
| Total Lines | 466 |
| 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 |
||
| 36 | class Commerce extends Component |
||
| 37 | { |
||
| 38 | // Public Methods |
||
| 39 | // ========================================================================= |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Send analytics information for the completed order |
||
| 43 | * |
||
| 44 | * @param Order $order the Product or Variant |
||
| 45 | */ |
||
| 46 | public function orderComplete($order = null) |
||
| 47 | { |
||
| 48 | if ($order) { |
||
| 49 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
| 50 | 'Commerce', |
||
| 51 | 'Purchase', |
||
| 52 | $order->number, |
||
| 53 | $order->totalPrice |
||
| 54 | ); |
||
| 55 | |||
| 56 | if ($analytics) { |
||
| 57 | $this->addCommerceOrderToAnalytics($analytics, $order); |
||
| 58 | // Don't forget to set the product action, in this case to PURCHASE |
||
| 59 | $analytics->setProductActionToPurchase(); |
||
| 60 | $analytics->sendEvent(); |
||
| 61 | |||
| 62 | Craft::info(Craft::t( |
||
| 63 | 'instant-analytics', |
||
| 64 | 'orderComplete for `Commerce` - `Purchase` - `{number}` - `{price}`', |
||
| 65 | [ 'number' => $order->number, 'price' => $order->totalPrice ] |
||
| 66 | ), __METHOD__); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Send analytics information for the item added to the cart |
||
| 73 | * @param Order $order the Product or Variant |
||
| 74 | * @param LineItem $lineItem the line item that was added |
||
| 75 | */ |
||
| 76 | public function addToCart(/** @noinspection PhpUnusedParameterInspection */ $order = null, $lineItem = null) |
||
| 77 | { |
||
| 78 | if ($lineItem) { |
||
| 79 | $title = $lineItem->purchasable->title; |
||
| 80 | $quantity = $lineItem->qty; |
||
| 81 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics('Commerce', 'Add to Cart', $title, $quantity); |
||
| 82 | |||
| 83 | if ($analytics) { |
||
| 84 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
| 85 | $analytics->setEventLabel($title); |
||
| 86 | // Don't forget to set the product action, in this case to ADD |
||
| 87 | $analytics->setProductActionToAdd(); |
||
| 88 | $analytics->sendEvent(); |
||
| 89 | |||
| 90 | Craft::info(Craft::t( |
||
| 91 | 'instant-analytics', |
||
| 92 | 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', |
||
| 93 | [ 'title' => $title, 'quantity' => $quantity ] |
||
| 94 | ), __METHOD__); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Send analytics information for the item removed from the cart |
||
| 101 | * |
||
| 102 | * @param Order|null $order |
||
| 103 | * @param LineItem|null $lineItem |
||
| 104 | */ |
||
| 105 | public function removeFromCart(/** @noinspection PhpUnusedParameterInspection */ $order = null, $lineItem = null) |
||
| 106 | { |
||
| 107 | if ($lineItem) { |
||
| 108 | $title = $lineItem->purchasable->title; |
||
| 109 | $quantity = $lineItem->qty; |
||
| 110 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
| 111 | 'Commerce', |
||
| 112 | 'Remove from Cart', |
||
| 113 | $title, |
||
| 114 | $quantity |
||
| 115 | ); |
||
| 116 | |||
| 117 | if ($analytics) { |
||
| 118 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
| 119 | $analytics->setEventLabel($title); |
||
| 120 | // Don't forget to set the product action, in this case to ADD |
||
| 121 | $analytics->setProductActionToRemove(); |
||
| 122 | $analytics->sendEvent(); |
||
| 123 | |||
| 124 | Craft::info(Craft::t( |
||
| 125 | 'instant-analytics', |
||
| 126 | 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', |
||
| 127 | [ 'title' => $title, 'quantity' => $quantity ] |
||
| 128 | ), __METHOD__); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Add a Craft Commerce OrderModel to an Analytics object |
||
| 136 | * |
||
| 137 | * @param IAnalytics $analytics the Analytics object |
||
| 138 | * @param Order $order the Product or Variant |
||
| 139 | */ |
||
| 140 | public function addCommerceOrderToAnalytics($analytics = null, $order = null) |
||
| 141 | { |
||
| 142 | if ($order && $analytics) { |
||
| 143 | // First, include the transaction data |
||
| 144 | $analytics->setTransactionId($order->number) |
||
| 145 | ->setRevenue($order->totalPrice) |
||
| 146 | ->setTax($order->getAdjustmentsTotalByType('tax', true)) |
||
| 147 | ->setShipping($order->getAdjustmentsTotalByType('shipping', true)); |
||
| 148 | |||
| 149 | // Coupon code? |
||
| 150 | if ($order->couponCode) { |
||
| 151 | $analytics->setCouponCode($order->couponCode); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Add each line item in the transaction |
||
| 155 | // Two cases - variant and non variant products |
||
| 156 | $index = 1; |
||
| 157 | |||
| 158 | foreach ($order->lineItems as $key => $lineItem) { |
||
| 159 | $this->addProductDataFromLineItem($analytics, $lineItem, $index, ''); |
||
| 160 | $index++; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add a Craft Commerce LineItem to an Analytics object |
||
| 167 | * |
||
| 168 | * @param IAnalytics|null $analytics |
||
| 169 | * @param LineItem|null $lineItem |
||
| 170 | * @param int $index |
||
| 171 | * @param string $listName |
||
| 172 | * |
||
| 173 | * @return string the title of the product |
||
| 174 | */ |
||
| 175 | public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = ''): string |
||
| 176 | { |
||
| 177 | $result = ''; |
||
| 178 | if ($lineItem && $analytics) { |
||
| 179 | //This is the same for both variant and non variant products |
||
| 180 | $productData = [ |
||
| 181 | 'sku' => $lineItem->purchasable->sku, |
||
| 182 | 'price' => $lineItem->salePrice, |
||
| 183 | 'quantity' => $lineItem->qty, |
||
| 184 | ]; |
||
| 185 | |||
| 186 | $productVariant = $lineItem->purchasable; |
||
| 187 | $productData['name'] = $lineItem->purchasable->title; |
||
| 188 | /** |
||
| 189 | * @TODO: See if there is a Commerce 2 equivalent |
||
| 190 | $productData['category'] = $lineItem->purchasable->type->name; |
||
| 191 | */ |
||
| 192 | |||
| 193 | $result = $productData['name']; |
||
| 194 | |||
| 195 | if ($index) { |
||
| 196 | $productData['position'] = $index; |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($listName) { |
||
| 200 | $productData['list'] = $listName; |
||
| 201 | } |
||
| 202 | |||
| 203 | $settings = InstantAnalytics::$plugin->getSettings(); |
||
| 204 | |||
| 205 | if ($settings) { |
||
| 206 | if (isset($settings['productCategoryField']) && !empty($settings['productCategoryField'])) { |
||
| 207 | $productData['category'] = $this->pullDataFromField( |
||
| 208 | $productVariant, |
||
| 209 | $settings['productCategoryField'] |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | if (isset($settings['productBrandField']) && !empty($settings['productBrandField'])) { |
||
| 214 | $productData['brand'] = $this->pullDataFromField( |
||
| 215 | $productVariant, |
||
| 216 | $settings['productBrandField'] |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | //Add each product to the hit to be sent |
||
| 222 | $analytics->addProduct($productData); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $result; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Add a product impression from a Craft Commerce Product or Variant |
||
| 230 | * |
||
| 231 | * @param IAnalytics $analytics the Analytics object |
||
| 232 | * @param Product|Variant $productVariant the Product or Variant |
||
| 233 | * @param int $index Where the product appears in the list |
||
| 234 | * @param string $listName |
||
| 235 | * @param int $listIndex |
||
| 236 | * |
||
| 237 | * @throws \yii\base\InvalidConfigException |
||
| 238 | */ |
||
| 239 | public function addCommerceProductImpression( |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Add a product detail view from a Craft Commerce Product or Variant |
||
| 273 | * |
||
| 274 | * @param IAnalytics $analytics the Analytics object |
||
| 275 | * @param Product|Variant $productVariant the Product or Variant |
||
| 276 | * |
||
| 277 | * @throws \yii\base\InvalidConfigException |
||
| 278 | */ |
||
| 279 | public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Add a checkout step and option to an Analytics object |
||
| 300 | * |
||
| 301 | * @param IAnalytics $analytics the Analytics object |
||
| 302 | * @param Order $order the Product or Variant |
||
| 303 | * @param int $step the checkout step |
||
| 304 | * @param string $option the checkout option |
||
| 305 | */ |
||
| 306 | public function addCommerceCheckoutStep($analytics = null, $order = null, $step = 1, $option = '') |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Extract product data from a Craft Commerce Product or Variant |
||
| 337 | * |
||
| 338 | * @param Product|Variant $productVariant the Product or Variant |
||
| 339 | * |
||
| 340 | * @return array the product data |
||
| 341 | * @throws \yii\base\InvalidConfigException |
||
| 342 | */ |
||
| 343 | public function getProductDataFromProduct($productVariant = null): array |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param Product|Variant|null $productVariant |
||
| 444 | * @param string $fieldHandle |
||
| 445 | * @param bool $isBrand |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | private function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
| 504 |