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 | * @param int $cart_no |
||
| 354 | * @return \Eccube\Service\CartService |
||
| 355 | */ |
||
| 356 | 79 | public function removeCartNo($cart_no) |
|
| 363 | |||
| 364 | /** |
||
| 365 | 9 | * 支払い方法を再設定する |
|
| 366 | * |
||
| 367 | * @return $this |
||
| 368 | 9 | */ |
|
| 369 | 9 | protected function resetPayment() |
|
| 393 | 6 | ||
| 394 | 6 | /** |
|
| 395 | * |
||
| 396 | * @param string $productClassId |
||
| 397 | * @param integer $quantity |
||
| 398 | 1 | * @return \Eccube\Service\CartService |
|
| 399 | */ |
||
| 400 | public function addProduct($productClassId, $quantity = 1) |
||
| 407 | 158 | ||
| 408 | /** |
||
| 409 | 158 | * @param string $productClassId |
|
| 410 | 48 | * @return integer |
|
| 411 | 48 | */ |
|
| 412 | 48 | public function getProductQuantity($productClassId) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
| 424 | * @param integer $quantity |
||
| 425 | 48 | * @return \Eccube\Service\CartService |
|
| 426 | 48 | * @throws CartException |
|
| 427 | 48 | */ |
|
| 428 | public function setProductQuantity($ProductClass, $quantity) |
||
| 508 | 5 | ||
| 509 | /** |
||
| 510 | 5 | * @param string $productClassId |
|
| 511 | * @return boolean |
||
| 512 | 5 | */ |
|
| 513 | 1 | public function canAddProduct($productClassId) |
|
| 528 | 20 | ||
| 529 | /** |
||
| 530 | 20 | * @param \Eccube\Entity\Master\ProductType $ProductType |
|
| 531 | 20 | * @return bool |
|
| 532 | */ |
||
| 533 | 20 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * カートブロックに表示するカートを取得します。 |
||
| 574 | * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。 |
||
| 575 | * |
||
| 576 | 1 | * @return \Eccube\Entity\Cart |
|
| 577 | */ |
||
| 578 | 1 | public function getCartObj() |
|
| 602 | |||
| 603 | /** |
||
| 604 | * カートを取得します。 |
||
| 605 | * |
||
| 606 | * @return \Eccube\Entity\Cart |
||
| 607 | */ |
||
| 608 | public function getCart() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @param string $productClassId |
||
| 659 | * @return \Eccube\Service\CartService |
||
| 660 | */ |
||
| 661 | public function removeProduct($productClassId) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param string $error |
||
| 671 | * @param string $productName |
||
| 672 | * @return \Eccube\Service\CartService |
||
| 673 | */ |
||
| 674 | public function addError($error = null, $productName = null) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param string $productClassId |
||
| 687 | * @return \Eccube\Service\CartService |
||
| 688 | */ |
||
| 689 | public function upProductQuantity($productClassId) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param string $productClassId |
||
| 699 | * @return \Eccube\Service\CartService |
||
| 700 | */ |
||
| 701 | public function downProductQuantity($productClassId) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @param int $cart_no |
||
| 713 | * @return \Eccube\Service\CartService |
||
| 714 | */ |
||
| 715 | public function upCartNoQuantity($cart_no) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param int $cart_no |
||
| 727 | * @return \Eccube\Service\CartService |
||
| 728 | */ |
||
| 729 | public function downCartNoQuantity($cart_no) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @return array |
||
| 744 | */ |
||
| 745 | public function getProductTypes() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @return string[] |
||
| 761 | */ |
||
| 762 | public function getErrors() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * @return string[] |
||
| 769 | */ |
||
| 770 | public function getMessages() |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @param string $message |
||
| 777 | * @return \Eccube\Service\CartService |
||
| 778 | */ |
||
| 779 | public function setMessage($message) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @return string |
||
| 788 | */ |
||
| 789 | public function getError() |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @param string $error |
||
| 796 | * @return \Eccube\Service\CartService |
||
| 797 | */ |
||
| 798 | public function setError($error = null) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * 商品名を取得 |
||
| 808 | * |
||
| 809 | * @param ProductClass $ProductClass |
||
| 810 | * @return string |
||
| 811 | */ |
||
| 812 | private function getProductName(ProductClass $ProductClass) |
||
| 827 | |||
| 828 | |||
| 829 | /** |
||
| 830 | * 非公開商品の場合、カートから削除 |
||
| 831 | * |
||
| 832 | * @param ProductClass $ProductClass |
||
| 833 | * @return bool |
||
| 834 | */ |
||
| 835 | private function isProductDisplay(ProductClass $ProductClass) |
||
| 847 | |||
| 848 | |||
| 849 | /** |
||
| 850 | * 在庫数と販売制限数のチェック |
||
| 851 | * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、 |
||
| 852 | * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する |
||
| 853 | * |
||
| 854 | * @param ProductClass $ProductClass |
||
| 855 | * @param $productName |
||
| 856 | * @param $quantity |
||
| 857 | * @return int チェック後に更新した個数 |
||
| 858 | */ |
||
| 859 | private function setProductLimit(ProductClass $ProductClass, $productName, $quantity) |
||
| 921 | |||
| 922 | /** |
||
| 923 | * @return \Eccube\Service\CartCompareService |
||
| 924 | */ |
||
| 925 | public function generateCartCompareService() |
||
| 929 | } |
||
| 930 |