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 | 272 | public function __construct(\Eccube\Application $app) |
|
78 | { |
||
79 | 272 | $this->app = $app; |
|
80 | 272 | $this->session = $app['session']; |
|
81 | 272 | $this->entityManager = $app['orm.em']; |
|
82 | |||
83 | 272 | if ($this->session->has('cart')) { |
|
84 | $this->cart = $this->session->get('cart'); |
||
85 | } else { |
||
86 | 272 | $this->cart = new \Eccube\Entity\Cart(); |
|
87 | } |
||
88 | |||
89 | 272 | $this->loadProductClassFromCart(); |
|
90 | |||
91 | 272 | $this->BaseInfo = $app['eccube.repository.base_info']->get(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
||
96 | */ |
||
97 | 272 | protected function loadProductClassFromCart() |
|
98 | { |
||
99 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
100 | 272 | $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete'); |
|
101 | 272 | $excludes = $softDeleteFilter->getExcludes(); |
|
102 | 272 | $softDeleteFilter->setExcludes(array( |
|
103 | 272 | 'Eccube\Entity\ProductClass', |
|
104 | )); |
||
105 | |||
106 | 272 | foreach ($this->cart->getCartItems() as $CartItem) { |
|
107 | 272 | $this->loadProductClassFromCartItem($CartItem); |
|
108 | } |
||
109 | |||
110 | 272 | $softDeleteFilter->setExcludes($excludes); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * CartItem に対応する ProductClass を設定します。 |
||
115 | * |
||
116 | * @param CartItem $CartItem |
||
117 | */ |
||
118 | 132 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
|
119 | { |
||
120 | $ProductClass = $this |
||
121 | 132 | ->entityManager |
|
122 | 132 | ->getRepository($CartItem->getClassName()) |
|
123 | 132 | ->find($CartItem->getClassId()); |
|
124 | |||
125 | 132 | $CartItem->setObject($ProductClass); |
|
126 | |||
127 | 132 | if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
|
128 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | 171 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
|
140 | |||
141 | 164 | public function save() |
|
142 | { |
||
143 | 164 | return $this->session->set('cart', $this->cart); |
|
144 | } |
||
145 | |||
146 | 14 | public function unlock() |
|
152 | |||
153 | 116 | public function lock() |
|
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | 106 | public function isLocked() |
|
164 | { |
||
165 | 106 | return $this->cart->getLock(); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $pre_order_id |
||
170 | * @return \Eccube\Service\CartService |
||
171 | */ |
||
172 | 94 | public function setPreOrderId($pre_order_id) |
|
173 | { |
||
174 | 94 | $this->cart->setPreOrderId($pre_order_id); |
|
175 | |||
176 | 94 | return $this; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | 93 | public function getPreOrderId() |
|
183 | { |
||
184 | 93 | return $this->cart->getPreOrderId(); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return \Eccube\Service\CartService |
||
189 | */ |
||
190 | 29 | public function clear() |
|
191 | { |
||
192 | 29 | $this->cart |
|
193 | 29 | ->setPreOrderId(null) |
|
194 | 29 | ->setLock(false) |
|
195 | 29 | ->clearCartItems(); |
|
196 | |||
197 | 29 | return $this; |
|
198 | } |
||
199 | |||
200 | 1 | public function getCanAddProductType() |
|
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param string $productClassId |
||
208 | * @param integer $quantity |
||
209 | * @return \Eccube\Service\CartService |
||
210 | */ |
||
211 | 127 | public function addProduct($productClassId, $quantity = 1) |
|
218 | |||
219 | /** |
||
220 | * @param string $productClassId |
||
221 | * @return integer |
||
222 | */ |
||
223 | 136 | public function getProductQuantity($productClassId) |
|
224 | { |
||
225 | 136 | $CartItem = $this->cart->getCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
|
232 | |||
233 | /** |
||
234 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
235 | * @param integer $quantity |
||
236 | * @return \Eccube\Service\CartService |
||
237 | * @throws CartException |
||
238 | */ |
||
239 | 174 | public function setProductQuantity($ProductClass, $quantity) |
|
316 | |||
317 | /** |
||
318 | * @param string $productClassId |
||
319 | * @return boolean |
||
320 | */ |
||
321 | 122 | public function canAddProduct($productClassId) |
|
336 | |||
337 | /** |
||
338 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
339 | * @return bool |
||
340 | */ |
||
341 | 49 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
|
379 | |||
380 | /** |
||
381 | * カートブロックに表示するカートを取得します。 |
||
382 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
383 | * |
||
384 | * @return \Eccube\Entity\Cart |
||
385 | */ |
||
386 | 81 | public function getCartObj() |
|
387 | { |
||
388 | |||
389 | 81 | foreach ($this->cart->getCartItems() as $CartItem) { |
|
390 | |||
391 | /** @var \Eccube\Entity\ProductClass $ProductClass */ |
||
392 | 5 | $ProductClass = $CartItem->getObject(); |
|
393 | 5 | if (!$ProductClass) { |
|
394 | $this->loadProductClassFromCartItem($CartItem); |
||
395 | |||
396 | $ProductClass = $CartItem->getObject(); |
||
397 | } |
||
398 | |||
399 | 5 | if ($ProductClass->getDelFlg()) { |
|
400 | // 商品情報が削除されていたらエラー |
||
401 | $this->setError('cart.product.delete'); |
||
402 | // カートから削除 |
||
403 | 81 | $this->removeProduct($ProductClass->getId()); |
|
404 | } |
||
405 | } |
||
406 | |||
407 | 81 | return $this->cart; |
|
408 | |||
409 | } |
||
410 | |||
411 | /** |
||
412 | * カートを取得します。 |
||
413 | * |
||
414 | * @return \Eccube\Entity\Cart |
||
415 | */ |
||
416 | 175 | public function getCart() |
|
457 | |||
458 | /** |
||
459 | * @param string $productClassId |
||
460 | * @return \Eccube\Service\CartService |
||
461 | */ |
||
462 | 26 | public function removeProduct($productClassId) |
|
488 | |||
489 | /** |
||
490 | * @param string $error |
||
491 | * @param string $productName |
||
492 | * @return \Eccube\Service\CartService |
||
493 | */ |
||
494 | 28 | public function addError($error = null, $productName = null) |
|
504 | |||
505 | /** |
||
506 | * @param string $productClassId |
||
507 | * @return \Eccube\Service\CartService |
||
508 | */ |
||
509 | 11 | public function upProductQuantity($productClassId) |
|
516 | |||
517 | /** |
||
518 | * @param string $productClassId |
||
519 | * @return \Eccube\Service\CartService |
||
520 | */ |
||
521 | 12 | public function downProductQuantity($productClassId) |
|
530 | |||
531 | /** |
||
532 | * @return array |
||
533 | */ |
||
534 | 95 | public function getProductTypes() |
|
547 | |||
548 | /** |
||
549 | * @return string[] |
||
550 | */ |
||
551 | 3 | public function getErrors() |
|
555 | |||
556 | /** |
||
557 | * @return string[] |
||
558 | */ |
||
559 | 1 | public function getMessages() |
|
563 | |||
564 | /** |
||
565 | * @param string $message |
||
566 | * @return \Eccube\Service\CartService |
||
567 | */ |
||
568 | 1 | public function setMessage($message) |
|
574 | |||
575 | /** |
||
576 | * @return string |
||
577 | */ |
||
578 | 1 | public function getError() |
|
582 | |||
583 | /** |
||
584 | * @param string $error |
||
585 | * @return \Eccube\Service\CartService |
||
586 | */ |
||
587 | 12 | public function setError($error = null) |
|
594 | |||
595 | /** |
||
596 | * 商品名を取得 |
||
597 | * |
||
598 | * @param ProductClass $ProductClass |
||
599 | * @return string |
||
600 | */ |
||
601 | 170 | private function getProductName(ProductClass $ProductClass) |
|
616 | |||
617 | |||
618 | /** |
||
619 | * 非公開商品の場合、カートから削除 |
||
620 | * |
||
621 | * @param ProductClass $ProductClass |
||
622 | * @return bool |
||
623 | */ |
||
624 | 171 | private function isProductDisplay(ProductClass $ProductClass) |
|
636 | |||
637 | |||
638 | /** |
||
639 | * 在庫数と販売制限数のチェック |
||
640 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
641 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
642 | * |
||
643 | * @param ProductClass $ProductClass |
||
644 | * @param $productName |
||
645 | * @param $quantity |
||
646 | * @return int|string |
||
647 | */ |
||
648 | 169 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
|
713 | |||
714 | } |
||
715 |