Total Complexity | 73 |
Total Lines | 484 |
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) |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Send analytics information for the item added to the cart |
||
72 | * |
||
73 | * @param Order $order the Product or Variant |
||
74 | * @param LineItem $lineItem the line item that was added |
||
75 | */ |
||
76 | public function addToCart( |
||
77 | /** @noinspection PhpUnusedParameterInspection */ |
||
78 | $order = null, $lineItem = null |
||
79 | ) { |
||
80 | if ($lineItem) { |
||
81 | $title = $lineItem->purchasable->title; |
||
82 | $quantity = $lineItem->qty; |
||
83 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics('Commerce', 'Add to Cart', $title, $quantity); |
||
84 | |||
85 | if ($analytics) { |
||
86 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
87 | $analytics->setEventLabel($title); |
||
88 | // Don't forget to set the product action, in this case to ADD |
||
89 | $analytics->setProductActionToAdd(); |
||
90 | $analytics->sendEvent(); |
||
91 | |||
92 | Craft::info(Craft::t( |
||
93 | 'instant-analytics', |
||
94 | 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', |
||
95 | ['title' => $title, 'quantity' => $quantity] |
||
96 | ), __METHOD__); |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Send analytics information for the item removed from the cart |
||
103 | * |
||
104 | * @param Order|null $order |
||
105 | * @param LineItem|null $lineItem |
||
106 | */ |
||
107 | public function removeFromCart( |
||
108 | /** @noinspection PhpUnusedParameterInspection */ |
||
109 | $order = null, $lineItem = null |
||
110 | ) { |
||
111 | if ($lineItem) { |
||
112 | $title = $lineItem->purchasable->title; |
||
113 | $quantity = $lineItem->qty; |
||
114 | $analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
||
115 | 'Commerce', |
||
116 | 'Remove from Cart', |
||
117 | $title, |
||
118 | $quantity |
||
119 | ); |
||
120 | |||
121 | if ($analytics) { |
||
122 | $title = $this->addProductDataFromLineItem($analytics, $lineItem); |
||
123 | $analytics->setEventLabel($title); |
||
124 | // Don't forget to set the product action, in this case to ADD |
||
125 | $analytics->setProductActionToRemove(); |
||
126 | $analytics->sendEvent(); |
||
127 | |||
128 | Craft::info(Craft::t( |
||
129 | 'instant-analytics', |
||
130 | 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', |
||
131 | ['title' => $title, 'quantity' => $quantity] |
||
132 | ), __METHOD__); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Add a Craft Commerce OrderModel to an Analytics object |
||
140 | * |
||
141 | * @param IAnalytics $analytics the Analytics object |
||
142 | * @param Order $order the Product or Variant |
||
143 | */ |
||
144 | public function addCommerceOrderToAnalytics($analytics = null, $order = null) |
||
145 | { |
||
146 | if ($order && $analytics) { |
||
147 | // First, include the transaction data |
||
148 | $analytics->setTransactionId($order->number) |
||
149 | ->setRevenue($order->totalPrice) |
||
150 | ->setTax($order->getAdjustmentsTotalByType('tax', true)) |
||
151 | ->setShipping($order->getAdjustmentsTotalByType('shipping', true)); |
||
152 | |||
153 | // Coupon code? |
||
154 | if ($order->couponCode) { |
||
155 | $analytics->setCouponCode($order->couponCode); |
||
156 | } |
||
157 | |||
158 | // Add each line item in the transaction |
||
159 | // Two cases - variant and non variant products |
||
160 | $index = 1; |
||
161 | |||
162 | foreach ($order->lineItems as $key => $lineItem) { |
||
163 | $this->addProductDataFromLineItem($analytics, $lineItem, $index, ''); |
||
164 | $index++; |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Add a Craft Commerce LineItem to an Analytics object |
||
171 | * |
||
172 | * @param IAnalytics|null $analytics |
||
173 | * @param LineItem|null $lineItem |
||
174 | * @param int $index |
||
175 | * @param string $listName |
||
176 | * |
||
177 | * @return string the title of the product |
||
178 | * @throws \yii\base\InvalidConfigException |
||
179 | */ |
||
180 | public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = ''): string |
||
181 | { |
||
182 | $result = ''; |
||
183 | if ($lineItem && $analytics) { |
||
184 | $product = null; |
||
185 | $purchasable = $lineItem->purchasable; |
||
186 | //This is the same for both variant and non variant products |
||
187 | $productData = [ |
||
188 | 'name' => $purchasable->title, |
||
189 | 'sku' => $purchasable->sku, |
||
190 | 'price' => $lineItem->salePrice, |
||
191 | 'quantity' => $lineItem->qty, |
||
192 | ]; |
||
193 | // Handle this purchasable being a Variant |
||
194 | if (is_a($purchasable, Variant::class)) { |
||
195 | /** @var Variant $purchasable */ |
||
196 | $product = $purchasable->getProduct(); |
||
197 | $variant = $purchasable; |
||
198 | // Product with variants |
||
199 | $productData['name'] = $product->title; |
||
200 | $productData['variant'] = $variant->title; |
||
201 | $productData['category'] = $product->getType(); |
||
202 | } |
||
203 | // Handle this purchasable being a Product |
||
204 | if (is_a($purchasable, Product::class)) { |
||
205 | /** @var Product $purchasable */ |
||
206 | $product = $purchasable; |
||
207 | $productData['name'] = $product->title; |
||
208 | $productData['variant'] = $product->title; |
||
209 | $productData['category'] = $product->getType(); |
||
210 | } |
||
211 | // Handle product lists |
||
212 | if ($index) { |
||
213 | $productData['position'] = $index; |
||
214 | } |
||
215 | if ($listName) { |
||
216 | $productData['list'] = $listName; |
||
217 | } |
||
218 | // Add in any custom categories/brands that might be set |
||
219 | if (InstantAnalytics::$settings && $product) { |
||
220 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
221 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
222 | $productData['category'] = $this->pullDataFromField( |
||
223 | $product, |
||
224 | InstantAnalytics::$settings['productCategoryField'] |
||
225 | ); |
||
226 | } |
||
227 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
228 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
229 | $productData['brand'] = $this->pullDataFromField( |
||
230 | $product, |
||
231 | InstantAnalytics::$settings['productBrandField'] |
||
232 | ); |
||
233 | } |
||
234 | } |
||
235 | $result = $productData['name']; |
||
236 | //Add each product to the hit to be sent |
||
237 | $analytics->addProduct($productData); |
||
238 | } |
||
239 | |||
240 | return $result; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Add a product impression from a Craft Commerce Product or Variant |
||
245 | * |
||
246 | * @param IAnalytics $analytics the Analytics object |
||
247 | * @param Product|Variant $productVariant the Product or Variant |
||
248 | * @param int $index Where the product appears in the |
||
249 | * list |
||
250 | * @param string $listName |
||
251 | * @param int $listIndex |
||
252 | * |
||
253 | * @throws \yii\base\InvalidConfigException |
||
254 | */ |
||
255 | public function addCommerceProductImpression( |
||
284 | } |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Add a product detail view from a Craft Commerce Product or Variant |
||
289 | * |
||
290 | * @param IAnalytics $analytics the Analytics object |
||
291 | * @param Product|Variant $productVariant the Product or Variant |
||
292 | * |
||
293 | * @throws \yii\base\InvalidConfigException |
||
294 | */ |
||
295 | public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Add a checkout step and option to an Analytics object |
||
316 | * |
||
317 | * @param IAnalytics $analytics the Analytics object |
||
318 | * @param Order $order the Product or Variant |
||
319 | * @param int $step the checkout step |
||
320 | * @param string $option the checkout option |
||
321 | */ |
||
322 | public function addCommerceCheckoutStep($analytics = null, $order = null, $step = 1, $option = '') |
||
348 | } |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Extract product data from a Craft Commerce Product or Variant |
||
353 | * |
||
354 | * @param Product|Variant $productVariant the Product or Variant |
||
355 | * |
||
356 | * @return array the product data |
||
357 | * @throws \yii\base\InvalidConfigException |
||
358 | */ |
||
359 | public function getProductDataFromProduct($productVariant = null): array |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @param Product|Variant|null $productVariant |
||
461 | * @param string $fieldHandle |
||
462 | * @param bool $isBrand |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | private function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
521 |