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 | * |
||
207 | * @param string $productClassId |
||
208 | * @param integer $quantity |
||
209 | * @return \Eccube\Service\CartService |
||
210 | */ |
||
211 | 44 | public function addProduct($productClassId, $quantity = 1) |
|
218 | |||
219 | /** |
||
220 | * @param string $productClassId |
||
221 | * @return integer |
||
222 | */ |
||
223 | 53 | public function getProductQuantity($productClassId) |
|
224 | { |
||
225 | 53 | $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 | 91 | public function setProductQuantity($ProductClass, $quantity) |
|
240 | { |
||
241 | 91 | if (!$ProductClass instanceof ProductClass) { |
|
242 | 86 | $ProductClass = $this->entityManager |
|
243 | 86 | ->getRepository('Eccube\Entity\ProductClass') |
|
244 | 86 | ->find($ProductClass); |
|
245 | 86 | if (!$ProductClass) { |
|
246 | 3 | throw new CartException('cart.product.delete'); |
|
247 | } |
||
248 | } |
||
249 | 88 | ||
250 | 1 | if (!$this->isProductDisplay($ProductClass)) { |
|
251 | 1 | throw new CartException('cart.product.not.status'); |
|
252 | } |
||
253 | |||
254 | 87 | $productName = $this->getProductName($ProductClass); |
|
255 | 87 | ||
256 | 87 | // 商品種別に紐づく配送業者を取得 |
|
257 | $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType()); |
||
258 | 87 | ||
259 | 85 | if (count($deliveries) == 0) { |
|
260 | // 商品種別が存在しなければエラー |
||
261 | $this->removeProduct($ProductClass->getId()); |
||
262 | $this->addError('cart.product.not.producttype', $productName); |
||
263 | 87 | throw new CartException('cart.product.not.producttype'); |
|
264 | } |
||
265 | 87 | ||
266 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
267 | |||
268 | if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) { |
||
269 | if (!$this->canAddProduct($ProductClass->getId())) { |
||
270 | // 複数配送対応でなければ商品種別が異なればエラー |
||
271 | throw new CartException('cart.product.type.kind'); |
||
272 | 87 | } |
|
273 | } else { |
||
274 | 87 | // 複数配送の場合、同一支払方法がなければエラー |
|
275 | 79 | if (!$this->canAddProductPayment($ProductClass->getProductType())) { |
|
276 | throw new CartException('cart.product.payment.kind'); |
||
277 | 79 | } |
|
278 | } |
||
279 | |||
280 | $tmp_subtotal = 0; |
||
281 | 8 | $tmp_quantity = 0; |
|
282 | 1 | foreach ($this->getCartObj()->getCartItems() as $cartitem) { |
|
283 | $pc = $cartitem->getObject(); |
||
284 | if ($pc->getId() != $ProductClass->getId()) { |
||
285 | // 追加された商品以外のtotal priceをセット |
||
286 | $tmp_subtotal += $cartitem->getTotalPrice(); |
||
287 | 87 | } |
|
288 | 87 | } |
|
289 | 87 | for ($i = 0; $i < $quantity; $i++) { |
|
290 | 8 | $tmp_subtotal += $ProductClass->getPrice02IncTax(); |
|
291 | 8 | if ($tmp_subtotal > $this->app['config']['max_total_fee']) { |
|
292 | $this->setError('cart.over.price_limit'); |
||
293 | 87 | break; |
|
294 | } |
||
295 | $tmp_quantity++; |
||
296 | 87 | } |
|
297 | 87 | if ($tmp_quantity == 0) { |
|
298 | 87 | // 数量が0の場合、エラー |
|
299 | 1 | throw new CartException('cart.over.price_limit'); |
|
300 | 1 | } |
|
301 | |||
302 | 86 | // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
|
303 | $quantity = $this->setProductLimit($ProductClass, $productName, $tmp_quantity); |
||
304 | 87 | ||
305 | // 新しい数量でカート内商品を登録する |
||
306 | 87 | if (0 < $quantity) { |
|
307 | $CartItem = new CartItem(); |
||
308 | $CartItem |
||
309 | ->setClassName('Eccube\Entity\ProductClass') |
||
310 | ->setClassId((string)$ProductClass->getId()) |
||
311 | ->setPrice($ProductClass->getPrice02IncTax()) |
||
312 | 87 | ->setQuantity($quantity); |
|
313 | 2 | ||
314 | 1 | $this->cart->setCartItem($CartItem); |
|
315 | 1 | } |
|
316 | |||
317 | 1 | return $this; |
|
318 | 1 | } |
|
319 | |||
320 | /** |
||
321 | 87 | * @param string $productClassId |
|
322 | 2 | * @return boolean |
|
323 | 2 | */ |
|
324 | public function canAddProduct($productClassId) |
||
325 | 87 | { |
|
326 | 3 | $ProductClass = $this |
|
327 | ->entityManager |
||
328 | ->getRepository('\Eccube\Entity\ProductClass') |
||
329 | 87 | ->find($productClassId); |
|
330 | |||
331 | 87 | if (!$ProductClass) { |
|
332 | 87 | return false; |
|
333 | 87 | } |
|
334 | 87 | ||
335 | $ProductType = $ProductClass->getProductType(); |
||
336 | 87 | ||
337 | return $this->ProductType == $ProductType; |
||
338 | 87 | } |
|
339 | |||
340 | /** |
||
341 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
||
382 | 7 | ||
383 | 7 | /** |
|
384 | 7 | * カートブロックに表示するカートを取得します。 |
|
385 | 7 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
|
386 | 6 | * |
|
387 | 7 | * @return \Eccube\Entity\Cart |
|
388 | */ |
||
389 | public function getCartObj() |
||
390 | { |
||
391 | |||
392 | 7 | foreach ($this->cart->getCartItems() as $CartItem) { |
|
393 | 6 | ||
394 | 6 | /** @var \Eccube\Entity\ProductClass $ProductClass */ |
|
395 | $ProductClass = $CartItem->getObject(); |
||
396 | if (!$ProductClass) { |
||
397 | $this->loadProductClassFromCartItem($CartItem); |
||
398 | 1 | ||
399 | $ProductClass = $CartItem->getObject(); |
||
400 | } |
||
401 | |||
402 | if ($ProductClass->getDelFlg()) { |
||
403 | // 商品情報が削除されていたらエラー |
||
404 | $this->setError('cart.product.delete'); |
||
405 | // カートから削除 |
||
406 | $this->removeProduct($ProductClass->getId()); |
||
407 | 158 | } |
|
408 | } |
||
409 | 158 | ||
410 | 48 | return $this->cart; |
|
411 | 48 | ||
412 | 48 | } |
|
413 | |||
414 | 48 | /** |
|
415 | * カートを取得します。 |
||
416 | * |
||
417 | 48 | * @return \Eccube\Entity\Cart |
|
418 | */ |
||
419 | 48 | public function getCart() |
|
420 | 48 | { |
|
421 | foreach ($this->cart->getCartItems() as $CartItem) { |
||
422 | |||
423 | /** @var \Eccube\Entity\ProductClass $ProductClass */ |
||
424 | $ProductClass = $CartItem->getObject(); |
||
425 | 48 | if (!$ProductClass) { |
|
426 | 48 | $this->loadProductClassFromCartItem($CartItem); |
|
427 | 48 | ||
428 | $ProductClass = $CartItem->getObject(); |
||
429 | } |
||
430 | 48 | ||
431 | if ($ProductClass->getDelFlg() == Constant::DISABLED) { |
||
432 | 48 | // 商品情報が有効 |
|
433 | |||
434 | if (!$this->isProductDisplay($ProductClass)) { |
||
435 | $this->setError('cart.product.not.status'); |
||
436 | } else { |
||
437 | |||
438 | $productName = $this->getProductName($ProductClass); |
||
439 | 158 | ||
440 | // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している) |
||
441 | $quantity = $this->setProductLimit($ProductClass, $productName, $CartItem->getQuantity()); |
||
442 | |||
443 | 158 | /// 個数が異なれば、新しい数量でカート内商品を更新する |
|
444 | if ((0 < $quantity) && ($CartItem->getQuantity() != $quantity)) { |
||
445 | // 個数が異なれば更新 |
||
446 | $CartItem->setQuantity($quantity); |
||
447 | $this->cart->setCartItem($CartItem); |
||
448 | } |
||
449 | } |
||
450 | 9 | ||
451 | } else { |
||
452 | 9 | // 商品情報が削除されていたらエラー |
|
453 | $this->setError('cart.product.delete'); |
||
454 | // カートから削除 |
||
455 | 9 | $this->removeProduct($ProductClass->getId()); |
|
456 | } |
||
457 | } |
||
458 | 1 | ||
459 | 1 | return $this->cart; |
|
460 | } |
||
461 | 1 | ||
462 | 1 | /** |
|
463 | * @param string $productClassId |
||
464 | * @return \Eccube\Service\CartService |
||
465 | */ |
||
466 | 1 | public function removeProduct($productClassId) |
|
467 | { |
||
468 | $this->cart->removeCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId); |
||
469 | 1 | ||
470 | // 支払方法の再設定 |
||
471 | 1 | if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) { |
|
472 | |||
473 | // 複数配送対応 |
||
474 | 9 | $productTypes = array(); |
|
475 | foreach ($this->getCart()->getCartItems() as $item) { |
||
476 | /* @var $ProductClass \Eccube\Entity\ProductClass */ |
||
477 | $ProductClass = $item->getObject(); |
||
478 | $productTypes[] = $ProductClass->getProductType(); |
||
479 | } |
||
480 | |||
481 | // 配送業者を取得 |
||
482 | 4 | $deliveries = $this->entityManager->getRepository('Eccube\Entity\Delivery')->getDeliveries($productTypes); |
|
483 | |||
484 | 4 | // 支払方法を取得 |
|
485 | 4 | $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries); |
|
486 | 4 | ||
487 | 3 | $this->getCart()->setPayments($payments); |
|
488 | } |
||
489 | 4 | ||
490 | return $this; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * @param string $error |
||
495 | * @param string $productName |
||
496 | 4 | * @return \Eccube\Service\CartService |
|
497 | */ |
||
498 | 4 | public function addError($error = null, $productName = null) |
|
508 | 5 | ||
509 | /** |
||
510 | 5 | * @param string $productClassId |
|
511 | * @return \Eccube\Service\CartService |
||
512 | 5 | */ |
|
513 | 1 | public function upProductQuantity($productClassId) |
|
514 | { |
||
515 | 4 | $quantity = $this->getProductQuantity($productClassId) + 1; |
|
516 | $this->setProductQuantity($productClassId, $quantity); |
||
517 | |||
518 | 5 | return $this; |
|
519 | } |
||
520 | |||
521 | /** |
||
522 | * @param string $productClassId |
||
523 | * @return \Eccube\Service\CartService |
||
524 | 20 | */ |
|
525 | public function downProductQuantity($productClassId) |
||
534 | |||
535 | /** |
||
536 | * @return array |
||
537 | */ |
||
538 | public function getProductTypes() |
||
539 | { |
||
540 | 3 | ||
541 | $productTypes = array(); |
||
542 | 3 | foreach ($this->getCart()->getCartItems() as $item) { |
|
543 | /* @var $ProductClass \Eccube\Entity\ProductClass */ |
||
544 | $ProductClass = $item->getObject(); |
||
545 | $productTypes[] = $ProductClass->getProductType(); |
||
546 | } |
||
547 | |||
548 | 1 | return array_unique($productTypes); |
|
549 | |||
550 | 1 | } |
|
551 | |||
552 | /** |
||
553 | * @return string[] |
||
554 | */ |
||
555 | public function getErrors() |
||
556 | { |
||
557 | 1 | return $this->errors; |
|
558 | } |
||
559 | 1 | ||
560 | /** |
||
561 | 1 | * @return string[] |
|
562 | */ |
||
563 | public function getMessages() |
||
567 | 1 | ||
568 | /** |
||
569 | 1 | * @param string $message |
|
570 | * @return \Eccube\Service\CartService |
||
571 | */ |
||
572 | public function setMessage($message) |
||
573 | { |
||
574 | $this->messages[] = $message; |
||
575 | |||
576 | 1 | return $this; |
|
577 | } |
||
578 | 1 | ||
579 | 1 | /** |
|
580 | 1 | * @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 |