Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * @param ProductClass|integer $ProductClass 商品規格エンティティ、またはそのID |
||
| 207 | * @return CartItem |
||
| 208 | * @throws CartException |
||
| 209 | */ |
||
| 210 | public function generateCartItem($ProductClass) |
||
| 211 | 44 | { |
|
| 212 | View Code Duplication | if (!$ProductClass instanceof ProductClass) { |
|
| 229 | 51 | ||
| 230 | /** |
||
| 231 | * カート商品を追加する |
||
| 232 | * |
||
| 233 | * @param CartItem $CartItem |
||
| 234 | * @return \Eccube\Service\CartService |
||
| 235 | */ |
||
| 236 | public function addCartItem($CartItem) |
||
| 243 | 86 | ||
| 244 | 86 | /** |
|
| 245 | 86 | * カート商品の数量を取得する |
|
| 246 | 3 | * |
|
| 247 | * @param CartItem $CartItem |
||
| 248 | * @return int |
||
| 249 | 88 | */ |
|
| 250 | 1 | public function getCartItemQuantity($CartItem) |
|
| 258 | 87 | ||
| 259 | 85 | /** |
|
| 260 | * @param CartItem $CartItem |
||
| 261 | * @param integer $quantity |
||
| 262 | * @return \Eccube\Service\CartService |
||
| 263 | 87 | * @throws CartException |
|
| 264 | */ |
||
| 265 | 87 | public function setCartItemQuantity($CartItem, $quantity) |
|
| 351 | |||
| 352 | 79 | /** |
|
| 353 | * |
||
| 354 | * @param string $productClassId |
||
| 355 | * @param integer $quantity |
||
| 356 | 79 | * @return \Eccube\Service\CartService |
|
| 357 | */ |
||
| 358 | 79 | public function addProduct($productClassId, $quantity = 1) |
|
| 365 | 9 | ||
| 366 | /** |
||
| 367 | * @param string $productClassId |
||
| 368 | 9 | * @return integer |
|
| 369 | 9 | */ |
|
| 370 | 9 | public function getProductQuantity($productClassId) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
| 382 | 7 | * @param integer $quantity |
|
| 383 | 7 | * @return \Eccube\Service\CartService |
|
| 384 | 7 | * @throws CartException |
|
| 385 | 7 | */ |
|
| 386 | 6 | public function setProductQuantity($ProductClass, $quantity) |
|
| 466 | 1 | ||
| 467 | /** |
||
| 468 | * @param string $productClassId |
||
| 469 | 1 | * @return boolean |
|
| 470 | */ |
||
| 471 | 1 | public function canAddProduct($productClassId) |
|
| 486 | 4 | ||
| 487 | 3 | /** |
|
| 488 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
| 489 | 4 | * @return bool |
|
| 490 | */ |
||
| 491 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
||
| 529 | |||
| 530 | 20 | /** |
|
| 531 | 20 | * カートブロックに表示するカートを取得します。 |
|
| 532 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
| 533 | 20 | * |
|
| 534 | * @return \Eccube\Entity\Cart |
||
| 535 | */ |
||
| 536 | public function getCartObj() |
||
| 560 | |||
| 561 | 1 | /** |
|
| 562 | * カートを取得します。 |
||
| 563 | * |
||
| 564 | * @return \Eccube\Entity\Cart |
||
| 565 | */ |
||
| 566 | public function getCart() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @param string $productClassId |
||
| 611 | * @return \Eccube\Service\CartService |
||
| 612 | */ |
||
| 613 | public function removeProduct($productClassId) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param string $error |
||
| 642 | * @param string $productName |
||
| 643 | * @return \Eccube\Service\CartService |
||
| 644 | */ |
||
| 645 | public function addError($error = null, $productName = null) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param string $productClassId |
||
| 658 | * @return \Eccube\Service\CartService |
||
| 659 | */ |
||
| 660 | public function upProductQuantity($productClassId) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @param string $productClassId |
||
| 670 | * @return \Eccube\Service\CartService |
||
| 671 | */ |
||
| 672 | public function downProductQuantity($productClassId) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @return array |
||
| 684 | */ |
||
| 685 | public function getProductTypes() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @return string[] |
||
| 701 | */ |
||
| 702 | public function getErrors() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @return string[] |
||
| 709 | */ |
||
| 710 | public function getMessages() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @param string $message |
||
| 717 | * @return \Eccube\Service\CartService |
||
| 718 | */ |
||
| 719 | public function setMessage($message) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return string |
||
| 728 | */ |
||
| 729 | public function getError() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param string $error |
||
| 736 | * @return \Eccube\Service\CartService |
||
| 737 | */ |
||
| 738 | public function setError($error = null) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * 商品名を取得 |
||
| 748 | * |
||
| 749 | * @param ProductClass $ProductClass |
||
| 750 | * @return string |
||
| 751 | */ |
||
| 752 | private function getProductName(ProductClass $ProductClass) |
||
| 767 | |||
| 768 | |||
| 769 | /** |
||
| 770 | * 非公開商品の場合、カートから削除 |
||
| 771 | * |
||
| 772 | * @param ProductClass $ProductClass |
||
| 773 | * @return bool |
||
| 774 | */ |
||
| 775 | private function isProductDisplay(ProductClass $ProductClass) |
||
| 787 | |||
| 788 | |||
| 789 | /** |
||
| 790 | * 在庫数と販売制限数のチェック |
||
| 791 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
| 792 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
| 793 | * |
||
| 794 | * @param ProductClass $ProductClass |
||
| 795 | * @param $productName |
||
| 796 | * @param $quantity |
||
| 797 | * @return int チェック後に更新した個数 |
||
| 798 | */ |
||
| 799 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
||
| 861 | |||
| 862 | /** |
||
| 863 | * @return \Eccube\Service\CartCompareService |
||
| 864 | */ |
||
| 865 | public function generateCartCompareService() |
||
| 869 | } |
||
| 870 |