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 | 121 | public function __construct(\Eccube\Application $app) |
|
93 | |||
94 | /** |
||
95 | * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
||
96 | */ |
||
97 | 121 | 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 | 121 | ||
106 | foreach ($this->cart->getCartItems() as $CartItem) { |
||
107 | 121 | $this->loadProductClassFromCartItem($CartItem); |
|
108 | } |
||
109 | |||
110 | 121 | $softDeleteFilter->setExcludes($excludes); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * CartItem に対応する ProductClass を設定します。 |
||
115 | * |
||
116 | * @param CartItem $CartItem |
||
117 | 29 | */ |
|
118 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
||
131 | 68 | ||
132 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
||
140 | |||
141 | public function save() |
||
145 | 3 | ||
146 | public function unlock() |
||
152 | 13 | ||
153 | public function lock() |
||
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function isLocked() |
||
167 | |||
168 | /** |
||
169 | * @param string $pre_order_id |
||
170 | * @return \Eccube\Service\CartService |
||
171 | 17 | */ |
|
172 | public function setPreOrderId($pre_order_id) |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getPreOrderId() |
||
186 | |||
187 | /** |
||
188 | * @return \Eccube\Service\CartService |
||
189 | 7 | */ |
|
190 | public function clear() |
||
199 | 1 | ||
200 | public function getCanAddProductType() |
||
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param string $productClassId |
||
208 | * @param integer $quantity |
||
209 | * @return \Eccube\Service\CartService |
||
210 | 25 | */ |
|
211 | public function addProduct($productClassId, $quantity = 1) |
||
218 | |||
219 | /** |
||
220 | * @param string $productClassId |
||
221 | * @return integer |
||
222 | 27 | */ |
|
223 | public function getProductQuantity($productClassId) |
||
232 | |||
233 | /** |
||
234 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
235 | * @param integer $quantity |
||
236 | * @return \Eccube\Service\CartService |
||
237 | * @throws CartException |
||
238 | 61 | */ |
|
239 | public function setProductQuantity($ProductClass, $quantity) |
||
319 | |||
320 | /** |
||
321 | * @param string $productClassId |
||
322 | * @return boolean |
||
323 | */ |
||
324 | 61 | public function canAddProduct($productClassId) |
|
339 | |||
340 | /** |
||
341 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
342 | * @return bool |
||
343 | */ |
||
344 | 59 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
|
382 | |||
383 | /** |
||
384 | * カートブロックに表示するカートを取得します。 |
||
385 | 1 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
|
386 | 1 | * |
|
387 | * @return \Eccube\Entity\Cart |
||
388 | */ |
||
389 | public function getCartObj() |
||
413 | |||
414 | /** |
||
415 | * カートを取得します。 |
||
416 | * |
||
417 | * @return \Eccube\Entity\Cart |
||
418 | */ |
||
419 | 3 | public function getCart() |
|
461 | |||
462 | /** |
||
463 | * @param string $productClassId |
||
464 | * @return \Eccube\Service\CartService |
||
465 | */ |
||
466 | public function removeProduct($productClassId) |
||
492 | |||
493 | /** |
||
494 | * @param string $error |
||
495 | 2 | * @param string $productName |
|
496 | * @return \Eccube\Service\CartService |
||
497 | */ |
||
498 | public function addError($error = null, $productName = null) |
||
521 | |||
522 | /** |
||
523 | 17 | * @param string $productClassId |
|
524 | * @return \Eccube\Service\CartService |
||
525 | */ |
||
526 | 17 | public function upProductQuantity($productClassId) |
|
533 | |||
534 | /** |
||
535 | * @param string $productClassId |
||
536 | * @return \Eccube\Service\CartService |
||
537 | */ |
||
538 | public function downProductQuantity($productClassId) |
||
547 | |||
548 | /** |
||
549 | * @return array |
||
550 | */ |
||
551 | public function getProductTypes() |
||
564 | |||
565 | /** |
||
566 | 1 | * @return string[] |
|
567 | */ |
||
568 | 1 | public function getErrors() |
|
572 | |||
573 | /** |
||
574 | * @return string[] |
||
575 | 1 | */ |
|
576 | public function getMessages() |
||
580 | |||
581 | /** |
||
582 | * @param string $message |
||
583 | * @return \Eccube\Service\CartService |
||
584 | */ |
||
585 | public function setMessage($message) |
||
591 | |||
592 | /** |
||
593 | * @return string |
||
594 | */ |
||
595 | public function getError() |
||
599 | |||
600 | /** |
||
601 | * @param string $error |
||
602 | * @return \Eccube\Service\CartService |
||
603 | */ |
||
604 | public function setError($error = null) |
||
611 | |||
612 | /** |
||
613 | * 商品名を取得 |
||
614 | * |
||
615 | * @param ProductClass $ProductClass |
||
616 | * @return string |
||
617 | */ |
||
618 | private function getProductName(ProductClass $ProductClass) |
||
633 | |||
634 | |||
635 | /** |
||
636 | * 非公開商品の場合、カートから削除 |
||
637 | * |
||
638 | * @param ProductClass $ProductClass |
||
639 | * @return bool |
||
640 | */ |
||
641 | private function isProductDisplay(ProductClass $ProductClass) |
||
653 | |||
654 | |||
655 | /** |
||
656 | * 在庫数と販売制限数のチェック |
||
657 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
658 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
659 | * |
||
660 | * @param ProductClass $ProductClass |
||
661 | * @param $productName |
||
662 | * @param $quantity |
||
663 | * @return int チェック後に更新した個数 |
||
664 | */ |
||
665 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
||
727 | |||
728 | } |
||
729 |