Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CartService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CartService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class CartService |
||
|
|||
36 | { |
||
37 | /** @var \Eccube\Application */ |
||
38 | public $app; |
||
39 | |||
40 | /** |
||
41 | * @var Session |
||
42 | */ |
||
43 | private $session; |
||
44 | |||
45 | /** |
||
46 | * @var EntityManager |
||
47 | */ |
||
48 | private $entityManager; |
||
49 | |||
50 | /** |
||
51 | * @var \Eccube\Entity\Cart |
||
52 | */ |
||
53 | private $cart; |
||
54 | |||
55 | /** |
||
56 | * @var \Eccube\Entity\BaseInfo |
||
57 | */ |
||
58 | private $BaseInfo; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $errors = array(); |
||
64 | |||
65 | private $ProductType = null; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $messages = array(); |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $error; |
||
76 | |||
77 | 182 | public function __construct(\Eccube\Application $app) |
|
78 | { |
||
79 | 182 | $this->app = $app; |
|
80 | 182 | $this->session = $app['session']; |
|
81 | 182 | $this->entityManager = $app['orm.em']; |
|
82 | |||
83 | 182 | if ($this->session->has('cart')) { |
|
84 | $this->cart = $this->session->get('cart'); |
||
85 | } else { |
||
86 | 182 | $this->cart = new \Eccube\Entity\Cart(); |
|
87 | } |
||
88 | |||
89 | 182 | $this->loadProductClassFromCart(); |
|
90 | |||
91 | 182 | $this->BaseInfo = $app['eccube.repository.base_info']->get(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
||
96 | */ |
||
97 | protected function loadProductClassFromCart() |
||
98 | { |
||
99 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
100 | // $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete'); |
||
101 | // $excludes = $softDeleteFilter->getExcludes(); |
||
102 | // $softDeleteFilter->setExcludes(array( |
||
103 | // 'Eccube\Entity\ProductClass', |
||
104 | // )); |
||
105 | |||
106 | // foreach ($this->cart->getCartItems() as $CartItem) { |
||
107 | // $this->loadProductClassFromCartItem($CartItem); |
||
108 | // } |
||
109 | |||
110 | // $softDeleteFilter->setExcludes($excludes); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * CartItem に対応する ProductClass を設定します。 |
||
115 | * |
||
116 | * @param CartItem $CartItem |
||
117 | */ |
||
118 | 48 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
|
119 | { |
||
120 | $ProductClass = $this |
||
121 | 48 | ->entityManager |
|
122 | 48 | ->getRepository($CartItem->getClassName()) |
|
123 | 48 | ->find($CartItem->getClassId()); |
|
124 | |||
125 | 48 | $CartItem->setObject($ProductClass); |
|
126 | |||
127 | 48 | if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
|
128 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | 88 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
|
140 | |||
141 | 82 | public function save() |
|
142 | { |
||
143 | 82 | return $this->session->set('cart', $this->cart); |
|
144 | } |
||
145 | |||
146 | 4 | public function unlock() |
|
152 | |||
153 | 38 | public function lock() |
|
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | 45 | public function isLocked() |
|
164 | { |
||
165 | 45 | return $this->cart->getLock(); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $pre_order_id |
||
170 | * @return \Eccube\Service\CartService |
||
171 | */ |
||
172 | 20 | public function setPreOrderId($pre_order_id) |
|
173 | { |
||
174 | 20 | $this->cart->setPreOrderId($pre_order_id); |
|
175 | |||
176 | 20 | return $this; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | 39 | public function getPreOrderId() |
|
183 | { |
||
184 | 39 | return $this->cart->getPreOrderId(); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return \Eccube\Service\CartService |
||
189 | */ |
||
190 | 4 | public function clear() |
|
191 | { |
||
192 | 4 | $this->cart |
|
193 | 4 | ->setPreOrderId(null) |
|
194 | 4 | ->setLock(false) |
|
195 | 4 | ->clearCartItems(); |
|
196 | |||
197 | 4 | return $this; |
|
198 | } |
||
199 | |||
200 | 1 | public function getCanAddProductType() |
|
204 | |||
205 | /** |
||
206 | * @param ProductClass|integer $ProductClass 商品規格エンティティ、またはそのID |
||
207 | * @return CartItem |
||
208 | * @throws CartException |
||
209 | */ |
||
210 | public function generateCartItem($ProductClass) |
||
211 | 44 | { |
|
212 | View Code Duplication | if (!$ProductClass instanceof ProductClass) { |
|
229 | 51 | ||
230 | /** |
||
231 | * カート商品を追加する |
||
232 | * |
||
233 | * @param CartItem $CartItem |
||
234 | * @return \Eccube\Service\CartService |
||
235 | */ |
||
236 | public function addCartItem($CartItem) |
||
243 | 86 | ||
244 | 86 | /** |
|
245 | 86 | * カート商品の数量を取得する |
|
246 | 3 | * |
|
247 | * @param CartItem $CartItem |
||
248 | * @return int |
||
249 | 88 | */ |
|
250 | 1 | public function getCartItemQuantity($CartItem) |
|
258 | 87 | ||
259 | 85 | /** |
|
260 | * @param CartItem $CartItem |
||
261 | * @param integer $quantity |
||
262 | * @return \Eccube\Service\CartService |
||
263 | 87 | * @throws CartException |
|
264 | */ |
||
265 | 87 | public function setCartItemQuantity($CartItem, $quantity) |
|
266 | { |
||
267 | /** @var ProductClass $ProductClass */ |
||
268 | $ProductClass = $CartItem->getObject(); |
||
269 | |||
270 | if (!$this->isProductDisplay($ProductClass)) { |
||
271 | throw new CartException('cart.product.not.status'); |
||
272 | 87 | } |
|
273 | |||
274 | 87 | $productName = $this->getProductName($ProductClass); |
|
275 | 79 | ||
276 | // 商品種別に紐づく配送業者を取得 |
||
277 | 79 | $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType()); |
|
278 | |||
279 | View Code Duplication | if (count($deliveries) == 0) { |
|
280 | // 商品種別が存在しなければエラー |
||
281 | 8 | $this->removeProduct($ProductClass->getId()); |
|
282 | 1 | $this->addError('cart.product.not.producttype', $productName); |
|
283 | throw new CartException('cart.product.not.producttype'); |
||
284 | } |
||
285 | |||
286 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
287 | 87 | ||
288 | 87 | View Code Duplication | if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) { |
289 | 87 | if (!$this->canAddProduct($ProductClass->getId())) { |
|
290 | 8 | // 複数配送対応でなく、かつ商品種別が異なればエラー |
|
291 | 8 | throw new CartException('cart.product.type.kind'); |
|
292 | } |
||
293 | 87 | } else { |
|
294 | // 複数配送対応で、かつ同一支払方法がなければエラー |
||
295 | if (!$this->canAddProductPayment($ProductClass->getProductType())) { |
||
296 | 87 | throw new CartException('cart.product.payment.kind'); |
|
297 | 87 | } |
|
298 | 87 | } |
|
299 | 1 | ||
300 | 1 | $compareService = $this->generateCartCompareService(); |
|
301 | $ExistsCartItem = $compareService->getExistsCartItem($CartItem); |
||
302 | 86 | $subtotal = 0; |
|
303 | $productClassQuantity = 0; |
||
304 | 87 | ||
305 | foreach ($this->getCartObj()->getCartItems() as $CurrentCartItem) { |
||
306 | 87 | ||
307 | $CurrentProductClass = $CurrentCartItem->getObject(); |
||
308 | |||
309 | // 同じ商品規格IDの数量を集計 |
||
310 | if ($CurrentProductClass->getId() == $ProductClass->getId()) { |
||
311 | $productClassQuantity += $CurrentCartItem->getQuantity(); |
||
312 | 87 | } |
|
313 | 2 | ||
314 | 1 | // 小計を集計 |
|
315 | 1 | $subtotal += $CurrentCartItem->getTotalPrice(); |
|
316 | } |
||
317 | 1 | ||
318 | 1 | // 既存カートの数量と小計を除外 |
|
319 | if ($ExistsCartItem) { |
||
320 | $subtotal -= $CurrentCartItem->getTotalPrice(); |
||
321 | 87 | $productClassQuantity -= $CurrentCartItem->getQuantity(); |
|
322 | 2 | } |
|
323 | 2 | ||
324 | View Code Duplication | for ($newQuantity = 0; $newQuantity < $quantity; $newQuantity++) { |
|
325 | 87 | // TODO 単価をProductClassではなくCartItemから取得する |
|
326 | 3 | $subtotal += $ProductClass->getPrice02IncTax(); |
|
327 | if ($subtotal > $this->app['config']['max_total_fee']) { |
||
328 | $this->setError('cart.over.price_limit'); |
||
329 | 87 | break; |
|
330 | } |
||
331 | 87 | } |
|
332 | 87 | ||
333 | 87 | // 数量が0の場合、エラー |
|
334 | 87 | if ($newQuantity == 0) { |
|
335 | throw new CartException('cart.over.price_limit'); |
||
336 | 87 | } |
|
337 | |||
338 | 87 | $totalQuantity = $productClassQuantity + $newQuantity; |
|
339 | // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
||
340 | $newTotalQuantity = $this->setProductLimit($ProductClass, $productName, $totalQuantity); |
||
341 | $quantity = min($quantity, $newTotalQuantity - $productClassQuantity); |
||
342 | |||
343 | // 新しい数量でカート内商品を登録する |
||
344 | if (0 < $quantity) { |
||
345 | 79 | $CartItem->setQuantity($quantity); |
|
346 | $this->cart->setCartItem($CartItem, $compareService); |
||
347 | } |
||
348 | 79 | ||
349 | 79 | return $this; |
|
350 | 79 | } |
|
351 | |||
352 | 79 | /** |
|
353 | * |
||
354 | * @param string $productClassId |
||
355 | * @param integer $quantity |
||
356 | 79 | * @return \Eccube\Service\CartService |
|
357 | */ |
||
358 | 79 | public function addProduct($productClassId, $quantity = 1) |
|
359 | { |
||
360 | $quantity += $this->getProductQuantity($productClassId); |
||
361 | $this->setProductQuantity($productClassId, $quantity); |
||
362 | |||
363 | return $this; |
||
364 | } |
||
365 | 9 | ||
366 | /** |
||
367 | * @param string $productClassId |
||
368 | 9 | * @return integer |
|
369 | 9 | */ |
|
370 | 9 | public function getProductQuantity($productClassId) |
|
379 | |||
380 | /** |
||
381 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
382 | 7 | * @param integer $quantity |
|
383 | 7 | * @return \Eccube\Service\CartService |
|
384 | 7 | * @throws CartException |
|
385 | 7 | */ |
|
386 | 6 | public function setProductQuantity($ProductClass, $quantity) |
|
466 | 1 | ||
467 | /** |
||
468 | * @param string $productClassId |
||
469 | 1 | * @return boolean |
|
470 | */ |
||
471 | 1 | public function canAddProduct($productClassId) |
|
486 | 4 | ||
487 | 3 | /** |
|
488 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
489 | 4 | * @return bool |
|
490 | */ |
||
491 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
||
529 | |||
530 | 20 | /** |
|
531 | 20 | * カートブロックに表示するカートを取得します。 |
|
532 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
533 | 20 | * |
|
534 | * @return \Eccube\Entity\Cart |
||
535 | */ |
||
536 | public function getCartObj() |
||
560 | |||
561 | 1 | /** |
|
562 | * カートを取得します。 |
||
563 | * |
||
564 | * @return \Eccube\Entity\Cart |
||
565 | */ |
||
566 | public function getCart() |
||
608 | |||
609 | /** |
||
610 | * @param string $productClassId |
||
611 | * @return \Eccube\Service\CartService |
||
612 | */ |
||
613 | public function removeProduct($productClassId) |
||
639 | |||
640 | /** |
||
641 | * @param string $error |
||
642 | * @param string $productName |
||
643 | * @return \Eccube\Service\CartService |
||
644 | */ |
||
645 | public function addError($error = null, $productName = null) |
||
655 | |||
656 | /** |
||
657 | * @param string $productClassId |
||
658 | * @return \Eccube\Service\CartService |
||
659 | */ |
||
660 | public function upProductQuantity($productClassId) |
||
667 | |||
668 | /** |
||
669 | * @param string $productClassId |
||
670 | * @return \Eccube\Service\CartService |
||
671 | */ |
||
672 | public function downProductQuantity($productClassId) |
||
681 | |||
682 | /** |
||
683 | * @return array |
||
684 | */ |
||
685 | public function getProductTypes() |
||
698 | |||
699 | /** |
||
700 | * @return string[] |
||
701 | */ |
||
702 | public function getErrors() |
||
706 | |||
707 | /** |
||
708 | * @return string[] |
||
709 | */ |
||
710 | public function getMessages() |
||
714 | |||
715 | /** |
||
716 | * @param string $message |
||
717 | * @return \Eccube\Service\CartService |
||
718 | */ |
||
719 | public function setMessage($message) |
||
725 | |||
726 | /** |
||
727 | * @return string |
||
728 | */ |
||
729 | public function getError() |
||
733 | |||
734 | /** |
||
735 | * @param string $error |
||
736 | * @return \Eccube\Service\CartService |
||
737 | */ |
||
738 | public function setError($error = null) |
||
745 | |||
746 | /** |
||
747 | * 商品名を取得 |
||
748 | * |
||
749 | * @param ProductClass $ProductClass |
||
750 | * @return string |
||
751 | */ |
||
752 | private function getProductName(ProductClass $ProductClass) |
||
767 | |||
768 | |||
769 | /** |
||
770 | * 非公開商品の場合、カートから削除 |
||
771 | * |
||
772 | * @param ProductClass $ProductClass |
||
773 | * @return bool |
||
774 | */ |
||
775 | private function isProductDisplay(ProductClass $ProductClass) |
||
787 | |||
788 | |||
789 | /** |
||
790 | * 在庫数と販売制限数のチェック |
||
791 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
792 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
793 | * |
||
794 | * @param ProductClass $ProductClass |
||
795 | * @param $productName |
||
796 | * @param $quantity |
||
797 | * @return int チェック後に更新した個数 |
||
798 | */ |
||
799 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
||
861 | |||
862 | /** |
||
863 | * @return \Eccube\Service\CartCompareService |
||
864 | */ |
||
865 | public function generateCartCompareService() |
||
869 | } |
||
870 |