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 |
||
| 55 | class ShoppingService |
||
|
|
|||
| 56 | { |
||
| 57 | /** |
||
| 58 | * @var MailTemplateRepository |
||
| 59 | */ |
||
| 60 | protected $mailTemplateRepository; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var MailService |
||
| 64 | */ |
||
| 65 | protected $mailService; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var EventDispatcher |
||
| 69 | */ |
||
| 70 | protected $eventDispatcher; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var FormFactory |
||
| 74 | */ |
||
| 75 | protected $formFactory; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var DeliveryFeeRepository |
||
| 79 | */ |
||
| 80 | protected $deliveryFeeRepository; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var TaxRuleRepository |
||
| 84 | */ |
||
| 85 | protected $taxRuleRepository; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var CustomerAddressRepository |
||
| 89 | */ |
||
| 90 | protected $customerAddressRepository; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var DeliveryRepository |
||
| 94 | */ |
||
| 95 | protected $deliveryRepository; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var DeliveryTimeRepository |
||
| 99 | */ |
||
| 100 | protected $deliveryTimeRepository; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var OrderStatusRepository |
||
| 104 | */ |
||
| 105 | protected $orderStatusRepository; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var PaymentRepository |
||
| 109 | */ |
||
| 110 | protected $paymentRepository; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var DeviceTypeRepository |
||
| 114 | */ |
||
| 115 | protected $deviceTypeRepository; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var EntityManager |
||
| 119 | */ |
||
| 120 | protected $entityManager; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var EccubeConfig |
||
| 124 | */ |
||
| 125 | protected $eccubeConfig; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var PrefRepository |
||
| 129 | */ |
||
| 130 | protected $prefRepository; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var Session |
||
| 134 | */ |
||
| 135 | protected $session; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var OrderRepository |
||
| 139 | */ |
||
| 140 | protected $orderRepository; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var BaseInfo |
||
| 144 | */ |
||
| 145 | protected $BaseInfo; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var \Eccube\Service\CartService |
||
| 149 | */ |
||
| 150 | protected $cartService; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var \Eccube\Service\OrderService |
||
| 154 | * |
||
| 155 | * @deprecated |
||
| 156 | */ |
||
| 157 | protected $orderService; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var AuthorizationCheckerInterface |
||
| 161 | */ |
||
| 162 | protected $authorizationChecker; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * ShoppingService constructor. |
||
| 166 | * |
||
| 167 | * @param MailTemplateRepository $mailTemplateRepository |
||
| 168 | * @param MailService $mailService |
||
| 169 | * @param EventDispatcher $eventDispatcher |
||
| 170 | * @param FormFactory $formFactory |
||
| 171 | * @param DeliveryFeeRepository $deliveryFeeRepository |
||
| 172 | * @param TaxRuleRepository $taxRuleRepository |
||
| 173 | * @param CustomerAddressRepository $customerAddressRepository |
||
| 174 | * @param DeliveryRepository $deliveryRepository |
||
| 175 | * @param DeliveryTimeRepository $deliveryTimeRepository |
||
| 176 | * @param OrderStatusRepository $orderStatusRepository |
||
| 177 | * @param PaymentRepository $paymentRepository |
||
| 178 | * @param DeviceTypeRepository $deviceTypeRepository |
||
| 179 | * @param EntityManager $entityManager |
||
| 180 | * @param EccubeConfig $eccubeConfig |
||
| 181 | * @param PrefRepository $prefRepository |
||
| 182 | * @param Session $session |
||
| 183 | * @param OrderRepository $orderRepository |
||
| 184 | * @param CartService $cartService |
||
| 185 | * @param OrderService $orderService |
||
| 186 | * @param BaseInfo $BaseInfo |
||
| 187 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 188 | */ |
||
| 189 | 59 | public function __construct( |
|
| 234 | |||
| 235 | /** |
||
| 236 | * セッションにセットされた受注情報を取得 |
||
| 237 | * |
||
| 238 | * @param null $status |
||
| 239 | * |
||
| 240 | * @return null|object |
||
| 241 | */ |
||
| 242 | 51 | public function getOrder($status = null) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * 非会員情報を取得 |
||
| 267 | * |
||
| 268 | * @param $sesisonKey |
||
| 269 | * |
||
| 270 | * @return $Customer|null |
||
| 271 | */ |
||
| 272 | public function getNonMember($sesisonKey) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * 受注情報を作成 |
||
| 284 | * |
||
| 285 | * @param $Customer |
||
| 286 | * |
||
| 287 | * @return \Eccube\Entity\Order |
||
| 288 | */ |
||
| 289 | public function createOrder($Customer) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * 仮受注情報作成 |
||
| 313 | * |
||
| 314 | * @param $Customer |
||
| 315 | * @param $preOrderId |
||
| 316 | * |
||
| 317 | * @return mixed |
||
| 318 | * |
||
| 319 | * @throws \Doctrine\ORM\NoResultException |
||
| 320 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 321 | */ |
||
| 322 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * 受注情報を作成 |
||
| 389 | * |
||
| 390 | * @param $Customer |
||
| 391 | * |
||
| 392 | * @return \Eccube\Entity\Order |
||
| 393 | */ |
||
| 394 | public function getNewOrder(Customer $Customer) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * 受注情報を作成 |
||
| 404 | * |
||
| 405 | * @return \Eccube\Entity\Order |
||
| 406 | */ |
||
| 407 | public function newOrder() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * 受注情報を作成 |
||
| 417 | * |
||
| 418 | * @param \Eccube\Entity\Order $Order |
||
| 419 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 420 | * |
||
| 421 | * @return \Eccube\Entity\Order |
||
| 422 | */ |
||
| 423 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * 配送業者情報を取得 |
||
| 460 | * |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getDeliveriesCart() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * 配送業者情報を取得 |
||
| 473 | * |
||
| 474 | * @param Order $Order |
||
| 475 | * |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | public function getDeliveriesOrder(Order $Order) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * 配送業者情報を取得 |
||
| 488 | * |
||
| 489 | * @param $saleTypes |
||
| 490 | * |
||
| 491 | * @return array |
||
| 492 | */ |
||
| 493 | public function getDeliveries($saleTypes) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * お届け先情報を作成 |
||
| 511 | * |
||
| 512 | * @param Order $Order |
||
| 513 | * @param Customer $Customer |
||
| 514 | * @param $deliveries |
||
| 515 | * |
||
| 516 | * @return Order |
||
| 517 | */ |
||
| 518 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * お届け先情報を作成 |
||
| 546 | * |
||
| 547 | * @param \Eccube\Entity\Shipping $Shipping |
||
| 548 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 549 | * |
||
| 550 | * @return \Eccube\Entity\Shipping |
||
| 551 | */ |
||
| 552 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * 受注明細情報、配送商品情報を作成 |
||
| 608 | * |
||
| 609 | * @param Order $Order |
||
| 610 | * |
||
| 611 | * @return Order |
||
| 612 | */ |
||
| 613 | public function getNewDetails(Order $Order) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * 配送商品情報を作成 |
||
| 634 | * |
||
| 635 | * @param Order $Order |
||
| 636 | * @param Product $Product |
||
| 637 | * @param ProductClass $ProductClass |
||
| 638 | * @param $quantity |
||
| 639 | * |
||
| 640 | * @return \Eccube\Entity\OrderItem |
||
| 641 | */ |
||
| 642 | public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * お届け先ごとの送料合計を取得 |
||
| 702 | * |
||
| 703 | * @param $shippings |
||
| 704 | * |
||
| 705 | * @return int |
||
| 706 | */ |
||
| 707 | public function getShippingDeliveryFeeTotal($shippings) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * 商品ごとの配送料を取得 |
||
| 719 | * |
||
| 720 | * @param Shipping $Shipping |
||
| 721 | * |
||
| 722 | * @return int |
||
| 723 | */ |
||
| 724 | public function getProductDeliveryFee(Shipping $Shipping) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * 住所などの情報が変更された時に金額の再計算を行う |
||
| 738 | * |
||
| 739 | * @deprecated PurchaseFlowで行う |
||
| 740 | * |
||
| 741 | * @param Order $Order |
||
| 742 | * |
||
| 743 | * @return Order |
||
| 744 | */ |
||
| 745 | public function getAmount(Order $Order) |
||
| 765 | |||
| 766 | /** |
||
| 767 | * 配送料金の設定 |
||
| 768 | * |
||
| 769 | * @param Shipping $Shipping |
||
| 770 | * @param Delivery|null $Delivery |
||
| 771 | */ |
||
| 772 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
| 796 | * |
||
| 797 | * @param Order $Order |
||
| 798 | */ |
||
| 799 | 6 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
| 815 | |||
| 816 | /** |
||
| 817 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
| 818 | * |
||
| 819 | * @param Order $Order |
||
| 820 | */ |
||
| 821 | 6 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
| 837 | |||
| 838 | /** |
||
| 839 | * 受注情報、お届け先情報の更新 |
||
| 840 | * |
||
| 841 | * @param Order $Order 受注情報 |
||
| 842 | * @param $data フォームデータ |
||
| 843 | * |
||
| 844 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
| 845 | */ |
||
| 846 | public function setOrderUpdate(Order $Order, $data) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * 受注情報の更新 |
||
| 882 | * |
||
| 883 | * @param Order $Order 受注情報 |
||
| 884 | */ |
||
| 885 | 6 | public function setOrderUpdateData(Order $Order) |
|
| 892 | |||
| 893 | /** |
||
| 894 | * 会員情報の更新 |
||
| 895 | * |
||
| 896 | * @param Order $Order 受注情報 |
||
| 897 | * @param Customer $user ログインユーザ |
||
| 898 | */ |
||
| 899 | 3 | public function setCustomerUpdate(Order $Order, Customer $user) |
|
| 912 | |||
| 913 | /** |
||
| 914 | * 支払方法選択の表示設定 |
||
| 915 | * |
||
| 916 | * @param $payments 支払選択肢情報 |
||
| 917 | * @param $subTotal 小計 |
||
| 918 | * |
||
| 919 | * @return array |
||
| 920 | */ |
||
| 921 | public function getPayments($payments, $subTotal) |
||
| 938 | |||
| 939 | /** |
||
| 940 | * お届け日を取得 |
||
| 941 | * |
||
| 942 | * @param Order $Order |
||
| 943 | * |
||
| 944 | * @return array |
||
| 945 | */ |
||
| 946 | public function getFormDeliveryDurations(Order $Order) |
||
| 992 | |||
| 993 | /** |
||
| 994 | * 支払方法を取得 |
||
| 995 | * |
||
| 996 | * @param $deliveries |
||
| 997 | * @param Order $Order |
||
| 998 | * |
||
| 999 | * @return array |
||
| 1000 | */ |
||
| 1001 | public function getFormPayments($deliveries, Order $Order) |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * お届け先ごとにFormを作成 |
||
| 1019 | * |
||
| 1020 | * @param Order $Order |
||
| 1021 | * |
||
| 1022 | * @return \Symfony\Component\Form\Form |
||
| 1023 | * |
||
| 1024 | * @deprecated since 3.0, to be removed in 3.1 |
||
| 1025 | */ |
||
| 1026 | View Code Duplication | public function getShippingForm(Order $Order) |
|
| 1051 | |||
| 1052 | /** |
||
| 1053 | * お届け先ごとにFormBuilderを作成 |
||
| 1054 | * |
||
| 1055 | * @param Order $Order |
||
| 1056 | * |
||
| 1057 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
| 1058 | * |
||
| 1059 | * @deprecated 利用している箇所なし |
||
| 1060 | */ |
||
| 1061 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
| 1084 | |||
| 1085 | /** |
||
| 1086 | * フォームデータを更新 |
||
| 1087 | * |
||
| 1088 | * @param Order $Order |
||
| 1089 | * @param array $data |
||
| 1090 | * |
||
| 1091 | * @deprecated |
||
| 1092 | */ |
||
| 1093 | public function setFormData(Order $Order, array $data) |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * 配送料の合計金額を計算 |
||
| 1115 | * |
||
| 1116 | * @param Order $Order |
||
| 1117 | * |
||
| 1118 | * @return Order |
||
| 1119 | */ |
||
| 1120 | 6 | public function calculateDeliveryFee(Order $Order) |
|
| 1137 | |||
| 1138 | /** |
||
| 1139 | * 購入処理を行う |
||
| 1140 | * |
||
| 1141 | * @param Order $Order |
||
| 1142 | * |
||
| 1143 | * @deprecated PurchaseFlow::purchase() を使用してください |
||
| 1144 | */ |
||
| 1145 | 6 | public function processPurchase(Order $Order) |
|
| 1155 | |||
| 1156 | /** |
||
| 1157 | * 値引き可能かチェック |
||
| 1158 | * |
||
| 1159 | * @param Order $Order |
||
| 1160 | * @param $discount |
||
| 1161 | * |
||
| 1162 | * @return bool |
||
| 1163 | */ |
||
| 1164 | public function isDiscount(Order $Order, $discount) |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * 値引き金額をセット |
||
| 1175 | * |
||
| 1176 | * @param Order $Order |
||
| 1177 | * @param $discount |
||
| 1178 | */ |
||
| 1179 | public function setDiscount(Order $Order, $discount) |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * 合計金額を計算 |
||
| 1186 | * |
||
| 1187 | * @param Order $Order |
||
| 1188 | * |
||
| 1189 | * @return Order |
||
| 1190 | */ |
||
| 1191 | public function calculatePrice(Order $Order) |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * 受注ステータスをセット |
||
| 1208 | * |
||
| 1209 | * @param Order $Order |
||
| 1210 | * @param $status |
||
| 1211 | * |
||
| 1212 | * @return Order |
||
| 1213 | */ |
||
| 1214 | 6 | public function setOrderStatus(Order $Order, $status) |
|
| 1229 | |||
| 1230 | /** |
||
| 1231 | * 受注メール送信を行う |
||
| 1232 | * |
||
| 1233 | * @param Order $Order |
||
| 1234 | * |
||
| 1235 | * @return MailHistory |
||
| 1236 | */ |
||
| 1237 | 6 | public function sendOrderMail(Order $Order) |
|
| 1255 | |||
| 1256 | /** |
||
| 1257 | * 受注処理完了通知 |
||
| 1258 | * |
||
| 1259 | * @param Order $Order |
||
| 1260 | */ |
||
| 1261 | public function notifyComplete(Order $Order) |
||
| 1271 | } |
||
| 1272 |