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 | 30 | public function __construct(\Eccube\Application $app) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。 |
||
| 96 | */ |
||
| 97 | 30 | 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 | 30 | foreach ($this->cart->getCartItems() as $CartItem) { |
|
| 107 | $this->loadProductClassFromCartItem($CartItem); |
||
| 108 | 30 | } |
|
| 109 | |||
| 110 | $softDeleteFilter->setExcludes($excludes); |
||
| 111 | 30 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * CartItem に対応する ProductClass を設定します。 |
||
| 115 | * |
||
| 116 | * @param CartItem $CartItem |
||
| 117 | */ |
||
| 118 | 5 | protected function loadProductClassFromCartItem(CartItem $CartItem) |
|
| 119 | { |
||
| 120 | $ProductClass = $this |
||
| 121 | 5 | ->entityManager |
|
| 122 | ->getRepository($CartItem->getClassName()) |
||
| 123 | ->find($CartItem->getClassId()); |
||
| 124 | |||
| 125 | $CartItem->setObject($ProductClass); |
||
| 126 | |||
| 127 | if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) { |
||
| 128 | $this->setCanAddProductType($ProductClass->getProductType()); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | 22 | public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType) |
|
| 133 | { |
||
| 134 | if (is_null($this->ProductType)) { |
||
| 135 | 22 | $this->ProductType = $ProductType; |
|
| 136 | } |
||
| 137 | |||
| 138 | 18 | return $this; |
|
| 139 | 18 | } |
|
| 140 | |||
| 141 | public function save() |
||
| 145 | |||
| 146 | 1 | public function unlock() |
|
| 147 | { |
||
| 148 | 1 | $this->cart |
|
| 149 | 1 | ->setLock(false) |
|
| 150 | ->setPreOrderId(null); |
||
| 151 | 1 | } |
|
| 152 | |||
| 153 | 3 | public function lock() |
|
| 154 | { |
||
| 155 | 3 | $this->cart |
|
| 156 | 3 | ->setLock(true) |
|
| 157 | ->setPreOrderId(null); |
||
| 158 | 3 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function isLocked() |
||
| 164 | { |
||
| 165 | return $this->cart->getLock(); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $pre_order_id |
||
| 170 | * @return \Eccube\Service\CartService |
||
| 171 | */ |
||
| 172 | 4 | public function setPreOrderId($pre_order_id) |
|
| 173 | { |
||
| 174 | $this->cart->setPreOrderId($pre_order_id); |
||
| 175 | |||
| 176 | 4 | return $this; |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getPreOrderId() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return \Eccube\Service\CartService |
||
| 189 | */ |
||
| 190 | 1 | public function clear() |
|
| 191 | { |
||
| 192 | 1 | $this->cart |
|
| 193 | 1 | ->setPreOrderId(null) |
|
| 194 | 1 | ->setLock(false) |
|
| 195 | ->clearCartItems(); |
||
| 196 | |||
| 197 | 1 | return $this; |
|
| 198 | } |
||
| 199 | |||
| 200 | public function getCanAddProductType() |
||
| 201 | { |
||
| 202 | return $this->ProductType; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * |
||
| 207 | * @param string $productClassId |
||
| 208 | * @param integer $quantity |
||
| 209 | * @return \Eccube\Service\CartService |
||
| 210 | */ |
||
| 211 | 2 | public function addProduct($productClassId, $quantity = 1) |
|
| 212 | { |
||
| 213 | $quantity += $this->getProductQuantity($productClassId); |
||
| 214 | $this->setProductQuantity($productClassId, $quantity); |
||
| 215 | |||
| 216 | 2 | return $this; |
|
| 217 | 1 | } |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $productClassId |
||
| 221 | * @return integer |
||
| 222 | */ |
||
| 223 | 2 | public function getProductQuantity($productClassId) |
|
| 224 | { |
||
| 225 | $CartItem = $this->cart->getCartItemByIdentifier('Eccube\Entity\ProductClass', (string) $productClassId); |
||
| 226 | 1 | if ($CartItem) { |
|
| 227 | return $CartItem->getQuantity(); |
||
| 228 | } else { |
||
| 229 | 2 | return 0; |
|
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param \Eccube\Entity\ProductClass|integer $ProductClass |
||
| 235 | * @param integer $quantity |
||
| 236 | * @return \Eccube\Service\CartService |
||
| 237 | * @throws CartException |
||
| 238 | */ |
||
| 239 | 19 | public function setProductQuantity($ProductClass, $quantity) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $productClassId |
||
| 343 | * @return boolean |
||
| 344 | */ |
||
| 345 | 18 | public function canAddProduct($productClassId) |
|
| 346 | { |
||
| 347 | $ProductClass = $this |
||
| 348 | 18 | ->entityManager |
|
| 349 | 18 | ->getRepository('\Eccube\Entity\ProductClass') |
|
| 350 | ->find($productClassId); |
||
| 351 | |||
| 352 | 18 | if (!$ProductClass) { |
|
| 353 | return false; |
||
| 354 | } |
||
| 355 | |||
| 356 | $ProductType = $ProductClass->getProductType(); |
||
| 357 | |||
| 358 | 18 | return $this->ProductType == $ProductType; |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param \Eccube\Entity\Master\ProductType $ProductType |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | 2 | public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType) |
|
| 366 | { |
||
| 367 | $deliveries = $this |
||
| 368 | ->entityManager |
||
| 369 | ->getRepository('\Eccube\Entity\Delivery') |
||
| 370 | ->findBy(array('ProductType' => $ProductType)); |
||
| 371 | |||
| 372 | // 支払方法を取得 |
||
| 373 | $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries); |
||
| 374 | |||
| 375 | if ($this->getCart()->getTotalPrice() < 1) { |
||
| 376 | // カートになければ支払方法を全て設定 |
||
| 377 | $this->getCart()->setPayments($payments); |
||
| 378 | 2 | return true; |
|
| 379 | } |
||
| 380 | |||
| 381 | // カートに存在している支払方法と追加された商品の支払方法チェック |
||
| 382 | $arr = array(); |
||
| 383 | foreach ($payments as $payment) { |
||
| 384 | foreach ($this->getCart()->getPayments() as $p) { |
||
| 385 | if ($payment['id'] == $p['id']) { |
||
| 386 | $arr[] = $payment; |
||
| 387 | break; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | if (count($arr) > 0) { |
||
| 393 | $this->getCart()->setPayments($arr); |
||
| 394 | return true; |
||
| 395 | } |
||
| 396 | |||
| 397 | // 支払条件に一致しない |
||
| 398 | return false; |
||
| 399 | |||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * カートを取得します。 |
||
| 404 | * |
||
| 405 | * @return \Eccube\Entity\Cart |
||
| 406 | */ |
||
| 407 | 15 | public function getCart() |
|
| 408 | { |
||
| 409 | 15 | foreach ($this->cart->getCartItems() as $CartItem) { |
|
| 410 | $ProductClass = $CartItem->getObject(); |
||
| 411 | 2 | if (!$ProductClass) { |
|
| 412 | $this->loadProductClassFromCartItem($CartItem); |
||
| 413 | |||
| 414 | $ProductClass = $CartItem->getObject(); |
||
| 415 | } |
||
| 416 | |||
| 417 | if ($ProductClass->getDelFlg() == Constant::DISABLED) { |
||
| 418 | // 商品情報が有効 |
||
| 419 | $stockUnlimited = $ProductClass->getStockUnlimited(); |
||
| 420 | 2 | if ($stockUnlimited == Constant::DISABLED && $ProductClass->getStock() < 1) { |
|
| 421 | // 在庫がなければカートから削除 |
||
| 422 | $this->setError('cart.zero.stock'); |
||
| 423 | $this->removeProduct($ProductClass->getId()); |
||
| 424 | } else { |
||
| 425 | $quantity = $CartItem->getQuantity(); |
||
| 426 | $saleLimit = $ProductClass->getSaleLimit(); |
||
| 427 | 2 | if ($stockUnlimited == Constant::DISABLED && $ProductClass->getStock() < $quantity) { |
|
| 428 | // 購入数が在庫数を超えている場合、メッセージを表示 |
||
| 429 | $this->setError('cart.over.stock'); |
||
| 430 | } elseif (!is_null($saleLimit) && $saleLimit < $quantity) { |
||
| 431 | // 購入数が販売制限数を超えている場合、メッセージを表示 |
||
| 432 | $this->setError('cart.over.sale_limit'); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } else { |
||
| 436 | // 商品情報が削除されていたらエラー |
||
| 437 | $this->setError('cart.product.delete'); |
||
| 438 | // カートから削除 |
||
| 439 | $this->removeProduct($ProductClass->getId()); |
||
| 440 | 2 | } |
|
| 441 | 15 | } |
|
| 442 | |||
| 443 | 15 | return $this->cart; |
|
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param string $productClassId |
||
| 448 | * @return \Eccube\Service\CartService |
||
| 449 | */ |
||
| 450 | 1 | public function removeProduct($productClassId) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $error |
||
| 479 | * @param string $productName |
||
| 480 | * @return \Eccube\Service\CartService |
||
| 481 | */ |
||
| 482 | 3 | public function addError($error = null, $productName = null) |
|
| 483 | { |
||
| 484 | 3 | $this->errors[] = $error; |
|
| 485 | $this->session->getFlashBag()->add('eccube.front.request.error', $error); |
||
| 486 | if (!is_null($productName)) { |
||
| 487 | $this->session->getFlashBag()->add('eccube.front.request.product', $productName); |
||
| 488 | } |
||
| 489 | 3 | return $this; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param string $productClassId |
||
| 494 | * @return \Eccube\Service\CartService |
||
| 495 | */ |
||
| 496 | 1 | public function upProductQuantity($productClassId) |
|
| 497 | { |
||
| 498 | $quantity = $this->getProductQuantity($productClassId) + 1; |
||
| 499 | $this->setProductQuantity($productClassId, $quantity); |
||
| 500 | |||
| 501 | 1 | return $this; |
|
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $productClassId |
||
| 506 | * @return \Eccube\Service\CartService |
||
| 507 | */ |
||
| 508 | 1 | public function downProductQuantity($productClassId) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | 4 | public function getProductTypes() |
|
| 525 | { |
||
| 526 | |||
| 527 | 4 | $productTypes = array(); |
|
| 528 | foreach ($this->getCart()->getCartItems() as $item) { |
||
| 529 | /* @var $ProductClass \Eccube\Entity\ProductClass */ |
||
| 530 | $ProductClass = $item->getObject(); |
||
| 531 | $productTypes[] = $ProductClass->getProductType(); |
||
| 532 | } |
||
| 533 | return array_unique($productTypes); |
||
| 534 | |||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @return string[] |
||
| 539 | */ |
||
| 540 | 1 | public function getErrors() |
|
| 541 | { |
||
| 542 | 1 | return $this->errors; |
|
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @return string[] |
||
| 547 | */ |
||
| 548 | public function getMessages() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param string $message |
||
| 555 | * @return \Eccube\Service\CartService |
||
| 556 | */ |
||
| 557 | public function setMessage($message) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | 1 | public function getError() |
|
| 568 | { |
||
| 569 | 1 | return $this->error; |
|
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param string $error |
||
| 574 | * @return \Eccube\Service\CartService |
||
| 575 | */ |
||
| 576 | 1 | public function setError($error = null) |
|
| 577 | { |
||
| 578 | 1 | $this->error = $error; |
|
| 579 | $this->session->getFlashBag()->set('eccube.front.request.error', $error); |
||
| 580 | 1 | return $this; |
|
| 581 | } |
||
| 582 | |||
| 583 | } |
||
| 584 |