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 | 145 | public function __construct(\Eccube\Application $app) |
|
78 | { |
||
79 | 145 | $this->app = $app; |
|
80 | 145 | $this->session = $app['session']; |
|
81 | 145 | $this->entityManager = $app['orm.em']; |
|
82 | |||
83 | 145 | if ($this->session->has('cart')) { |
|
84 | $this->cart = $this->session->get('cart'); |
||
85 | } else { |
||
86 | 145 | $this->cart = new \Eccube\Entity\Cart(); |
|
87 | } |
||
88 | |||
89 | 145 | $this->loadProductClassFromCart(); |
|
90 | |||
91 | 145 | $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 | 39 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
|
119 | { |
||
120 | $ProductClass = $this |
||
121 | 39 | ->entityManager |
|
122 | 39 | ->getRepository($CartItem->getClassName()) |
|
123 | 39 | ->find($CartItem->getClassId()); |
|
124 | |||
125 | 39 | $CartItem->setObject($ProductClass); |
|
126 | |||
127 | 39 | if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
|
128 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | 66 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
|
140 | |||
141 | 71 | public function save() |
|
142 | { |
||
143 | 71 | return $this->session->set('cart', $this->cart); |
|
144 | } |
||
145 | |||
146 | 3 | public function unlock() |
|
152 | |||
153 | 35 | public function lock() |
|
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | 40 | public function isLocked() |
|
164 | { |
||
165 | 40 | return $this->cart->getLock(); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $pre_order_id |
||
170 | * @return \Eccube\Service\CartService |
||
171 | */ |
||
172 | 17 | public function setPreOrderId($pre_order_id) |
|
173 | { |
||
174 | 17 | $this->cart->setPreOrderId($pre_order_id); |
|
175 | |||
176 | 17 | return $this; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | 36 | public function getPreOrderId() |
|
183 | { |
||
184 | 36 | return $this->cart->getPreOrderId(); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return \Eccube\Service\CartService |
||
189 | */ |
||
190 | public function clear() |
||
191 | { |
||
192 | $this->cart |
||
193 | ->setPreOrderId(null) |
||
194 | ->setLock(false) |
||
195 | ->clearCartItems(); |
||
196 | |||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | public function getCanAddProductType() |
||
201 | { |
||
202 | return $this->ProductType; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param string $productClassId |
||
208 | * @param integer $quantity |
||
209 | * @return \Eccube\Service\CartService |
||
210 | */ |
||
211 | 36 | public function addProduct($productClassId, $quantity = 1) |
|
218 | |||
219 | /** |
||
220 | * @param string $productClassId |
||
221 | * @return integer |
||
222 | */ |
||
223 | 42 | public function getProductQuantity($productClassId) |
|
224 | { |
||
232 | |||
233 | /** |
||
234 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
235 | * @param integer $quantity |
||
236 | * @return \Eccube\Service\CartService |
||
237 | * @throws CartException |
||
238 | */ |
||
239 | 68 | public function setProductQuantity($ProductClass, $quantity) |
|
319 | |||
320 | /** |
||
321 | 66 | * @param string $productClassId |
|
322 | * @return boolean |
||
323 | */ |
||
324 | public function canAddProduct($productClassId) |
||
339 | |||
340 | /** |
||
341 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
||
382 | 3 | ||
383 | 3 | /** |
|
384 | 3 | * カートブロックに表示するカートを取得します。 |
|
385 | 3 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
|
386 | 3 | * |
|
387 | 3 | * @return \Eccube\Entity\Cart |
|
388 | */ |
||
389 | public function getCartObj() |
||
413 | |||
414 | 39 | /** |
|
415 | * カートを取得します。 |
||
416 | * |
||
417 | 39 | * @return \Eccube\Entity\Cart |
|
418 | */ |
||
419 | 39 | 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 | * @param string $productName |
||
496 | 3 | * @return \Eccube\Service\CartService |
|
497 | */ |
||
498 | 3 | public function addError($error = null, $productName = null) |
|
508 | 3 | ||
509 | /** |
||
510 | 3 | * @param string $productClassId |
|
511 | * @return \Eccube\Service\CartService |
||
512 | 3 | */ |
|
513 | public function upProductQuantity($productClassId) |
||
520 | |||
521 | /** |
||
522 | * @param string $productClassId |
||
523 | * @return \Eccube\Service\CartService |
||
524 | 17 | */ |
|
525 | public function downProductQuantity($productClassId) |
||
534 | |||
535 | /** |
||
536 | * @return array |
||
537 | */ |
||
538 | public function getProductTypes() |
||
551 | |||
552 | /** |
||
553 | * @return string[] |
||
554 | */ |
||
555 | public function getErrors() |
||
559 | |||
560 | /** |
||
561 | * @return string[] |
||
562 | */ |
||
563 | public function getMessages() |
||
567 | |||
568 | /** |
||
569 | * @param string $message |
||
570 | * @return \Eccube\Service\CartService |
||
571 | */ |
||
572 | public function setMessage($message) |
||
578 | |||
579 | /** |
||
580 | * @return string |
||
581 | */ |
||
582 | public function getError() |
||
586 | |||
587 | /** |
||
588 | * @param string $error |
||
589 | * @return \Eccube\Service\CartService |
||
590 | */ |
||
591 | public function setError($error = null) |
||
598 | |||
599 | /** |
||
600 | * 商品名を取得 |
||
601 | * |
||
602 | * @param ProductClass $ProductClass |
||
603 | * @return string |
||
604 | */ |
||
605 | private function getProductName(ProductClass $ProductClass) |
||
620 | |||
621 | |||
622 | /** |
||
623 | * 非公開商品の場合、カートから削除 |
||
624 | * |
||
625 | * @param ProductClass $ProductClass |
||
626 | * @return bool |
||
627 | */ |
||
628 | private function isProductDisplay(ProductClass $ProductClass) |
||
640 | |||
641 | |||
642 | /** |
||
643 | * 在庫数と販売制限数のチェック |
||
644 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
645 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
646 | * |
||
647 | * @param ProductClass $ProductClass |
||
648 | * @param $productName |
||
649 | * @param $quantity |
||
650 | * @return int チェック後に更新した個数 |
||
651 | */ |
||
652 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
||
714 | |||
715 | } |
||
716 |