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 ShoppingService 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 ShoppingService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class ShoppingService |
||
|
|
|||
| 57 | { |
||
| 58 | /** |
||
| 59 | * @var MailTemplateRepository |
||
| 60 | */ |
||
| 61 | protected $mailTemplateRepository; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var MailService |
||
| 65 | */ |
||
| 66 | protected $mailService; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var EventDispatcher |
||
| 70 | */ |
||
| 71 | protected $eventDispatcher; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var FormFactory |
||
| 75 | */ |
||
| 76 | protected $formFactory; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var DeliveryFeeRepository |
||
| 80 | */ |
||
| 81 | protected $deliveryFeeRepository; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var TaxRuleRepository |
||
| 85 | */ |
||
| 86 | protected $taxRuleRepository; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var CustomerAddressRepository |
||
| 90 | */ |
||
| 91 | protected $customerAddressRepository; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var DeliveryRepository |
||
| 95 | */ |
||
| 96 | protected $deliveryRepository; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var DeliveryTimeRepository |
||
| 100 | */ |
||
| 101 | protected $deliveryTimeRepository; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var OrderStatusRepository |
||
| 105 | */ |
||
| 106 | protected $orderStatusRepository; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var PaymentRepository |
||
| 110 | */ |
||
| 111 | protected $paymentRepository; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var DeviceTypeRepository |
||
| 115 | */ |
||
| 116 | protected $deviceTypeRepository; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var EntityManager |
||
| 120 | */ |
||
| 121 | protected $entityManager; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var EccubeConfig |
||
| 125 | */ |
||
| 126 | protected $eccubeConfig; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var PrefRepository |
||
| 130 | */ |
||
| 131 | protected $prefRepository; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var Session |
||
| 135 | */ |
||
| 136 | protected $session; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var OrderRepository |
||
| 140 | */ |
||
| 141 | protected $orderRepository; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var BaseInfo |
||
| 145 | */ |
||
| 146 | protected $BaseInfo; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var \Eccube\Service\CartService |
||
| 150 | */ |
||
| 151 | protected $cartService; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var \Eccube\Service\OrderService |
||
| 155 | * |
||
| 156 | * @deprecated |
||
| 157 | */ |
||
| 158 | protected $orderService; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var AuthorizationCheckerInterface |
||
| 162 | */ |
||
| 163 | protected $authorizationChecker; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * ShoppingService constructor. |
||
| 167 | * |
||
| 168 | * @param MailTemplateRepository $mailTemplateRepository |
||
| 169 | * @param MailService $mailService |
||
| 170 | * @param EventDispatcher $eventDispatcher |
||
| 171 | * @param FormFactory $formFactory |
||
| 172 | * @param DeliveryFeeRepository $deliveryFeeRepository |
||
| 173 | * @param TaxRuleRepository $taxRuleRepository |
||
| 174 | * @param CustomerAddressRepository $customerAddressRepository |
||
| 175 | * @param DeliveryRepository $deliveryRepository |
||
| 176 | * @param DeliveryTimeRepository $deliveryTimeRepository |
||
| 177 | * @param OrderStatusRepository $orderStatusRepository |
||
| 178 | * @param PaymentRepository $paymentRepository |
||
| 179 | * @param DeviceTypeRepository $deviceTypeRepository |
||
| 180 | * @param EntityManager $entityManager |
||
| 181 | * @param EccubeConfig $eccubeConfig |
||
| 182 | * @param PrefRepository $prefRepository |
||
| 183 | * @param Session $session |
||
| 184 | * @param OrderRepository $orderRepository |
||
| 185 | * @param CartService $cartService |
||
| 186 | * @param OrderService $orderService |
||
| 187 | * @param BaseInfo $BaseInfo |
||
| 188 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 189 | */ |
||
| 190 | 59 | public function __construct( |
|
| 235 | |||
| 236 | /** |
||
| 237 | * セッションにセットされた受注情報を取得 |
||
| 238 | * |
||
| 239 | * @param null $status |
||
| 240 | * |
||
| 241 | * @return null|object |
||
| 242 | */ |
||
| 243 | 51 | public function getOrder($status = null) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * 非会員情報を取得 |
||
| 268 | * |
||
| 269 | * @param $sesisonKey |
||
| 270 | * |
||
| 271 | * @return $Customer|null |
||
| 272 | */ |
||
| 273 | public function getNonMember($sesisonKey) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * 受注情報を作成 |
||
| 285 | * |
||
| 286 | * @param $Customer |
||
| 287 | * |
||
| 288 | * @return \Eccube\Entity\Order |
||
| 289 | */ |
||
| 290 | public function createOrder($Customer) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * 仮受注情報作成 |
||
| 314 | * |
||
| 315 | * @param $Customer |
||
| 316 | * @param $preOrderId |
||
| 317 | * |
||
| 318 | * @return mixed |
||
| 319 | * |
||
| 320 | * @throws \Doctrine\ORM\NoResultException |
||
| 321 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 322 | */ |
||
| 323 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * 受注情報を作成 |
||
| 390 | * |
||
| 391 | * @param $Customer |
||
| 392 | * |
||
| 393 | * @return \Eccube\Entity\Order |
||
| 394 | */ |
||
| 395 | public function getNewOrder(Customer $Customer) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * 受注情報を作成 |
||
| 405 | * |
||
| 406 | * @return \Eccube\Entity\Order |
||
| 407 | */ |
||
| 408 | public function newOrder() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * 受注情報を作成 |
||
| 418 | * |
||
| 419 | * @param \Eccube\Entity\Order $Order |
||
| 420 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 421 | * |
||
| 422 | * @return \Eccube\Entity\Order |
||
| 423 | */ |
||
| 424 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * 配送業者情報を取得 |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | public function getDeliveriesCart() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * 配送業者情報を取得 |
||
| 467 | * |
||
| 468 | * @param Order $Order |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | public function getDeliveriesOrder(Order $Order) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * 配送業者情報を取得 |
||
| 482 | * |
||
| 483 | * @param $saleTypes |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function getDeliveries($saleTypes) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * お届け先情報を作成 |
||
| 505 | * |
||
| 506 | * @param Order $Order |
||
| 507 | * @param Customer $Customer |
||
| 508 | * @param $deliveries |
||
| 509 | * |
||
| 510 | * @return Order |
||
| 511 | */ |
||
| 512 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * お届け先情報を作成 |
||
| 540 | * |
||
| 541 | * @param \Eccube\Entity\Shipping $Shipping |
||
| 542 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 543 | * |
||
| 544 | * @return \Eccube\Entity\Shipping |
||
| 545 | */ |
||
| 546 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * 受注明細情報、配送商品情報を作成 |
||
| 589 | * |
||
| 590 | * @param Order $Order |
||
| 591 | * |
||
| 592 | * @return Order |
||
| 593 | */ |
||
| 594 | public function getNewDetails(Order $Order) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * 配送商品情報を作成 |
||
| 615 | * |
||
| 616 | * @param Order $Order |
||
| 617 | * @param Product $Product |
||
| 618 | * @param ProductClass $ProductClass |
||
| 619 | * @param $quantity |
||
| 620 | * |
||
| 621 | * @return \Eccube\Entity\OrderItem |
||
| 622 | */ |
||
| 623 | public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * お届け先ごとの送料合計を取得 |
||
| 683 | * |
||
| 684 | * @param $shippings |
||
| 685 | * |
||
| 686 | * @return int |
||
| 687 | */ |
||
| 688 | public function getShippingDeliveryFeeTotal($shippings) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * 商品ごとの配送料を取得 |
||
| 700 | * |
||
| 701 | * @param Shipping $Shipping |
||
| 702 | * |
||
| 703 | * @return int |
||
| 704 | */ |
||
| 705 | public function getProductDeliveryFee(Shipping $Shipping) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * 住所などの情報が変更された時に金額の再計算を行う |
||
| 719 | * |
||
| 720 | * @deprecated PurchaseFlowで行う |
||
| 721 | * |
||
| 722 | * @param Order $Order |
||
| 723 | * |
||
| 724 | * @return Order |
||
| 725 | */ |
||
| 726 | public function getAmount(Order $Order) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * 配送料金の設定 |
||
| 749 | * |
||
| 750 | * @param Shipping $Shipping |
||
| 751 | * @param Delivery|null $Delivery |
||
| 752 | */ |
||
| 753 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
| 777 | * |
||
| 778 | * @param Order $Order |
||
| 779 | */ |
||
| 780 | 6 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
| 796 | |||
| 797 | /** |
||
| 798 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
| 799 | * |
||
| 800 | * @param Order $Order |
||
| 801 | */ |
||
| 802 | 6 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
| 818 | |||
| 819 | /** |
||
| 820 | * 受注情報、お届け先情報の更新 |
||
| 821 | * |
||
| 822 | * @param Order $Order 受注情報 |
||
| 823 | * @param $data フォームデータ |
||
| 824 | * |
||
| 825 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
| 826 | */ |
||
| 827 | public function setOrderUpdate(Order $Order, $data) |
||
| 860 | |||
| 861 | /** |
||
| 862 | * 受注情報の更新 |
||
| 863 | * |
||
| 864 | * @param Order $Order 受注情報 |
||
| 865 | */ |
||
| 866 | 6 | public function setOrderUpdateData(Order $Order) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * 会員情報の更新 |
||
| 876 | * |
||
| 877 | * @param Order $Order 受注情報 |
||
| 878 | * @param Customer $user ログインユーザ |
||
| 879 | */ |
||
| 880 | 3 | public function setCustomerUpdate(Order $Order, Customer $user) |
|
| 893 | |||
| 894 | /** |
||
| 895 | * 支払方法選択の表示設定 |
||
| 896 | * |
||
| 897 | * @param $payments 支払選択肢情報 |
||
| 898 | * @param $subTotal 小計 |
||
| 899 | * |
||
| 900 | * @return array |
||
| 901 | */ |
||
| 902 | public function getPayments($payments, $subTotal) |
||
| 919 | |||
| 920 | /** |
||
| 921 | * お届け日を取得 |
||
| 922 | * |
||
| 923 | * @param Order $Order |
||
| 924 | * |
||
| 925 | * @return array |
||
| 926 | */ |
||
| 927 | public function getFormDeliveryDurations(Order $Order) |
||
| 973 | |||
| 974 | /** |
||
| 975 | * 支払方法を取得 |
||
| 976 | * |
||
| 977 | * @param $deliveries |
||
| 978 | * @param Order $Order |
||
| 979 | * |
||
| 980 | * @return array |
||
| 981 | */ |
||
| 982 | public function getFormPayments($deliveries, Order $Order) |
||
| 997 | |||
| 998 | /** |
||
| 999 | * お届け先ごとにFormを作成 |
||
| 1000 | * |
||
| 1001 | * @param Order $Order |
||
| 1002 | * |
||
| 1003 | * @return \Symfony\Component\Form\Form |
||
| 1004 | * |
||
| 1005 | * @deprecated since 3.0, to be removed in 3.1 |
||
| 1006 | */ |
||
| 1007 | View Code Duplication | public function getShippingForm(Order $Order) |
|
| 1032 | |||
| 1033 | /** |
||
| 1034 | * お届け先ごとにFormBuilderを作成 |
||
| 1035 | * |
||
| 1036 | * @param Order $Order |
||
| 1037 | * |
||
| 1038 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
| 1039 | * |
||
| 1040 | * @deprecated 利用している箇所なし |
||
| 1041 | */ |
||
| 1042 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
| 1065 | |||
| 1066 | /** |
||
| 1067 | * フォームデータを更新 |
||
| 1068 | * |
||
| 1069 | * @param Order $Order |
||
| 1070 | * @param array $data |
||
| 1071 | * |
||
| 1072 | * @deprecated |
||
| 1073 | */ |
||
| 1074 | public function setFormData(Order $Order, array $data) |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * 配送料の合計金額を計算 |
||
| 1096 | * |
||
| 1097 | * @param Order $Order |
||
| 1098 | * |
||
| 1099 | * @return Order |
||
| 1100 | */ |
||
| 1101 | 6 | public function calculateDeliveryFee(Order $Order) |
|
| 1118 | |||
| 1119 | /** |
||
| 1120 | * 購入処理を行う |
||
| 1121 | * |
||
| 1122 | * @param Order $Order |
||
| 1123 | * |
||
| 1124 | * @deprecated PurchaseFlow::purchase() を使用してください |
||
| 1125 | */ |
||
| 1126 | 6 | public function processPurchase(Order $Order) |
|
| 1136 | |||
| 1137 | /** |
||
| 1138 | * 値引き可能かチェック |
||
| 1139 | * |
||
| 1140 | * @param Order $Order |
||
| 1141 | * @param $discount |
||
| 1142 | * |
||
| 1143 | * @return bool |
||
| 1144 | */ |
||
| 1145 | public function isDiscount(Order $Order, $discount) |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * 値引き金額をセット |
||
| 1156 | * |
||
| 1157 | * @param Order $Order |
||
| 1158 | * @param $discount |
||
| 1159 | */ |
||
| 1160 | public function setDiscount(Order $Order, $discount) |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * 合計金額を計算 |
||
| 1167 | * |
||
| 1168 | * @param Order $Order |
||
| 1169 | * |
||
| 1170 | * @return Order |
||
| 1171 | */ |
||
| 1172 | public function calculatePrice(Order $Order) |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * 受注ステータスをセット |
||
| 1189 | * |
||
| 1190 | * @param Order $Order |
||
| 1191 | * @param $status |
||
| 1192 | * |
||
| 1193 | * @return Order |
||
| 1194 | */ |
||
| 1195 | 6 | public function setOrderStatus(Order $Order, $status) |
|
| 1210 | |||
| 1211 | /** |
||
| 1212 | * 受注メール送信を行う |
||
| 1213 | * |
||
| 1214 | * @param Order $Order |
||
| 1215 | * |
||
| 1216 | * @return MailHistory |
||
| 1217 | */ |
||
| 1218 | 6 | public function sendOrderMail(Order $Order) |
|
| 1236 | |||
| 1237 | /** |
||
| 1238 | * 受注処理完了通知 |
||
| 1239 | * |
||
| 1240 | * @param Order $Order |
||
| 1241 | */ |
||
| 1242 | public function notifyComplete(Order $Order) |
||
| 1252 | } |
||
| 1253 |