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) |
|
333 | 87 | ||
334 | 87 | /** |
|
335 | * @param CartItem $CartItem |
||
336 | 87 | * @return \Eccube\Service\CartService |
|
337 | */ |
||
338 | 87 | View Code Duplication | public function removeCartItem($CartItem) |
365 | 9 | ||
366 | /** |
||
367 | * |
||
368 | 9 | * @param string $productClassId |
|
369 | 9 | * @param integer $quantity |
|
370 | 9 | * @return \Eccube\Service\CartService |
|
371 | */ |
||
372 | public function addProduct($productClassId, $quantity = 1) |
||
379 | |||
380 | /** |
||
381 | * @param string $productClassId |
||
382 | 7 | * @return integer |
|
383 | 7 | */ |
|
384 | 7 | public function getProductQuantity($productClassId) |
|
393 | 6 | ||
394 | 6 | /** |
|
395 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
396 | * @param integer $quantity |
||
397 | * @return \Eccube\Service\CartService |
||
398 | 1 | * @throws CartException |
|
399 | */ |
||
400 | public function setProductQuantity($ProductClass, $quantity) |
||
480 | |||
481 | /** |
||
482 | 4 | * @param string $productClassId |
|
483 | * @return boolean |
||
484 | 4 | */ |
|
485 | 4 | public function canAddProduct($productClassId) |
|
500 | |||
501 | 3 | /** |
|
502 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
503 | * @return bool |
||
504 | */ |
||
505 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
||
543 | |||
544 | /** |
||
545 | * カートブロックに表示するカートを取得します。 |
||
546 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
547 | * |
||
548 | 1 | * @return \Eccube\Entity\Cart |
|
549 | */ |
||
550 | 1 | public function getCartObj() |
|
574 | |||
575 | /** |
||
576 | 1 | * カートを取得します。 |
|
577 | * |
||
578 | 1 | * @return \Eccube\Entity\Cart |
|
579 | 1 | */ |
|
580 | 1 | public function getCart() |
|
622 | |||
623 | /** |
||
624 | * @param string $productClassId |
||
625 | * @return \Eccube\Service\CartService |
||
626 | */ |
||
627 | View Code Duplication | public function removeProduct($productClassId) |
|
653 | |||
654 | /** |
||
655 | * @param string $error |
||
656 | * @param string $productName |
||
657 | * @return \Eccube\Service\CartService |
||
658 | */ |
||
659 | public function addError($error = null, $productName = null) |
||
669 | |||
670 | /** |
||
671 | * @param string $productClassId |
||
672 | * @return \Eccube\Service\CartService |
||
673 | */ |
||
674 | public function upProductQuantity($productClassId) |
||
681 | |||
682 | /** |
||
683 | * @param string $productClassId |
||
684 | * @return \Eccube\Service\CartService |
||
685 | */ |
||
686 | public function downProductQuantity($productClassId) |
||
695 | |||
696 | /** |
||
697 | * @return array |
||
698 | */ |
||
699 | public function getProductTypes() |
||
712 | |||
713 | /** |
||
714 | * @return string[] |
||
715 | */ |
||
716 | public function getErrors() |
||
720 | |||
721 | /** |
||
722 | * @return string[] |
||
723 | */ |
||
724 | public function getMessages() |
||
728 | |||
729 | /** |
||
730 | * @param string $message |
||
731 | * @return \Eccube\Service\CartService |
||
732 | */ |
||
733 | public function setMessage($message) |
||
739 | |||
740 | /** |
||
741 | * @return string |
||
742 | */ |
||
743 | public function getError() |
||
747 | |||
748 | /** |
||
749 | * @param string $error |
||
750 | * @return \Eccube\Service\CartService |
||
751 | */ |
||
752 | public function setError($error = null) |
||
759 | |||
760 | /** |
||
761 | * 商品名を取得 |
||
762 | * |
||
763 | * @param ProductClass $ProductClass |
||
764 | * @return string |
||
765 | */ |
||
766 | private function getProductName(ProductClass $ProductClass) |
||
781 | |||
782 | |||
783 | /** |
||
784 | * 非公開商品の場合、カートから削除 |
||
785 | * |
||
786 | * @param ProductClass $ProductClass |
||
787 | * @return bool |
||
788 | */ |
||
789 | private function isProductDisplay(ProductClass $ProductClass) |
||
801 | |||
802 | |||
803 | /** |
||
804 | * 在庫数と販売制限数のチェック |
||
805 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
806 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
807 | * |
||
808 | * @param ProductClass $ProductClass |
||
809 | * @param $productName |
||
810 | * @param $quantity |
||
811 | * @return int チェック後に更新した個数 |
||
812 | */ |
||
813 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
||
875 | |||
876 | /** |
||
877 | * @return \Eccube\Service\CartCompareService |
||
878 | */ |
||
879 | public function generateCartCompareService() |
||
883 | } |
||
884 |