Total Complexity | 41 |
Total Lines | 304 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like DiscountHelper 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 DiscountHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class DiscountHelper |
||
21 | { |
||
22 | use Injectable; |
||
23 | |||
24 | /** |
||
25 | * @var |
||
26 | */ |
||
27 | private $product; |
||
28 | |||
29 | /** |
||
30 | * @var DataList|null |
||
31 | */ |
||
32 | private $available_discounts = null; |
||
33 | |||
34 | /** |
||
35 | * @var Variation|null |
||
36 | */ |
||
37 | private $variation = null; |
||
38 | |||
39 | |||
40 | /** |
||
41 | * @var DiscountTier |
||
42 | */ |
||
43 | private $discount_tier; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $quantity; |
||
49 | |||
50 | /** |
||
51 | * @var DBField|DBCurrency |
||
52 | */ |
||
53 | private $discounted_price; |
||
54 | |||
55 | /** |
||
56 | * DiscountHelper constructor. |
||
57 | * @param $product |
||
58 | * @param int $quantity |
||
59 | * @param Variation|null $variation |
||
60 | */ |
||
61 | public function __construct($product, $quantity = 1, $variation = null) |
||
62 | { |
||
63 | $this->setProduct($product); |
||
64 | $this->setQuantity($quantity); |
||
65 | $this->setDiscountTier(); |
||
66 | |||
67 | if ($variation instanceof Variation) { |
||
68 | $this->setVariation($variation); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getProduct() |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param $product |
||
82 | * @return $this |
||
83 | */ |
||
84 | public function setProduct($product): self |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set the available discounts based on DiscountHelper::product |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setAvailableDiscounts() |
||
99 | { |
||
100 | if (!$this->getProduct()->ExcludeFromDiscounts) { |
||
101 | $now = date("Y-m-d H:i:s", strtotime('now')); |
||
102 | //don't get discounts the product is excluded from |
||
103 | $list = Discount::get()->exclude([ |
||
104 | 'ExcludeProducts.ID' => $this->getProduct()->ID, |
||
105 | ])->whereAny([ |
||
106 | "`StartTime` <= '{$now}' AND `EndTime` >= '{$now}'", |
||
107 | "(`StartTime` = '' OR `StartTime` IS NULL) AND (`EndTime` = '' OR `EndTime` IS NULL)", |
||
108 | "`StartTime` <= '{$now}' AND (`EndTime` = '' OR `EndTime` IS NULL)", |
||
109 | "(`StartTime` = '' OR `StartTime` IS NULL) AND `EndTime` >= '{$now}'", |
||
110 | ]); |
||
111 | |||
112 | $customerDiscountIDs = Member::get()->filter('DiscountID:GreaterThan', 0); |
||
113 | |||
114 | if ($member = Security::getCurrentUser()) { |
||
115 | $customerDiscountIDs = $customerDiscountIDs->exclude('ID', $member->ID); |
||
116 | } |
||
117 | |||
118 | $customerDiscountIDs = $customerDiscountIDs->column('DiscountID'); |
||
119 | |||
120 | if (count($customerDiscountIDs)) { |
||
121 | $list = $list->exclude('ID', $customerDiscountIDs); |
||
122 | } |
||
123 | |||
124 | $strict = $list->filter([ |
||
125 | 'Products.Count():GreaterThan' => 0, |
||
126 | 'Products.ID' => $this->getProduct()->ID, |
||
127 | ]); |
||
128 | |||
129 | $global = $list->filter('Products.Count()', 0); |
||
130 | |||
131 | $merge = array_merge(array_values($strict->column()), array_values($global->column())); |
||
132 | |||
133 | $this->available_discounts = count($merge) ? Discount::get()->byIDs($merge) : ArrayList::create(); |
||
134 | } else { |
||
135 | $this->available_discounts = ArrayList::create(); |
||
136 | } |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return DataList|null |
||
143 | */ |
||
144 | public function getAvailableDiscounts() |
||
145 | { |
||
146 | return $this->available_discounts; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param Variation $variation |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setVariation($variation): self |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return mixed |
||
168 | */ |
||
169 | public function getVariation() |
||
170 | { |
||
171 | return $this->variation; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param $quantity |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function setQuantity($quantity) |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return int |
||
193 | */ |
||
194 | public function getQuantity() |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setDiscountTier() |
||
203 | { |
||
204 | $this->discount_tier = $this->findBestDiscount(); |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return DiscountTier |
||
211 | */ |
||
212 | public function getDiscountTier() |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @return mixed |
||
223 | */ |
||
224 | protected function findBestDiscount() |
||
225 | { |
||
226 | $appropriateTiers = ArrayList::create(); |
||
227 | |||
228 | /** @var Discount $discount */ |
||
229 | foreach ($this->getAvailableDiscounts() as $discount) { |
||
230 | if ($tier = $discount->getTierByQuantity($this->getQuantity())) { |
||
231 | $appropriateTiers->push($tier); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | return $this->resolveDiscountTiers($appropriateTiers); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param $discountTiers |
||
240 | * @return DiscountTier|null |
||
241 | */ |
||
242 | protected function resolveDiscountTiers($discountTiers) |
||
243 | { |
||
244 | if (!$discountTiers->count()) { |
||
245 | return null; |
||
246 | } |
||
247 | |||
248 | $basePrice = $this->getProduct()->Price; |
||
249 | $bestTier = null; |
||
250 | $calculatePrice = function (DiscountTier $tier) use ($basePrice) { |
||
251 | if ($tier->ParentType == 'Percent') { |
||
252 | return $basePrice - ($basePrice * ($tier->Percentage / 100)); |
||
253 | } else { |
||
254 | return $basePrice - $tier->Amount; |
||
255 | } |
||
256 | }; |
||
257 | |||
258 | /** @var DiscountTier $tier */ |
||
259 | foreach ($discountTiers as $tier) { |
||
260 | if ($bestTier == null) { |
||
261 | $bestTier = [ |
||
262 | 'price' => $calculatePrice($tier), |
||
263 | 'discountTier' => $tier, |
||
264 | ]; |
||
265 | continue; |
||
266 | } |
||
267 | |||
268 | if ($calculatePrice($tier) < $bestTier['price']) { |
||
269 | $bestTier['price'] = $calculatePrice($tier); |
||
270 | $bestTier['discountTier'] = $tier; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | return is_array($bestTier) && isset($bestTier['discountTier']) ? $bestTier['discountTier'] : null; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return DBCurrency |
||
279 | */ |
||
280 | public function getDiscountedPrice() |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @return string |
||
297 | */ |
||
298 | public function getFoxyDiscountType() |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @return false|string |
||
307 | */ |
||
308 | public function getDiscountFieldValue() |
||
324 | } |
||
325 | } |
||
326 |