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 | 72 | ||
77 | public function __construct(\Eccube\Application $app) |
||
78 | 72 | { |
|
79 | $this->app = $app; |
||
80 | $this->session = $app['session']; |
||
81 | $this->entityManager = $app['orm.em']; |
||
82 | |||
83 | if ($this->session->has('cart')) { |
||
84 | $this->cart = $this->session->get('cart'); |
||
85 | } else { |
||
86 | $this->cart = new \Eccube\Entity\Cart(); |
||
87 | } |
||
88 | 72 | ||
89 | $this->loadProductClassFromCart(); |
||
90 | |||
91 | $this->BaseInfo = $app['eccube.repository.base_info']->get(); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
||
96 | 72 | */ |
|
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 | 46 | $softDeleteFilter->setExcludes(array( |
|
103 | 'Eccube\Entity\ProductClass', |
||
104 | )); |
||
105 | 46 | ||
106 | foreach ($this->cart->getCartItems() as $CartItem) { |
||
107 | $this->loadProductClassFromCartItem($CartItem); |
||
108 | 38 | } |
|
109 | 38 | ||
110 | $softDeleteFilter->setExcludes($excludes); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * CartItem に対応する ProductClass を設定します。 |
||
115 | * |
||
116 | 1 | * @param CartItem $CartItem |
|
117 | */ |
||
118 | 1 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
|
119 | 1 | { |
|
120 | $ProductClass = $this |
||
121 | 1 | ->entityManager |
|
122 | ->getRepository($CartItem->getClassName()) |
||
123 | 1 | ->find($CartItem->getClassId()); |
|
124 | |||
125 | 1 | $CartItem->setObject($ProductClass); |
|
126 | 1 | ||
127 | if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
||
128 | 1 | $this->setCanAddProductType($ProductClass->getProductType()); |
|
129 | } |
||
130 | } |
||
131 | |||
132 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
||
133 | { |
||
134 | if (is_null($this->ProductType)) { |
||
135 | $this->ProductType = $ProductType; |
||
136 | } |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | public function save() |
||
142 | 7 | { |
|
143 | return $this->session->set('cart', $this->cart); |
||
144 | } |
||
145 | |||
146 | 7 | public function unlock() |
|
147 | { |
||
148 | $this->cart |
||
149 | ->setLock(false) |
||
150 | ->setPreOrderId(null); |
||
151 | } |
||
152 | |||
153 | public function lock() |
||
154 | { |
||
155 | $this->cart |
||
156 | ->setLock(true) |
||
157 | ->setPreOrderId(null); |
||
158 | } |
||
159 | |||
160 | 3 | /** |
|
161 | * @return bool |
||
162 | 3 | */ |
|
163 | 3 | public function isLocked() |
|
167 | 3 | ||
168 | /** |
||
169 | * @param string $pre_order_id |
||
170 | 1 | * @return \Eccube\Service\CartService |
|
171 | */ |
||
172 | 1 | public function setPreOrderId($pre_order_id) |
|
173 | { |
||
174 | $this->cart->setPreOrderId($pre_order_id); |
||
175 | |||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | 5 | */ |
|
182 | public function getPreOrderId() |
||
183 | { |
||
184 | return $this->cart->getPreOrderId(); |
||
185 | } |
||
186 | 5 | ||
187 | 3 | /** |
|
188 | * @return \Eccube\Service\CartService |
||
189 | */ |
||
190 | public function clear() |
||
191 | { |
||
192 | $this->cart |
||
193 | 7 | ->setPreOrderId(null) |
|
194 | ->setLock(false) |
||
195 | ->clearCartItems(); |
||
196 | 6 | ||
197 | return $this; |
||
198 | } |
||
199 | 7 | ||
200 | public function getCanAddProductType() |
||
201 | { |
||
202 | return $this->ProductType; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param string $productClassId |
||
208 | * @param integer $quantity |
||
209 | 39 | * @return \Eccube\Service\CartService |
|
210 | */ |
||
211 | 39 | public function addProduct($productClassId, $quantity = 1) |
|
218 | |||
219 | /** |
||
220 | * @param string $productClassId |
||
221 | * @return integer |
||
222 | */ |
||
223 | public function getProductQuantity($productClassId) |
||
224 | { |
||
225 | $CartItem = $this->cart->getCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
||
226 | if ($CartItem) { |
||
227 | return $CartItem->getQuantity(); |
||
228 | } else { |
||
229 | return 0; |
||
232 | |||
233 | /** |
||
234 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
235 | * @param integer $quantity |
||
236 | * @return \Eccube\Service\CartService |
||
237 | 38 | * @throws CartException |
|
238 | */ |
||
239 | 39 | public function setProductQuantity($ProductClass, $quantity) |
|
319 | 5 | ||
320 | /** |
||
321 | * @param string $productClassId |
||
322 | 1 | * @return boolean |
|
323 | 1 | */ |
|
324 | public function canAddProduct($productClassId) |
||
339 | |||
340 | 1 | /** |
|
341 | 1 | * @param \Eccube\Entity\Master\ProductType $ProductType |
|
342 | * @return bool |
||
343 | */ |
||
344 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
||
382 | |||
383 | 3 | /** |
|
384 | * カートブロックに表示するカートを取得します。 |
||
385 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
386 | * |
||
387 | * @return \Eccube\Entity\Cart |
||
388 | */ |
||
389 | public function getCartObj() |
||
413 | |||
414 | /** |
||
415 | * カートを取得します。 |
||
416 | * |
||
417 | * @return \Eccube\Entity\Cart |
||
418 | 1 | */ |
|
419 | public function getCart() |
||
461 | 2 | ||
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 | * @return \Eccube\Service\CartService |
||
497 | */ |
||
498 | public function addError($error = null, $productName = null) |
||
521 | |||
522 | /** |
||
523 | * @param string $productClassId |
||
524 | * @return \Eccube\Service\CartService |
||
525 | */ |
||
526 | public function upProductQuantity($productClassId) |
||
533 | |||
534 | /** |
||
535 | * @param string $productClassId |
||
536 | 1 | * @return \Eccube\Service\CartService |
|
537 | */ |
||
538 | 1 | public function downProductQuantity($productClassId) |
|
547 | |||
548 | /** |
||
549 | * @return array |
||
550 | */ |
||
551 | public function getProductTypes() |
||
564 | |||
565 | /** |
||
566 | * @return string[] |
||
567 | */ |
||
568 | public function getErrors() |
||
572 | |||
573 | /** |
||
574 | * @return string[] |
||
575 | */ |
||
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 |