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 | 227 | public function __construct(\Eccube\Application $app) |
|
78 | { |
||
79 | 227 | $this->app = $app; |
|
80 | 227 | $this->session = $app['session']; |
|
81 | 227 | $this->entityManager = $app['orm.em']; |
|
82 | |||
83 | 227 | if ($this->session->has('cart')) { |
|
84 | $this->cart = $this->session->get('cart'); |
||
85 | } else { |
||
86 | 227 | $this->cart = new \Eccube\Entity\Cart(); |
|
87 | } |
||
88 | |||
89 | 227 | $this->loadProductClassFromCart(); |
|
90 | |||
91 | 227 | $this->BaseInfo = $app['eccube.repository.base_info']->get(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
||
96 | */ |
||
97 | 227 | protected function loadProductClassFromCart() |
|
98 | { |
||
99 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
100 | 227 | $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete'); |
|
101 | 227 | $excludes = $softDeleteFilter->getExcludes(); |
|
102 | 227 | $softDeleteFilter->setExcludes(array( |
|
103 | 227 | 'Eccube\Entity\ProductClass', |
|
104 | )); |
||
105 | |||
106 | 227 | foreach ($this->cart->getCartItems() as $CartItem) { |
|
107 | 227 | $this->loadProductClassFromCartItem($CartItem); |
|
108 | } |
||
109 | |||
110 | 227 | $softDeleteFilter->setExcludes($excludes); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * CartItem に対応する ProductClass を設定します。 |
||
115 | * |
||
116 | * @param CartItem $CartItem |
||
117 | */ |
||
118 | 78 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
|
119 | { |
||
120 | $ProductClass = $this |
||
121 | 78 | ->entityManager |
|
122 | 78 | ->getRepository($CartItem->getClassName()) |
|
123 | 78 | ->find($CartItem->getClassId()); |
|
124 | |||
125 | 78 | $CartItem->setObject($ProductClass); |
|
126 | |||
127 | 78 | if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
|
128 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | 126 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
|
140 | |||
141 | 119 | public function save() |
|
142 | { |
||
143 | 119 | return $this->session->set('cart', $this->cart); |
|
144 | } |
||
145 | |||
146 | 7 | public function unlock() |
|
152 | |||
153 | 76 | public function lock() |
|
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | 68 | public function isLocked() |
|
164 | { |
||
165 | 68 | return $this->cart->getLock(); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $pre_order_id |
||
170 | * @return \Eccube\Service\CartService |
||
171 | */ |
||
172 | 58 | public function setPreOrderId($pre_order_id) |
|
173 | { |
||
174 | 58 | $this->cart->setPreOrderId($pre_order_id); |
|
175 | |||
176 | 58 | return $this; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | 57 | public function getPreOrderId() |
|
183 | { |
||
184 | 57 | return $this->cart->getPreOrderId(); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return \Eccube\Service\CartService |
||
189 | */ |
||
190 | 19 | public function clear() |
|
191 | { |
||
192 | 19 | $this->cart |
|
193 | 19 | ->setPreOrderId(null) |
|
194 | 19 | ->setLock(false) |
|
195 | 19 | ->clearCartItems(); |
|
196 | |||
197 | 19 | 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 | 82 | public function addProduct($productClassId, $quantity = 1) |
|
218 | |||
219 | /** |
||
220 | * @param string $productClassId |
||
221 | * @return integer |
||
222 | */ |
||
223 | 91 | public function getProductQuantity($productClassId) |
|
224 | { |
||
225 | 91 | $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 | 129 | public function setProductQuantity($ProductClass, $quantity) |
|
316 | |||
317 | /** |
||
318 | * @param string $productClassId |
||
319 | * @return boolean |
||
320 | */ |
||
321 | 117 | public function canAddProduct($productClassId) |
|
336 | |||
337 | /** |
||
338 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
339 | * @return bool |
||
340 | */ |
||
341 | 9 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
|
379 | |||
380 | /** |
||
381 | * カートブロックに表示するカートを取得します。 |
||
382 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
383 | * |
||
384 | * @return \Eccube\Entity\Cart |
||
385 | */ |
||
386 | 76 | public function getCartObj() |
|
410 | |||
411 | /** |
||
412 | * カートを取得します。 |
||
413 | * |
||
414 | * @return \Eccube\Entity\Cart |
||
415 | */ |
||
416 | 130 | public function getCart() |
|
457 | |||
458 | /** |
||
459 | * @param string $productClassId |
||
460 | * @return \Eccube\Service\CartService |
||
461 | */ |
||
462 | 27 | public function removeProduct($productClassId) |
|
488 | |||
489 | /** |
||
490 | * @param string $error |
||
491 | * @param string $productName |
||
492 | * @return \Eccube\Service\CartService |
||
493 | */ |
||
494 | 22 | public function addError($error = null, $productName = null) |
|
504 | |||
505 | /** |
||
506 | * @param string $productClassId |
||
507 | * @return \Eccube\Service\CartService |
||
508 | */ |
||
509 | 10 | public function upProductQuantity($productClassId) |
|
516 | |||
517 | /** |
||
518 | * @param string $productClassId |
||
519 | * @return \Eccube\Service\CartService |
||
520 | */ |
||
521 | 11 | public function downProductQuantity($productClassId) |
|
533 | |||
534 | /** |
||
535 | * @return array |
||
536 | */ |
||
537 | 58 | public function getProductTypes() |
|
550 | |||
551 | /** |
||
552 | * @return string[] |
||
553 | */ |
||
554 | 3 | public function getErrors() |
|
558 | |||
559 | /** |
||
560 | * @return string[] |
||
561 | */ |
||
562 | 1 | public function getMessages() |
|
566 | |||
567 | /** |
||
568 | * @param string $message |
||
569 | * @return \Eccube\Service\CartService |
||
570 | */ |
||
571 | 1 | public function setMessage($message) |
|
577 | |||
578 | /** |
||
579 | * @return string |
||
580 | */ |
||
581 | 1 | public function getError() |
|
585 | |||
586 | /** |
||
587 | * @param string $error |
||
588 | * @return \Eccube\Service\CartService |
||
589 | */ |
||
590 | 10 | public function setError($error = null) |
|
597 | |||
598 | /** |
||
599 | * 商品名を取得 |
||
600 | * |
||
601 | * @param ProductClass $ProductClass |
||
602 | * @return string |
||
603 | */ |
||
604 | 125 | private function getProductName(ProductClass $ProductClass) |
|
619 | |||
620 | |||
621 | /** |
||
622 | * 非公開商品の場合、カートから削除 |
||
623 | * |
||
624 | * @param ProductClass $ProductClass |
||
625 | * @return bool |
||
626 | */ |
||
627 | 126 | private function isProductDisplay(ProductClass $ProductClass) |
|
639 | |||
640 | |||
641 | /** |
||
642 | * 在庫数と販売制限数のチェック |
||
643 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
644 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
645 | * |
||
646 | * @param ProductClass $ProductClass |
||
647 | * @param $productName |
||
648 | * @param $quantity |
||
649 | * @return int|string |
||
650 | */ |
||
651 | 124 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
|
716 | |||
717 | } |
||
718 |