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 |
||
| 66 | class ShoppingService |
||
| 67 | { |
||
| 68 | /** |
||
| 69 | * @Inject(MailTemplateRepository::class) |
||
| 70 | * @var MailTemplateRepository |
||
| 71 | */ |
||
| 72 | protected $mailTemplateRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @Inject(MailService::class) |
||
| 76 | * @var MailService |
||
| 77 | */ |
||
| 78 | protected $mailService; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @Inject("eccube.event.dispatcher") |
||
| 82 | * @var EventDispatcher |
||
| 83 | */ |
||
| 84 | protected $eventDispatcher; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @Inject("form.factory") |
||
| 88 | * @var FormFactory |
||
| 89 | */ |
||
| 90 | protected $formFactory; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @Inject(DeliveryFeeRepository::class) |
||
| 94 | * @var DeliveryFeeRepository |
||
| 95 | */ |
||
| 96 | protected $deliveryFeeRepository; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @Inject(TaxRuleRepository::class) |
||
| 100 | * @var TaxRuleRepository |
||
| 101 | */ |
||
| 102 | protected $taxRuleRepository; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @Inject(CustomerAddressRepository::class) |
||
| 106 | * @var CustomerAddressRepository |
||
| 107 | */ |
||
| 108 | protected $customerAddressRepository; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @Inject(DeliveryRepository::class) |
||
| 112 | * @var DeliveryRepository |
||
| 113 | */ |
||
| 114 | protected $deliveryRepository; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @Inject(OrderStatusRepository::class) |
||
| 118 | * @var OrderStatusRepository |
||
| 119 | */ |
||
| 120 | protected $orderStatusRepository; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @Inject(PaymentRepository::class) |
||
| 124 | * @var PaymentRepository |
||
| 125 | */ |
||
| 126 | protected $paymentRepository; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @Inject(DeviceTypeRepository::class) |
||
| 130 | * @var DeviceTypeRepository |
||
| 131 | */ |
||
| 132 | protected $deviceTypeRepository; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @Inject("orm.em") |
||
| 136 | * @var EntityManager |
||
| 137 | */ |
||
| 138 | protected $entityManager; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @Inject("config") |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | protected $appConfig; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @Inject(PrefRepository::class) |
||
| 148 | * @var PrefRepository |
||
| 149 | */ |
||
| 150 | protected $prefRepository; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @Inject("session") |
||
| 154 | * @var Session |
||
| 155 | */ |
||
| 156 | protected $session; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @Inject(OrderRepository::class) |
||
| 160 | * @var OrderRepository |
||
| 161 | */ |
||
| 162 | protected $orderRepository; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @Inject(BaseInfoRepository::class) |
||
| 166 | * @var BaseInfoRepository |
||
| 167 | */ |
||
| 168 | protected $baseInfoRepository; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @Inject(Application::class) |
||
| 172 | * @var \Eccube\Application |
||
| 173 | */ |
||
| 174 | public $app; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @Inject(CartService::class) |
||
| 178 | * @var \Eccube\Service\CartService |
||
| 179 | */ |
||
| 180 | protected $cartService; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var \Eccube\Service\OrderService |
||
| 184 | * |
||
| 185 | * @deprecated |
||
| 186 | */ |
||
| 187 | protected $orderService; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * セッションにセットされた受注情報を取得 |
||
| 191 | * |
||
| 192 | * @param null $status |
||
| 193 | * @return null|object |
||
| 194 | */ |
||
| 195 | 11 | public function getOrder($status = null) |
|
| 219 | |||
| 220 | /** |
||
|
|
|||
| 221 | * 非会員情報を取得 |
||
| 222 | * |
||
| 223 | * @param $sesisonKey |
||
| 224 | * @return $Customer|null |
||
| 225 | */ |
||
| 226 | 4 | public function getNonMember($sesisonKey) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * 受注情報を作成 |
||
| 254 | * |
||
| 255 | * @param $Customer |
||
| 256 | * @return \Eccube\Entity\Order |
||
| 257 | */ |
||
| 258 | public function createOrder($Customer) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * 仮受注情報作成 |
||
| 282 | * |
||
| 283 | * @param $Customer |
||
| 284 | * @param $preOrderId |
||
| 285 | * @return mixed |
||
| 286 | * @throws \Doctrine\ORM\NoResultException |
||
| 287 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 288 | */ |
||
| 289 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * 受注情報を作成 |
||
| 357 | * |
||
| 358 | * @param $Customer |
||
| 359 | * @return \Eccube\Entity\Order |
||
| 360 | */ |
||
| 361 | public function getNewOrder(Customer $Customer) |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * 受注情報を作成 |
||
| 372 | * |
||
| 373 | * @return \Eccube\Entity\Order |
||
| 374 | */ |
||
| 375 | public function newOrder() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * 受注情報を作成 |
||
| 385 | * |
||
| 386 | * @param \Eccube\Entity\Order $Order |
||
| 387 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 388 | * @return \Eccube\Entity\Order |
||
| 389 | */ |
||
| 390 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * 配送業者情報を取得 |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | public function getDeliveriesCart() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * 配送業者情報を取得 |
||
| 443 | * |
||
| 444 | * @param Order $Order |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getDeliveriesOrder(Order $Order) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * 配送業者情報を取得 |
||
| 459 | * |
||
| 460 | * @param $productTypes |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | 3 | public function getDeliveries($productTypes) |
|
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * お届け先情報を作成 |
||
| 490 | * |
||
| 491 | * @param Order $Order |
||
| 492 | * @param Customer $Customer |
||
| 493 | * @param $deliveries |
||
| 494 | * @return Order |
||
| 495 | */ |
||
| 496 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * お届け先情報を作成 |
||
| 523 | * |
||
| 524 | * @param \Eccube\Entity\Shipping $Shipping |
||
| 525 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 526 | * @return \Eccube\Entity\Shipping |
||
| 527 | */ |
||
| 528 | 1 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
|
| 581 | |||
| 582 | |||
| 583 | /** |
||
| 584 | * 受注明細情報、配送商品情報を作成 |
||
| 585 | * |
||
| 586 | * @param Order $Order |
||
| 587 | * @return Order |
||
| 588 | */ |
||
| 589 | public function getNewDetails(Order $Order) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * 受注明細情報を作成 |
||
| 616 | * |
||
| 617 | * @param Product $Product |
||
| 618 | * @param ProductClass $ProductClass |
||
| 619 | * @param $quantity |
||
| 620 | * @return \Eccube\Entity\OrderDetail |
||
| 621 | */ |
||
| 622 | public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * 配送商品情報を作成 |
||
| 653 | * |
||
| 654 | * @param Order $Order |
||
| 655 | * @param Product $Product |
||
| 656 | * @param ProductClass $ProductClass |
||
| 657 | * @param $quantity |
||
| 658 | * @return \Eccube\Entity\ShipmentItem |
||
| 659 | */ |
||
| 660 | public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * お届け先ごとの送料合計を取得 |
||
| 718 | * |
||
| 719 | * @param $shippings |
||
| 720 | * @return int |
||
| 721 | */ |
||
| 722 | public function getShippingDeliveryFeeTotal($shippings) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * 商品ごとの配送料を取得 |
||
| 735 | * |
||
| 736 | * @param Shipping $Shipping |
||
| 737 | * @return int |
||
| 738 | */ |
||
| 739 | public function getProductDeliveryFee(Shipping $Shipping) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * 住所などの情報が変更された時に金額の再計算を行う |
||
| 752 | * @deprecated PurchaseFlowで行う |
||
| 753 | * @param Order $Order |
||
| 754 | * @return Order |
||
| 755 | */ |
||
| 756 | 1 | public function getAmount(Order $Order) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * 配送料金の設定 |
||
| 781 | * |
||
| 782 | * @param Shipping $Shipping |
||
| 783 | * @param Delivery|null $Delivery |
||
| 784 | */ |
||
| 785 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
| 809 | * |
||
| 810 | * @param Order $Order |
||
| 811 | */ |
||
| 812 | 2 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
| 829 | |||
| 830 | /** |
||
| 831 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
| 832 | * |
||
| 833 | * @param Order $Order |
||
| 834 | */ |
||
| 835 | 1 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
| 852 | |||
| 853 | |||
| 854 | /** |
||
| 855 | * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする |
||
| 856 | * |
||
| 857 | * @param $em トランザクション制御されているEntityManager |
||
| 858 | * @param Order $Order 受注情報 |
||
| 859 | * @return bool true : 成功、false : 失敗 |
||
| 860 | */ |
||
| 861 | 4 | public function isOrderProduct($em, \Eccube\Entity\Order $Order) |
|
| 937 | |||
| 938 | /** |
||
| 939 | * 受注情報、お届け先情報の更新 |
||
| 940 | * |
||
| 941 | * @param Order $Order 受注情報 |
||
| 942 | * @param $data フォームデータ |
||
| 943 | * |
||
| 944 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
| 945 | */ |
||
| 946 | public function setOrderUpdate(Order $Order, $data) |
||
| 979 | |||
| 980 | |||
| 981 | /** |
||
| 982 | * 受注情報の更新 |
||
| 983 | * |
||
| 984 | * @param Order $Order 受注情報 |
||
| 985 | */ |
||
| 986 | 1 | public function setOrderUpdateData(Order $Order) |
|
| 994 | |||
| 995 | |||
| 996 | /** |
||
| 997 | * 在庫情報の更新 |
||
| 998 | * |
||
| 999 | * @param $em トランザクション制御されているEntityManager |
||
| 1000 | * @param Order $Order 受注情報 |
||
| 1001 | */ |
||
| 1002 | 1 | public function setStockUpdate($em, Order $Order) |
|
| 1031 | |||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * 会員情報の更新 |
||
| 1035 | * |
||
| 1036 | * @param Order $Order 受注情報 |
||
| 1037 | * @param Customer $user ログインユーザ |
||
| 1038 | */ |
||
| 1039 | 1 | public function setCustomerUpdate(Order $Order, Customer $user) |
|
| 1056 | |||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * 支払方法選択の表示設定 |
||
| 1060 | * |
||
| 1061 | * @param $payments 支払選択肢情報 |
||
| 1062 | * @param $subTotal 小計 |
||
| 1063 | * @return array |
||
| 1064 | */ |
||
| 1065 | 1 | public function getPayments($payments, $subTotal) |
|
| 1083 | |||
| 1084 | /** |
||
| 1085 | * お届け日を取得 |
||
| 1086 | * |
||
| 1087 | * @param Order $Order |
||
| 1088 | * @return array |
||
| 1089 | */ |
||
| 1090 | 2 | public function getFormDeliveryDates(Order $Order) |
|
| 1134 | |||
| 1135 | /** |
||
| 1136 | * 支払方法を取得 |
||
| 1137 | * |
||
| 1138 | * @param $deliveries |
||
| 1139 | * @param Order $Order |
||
| 1140 | * @return array |
||
| 1141 | */ |
||
| 1142 | public function getFormPayments($deliveries, Order $Order) |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * お届け先ごとにFormを作成 |
||
| 1167 | * |
||
| 1168 | * @param Order $Order |
||
| 1169 | * @return \Symfony\Component\Form\Form |
||
| 1170 | * @deprecated since 3.0, to be removed in 3.1 |
||
| 1171 | */ |
||
| 1172 | View Code Duplication | public function getShippingForm(Order $Order) |
|
| 1198 | |||
| 1199 | /** |
||
| 1200 | * お届け先ごとにFormBuilderを作成 |
||
| 1201 | * |
||
| 1202 | * @param Order $Order |
||
| 1203 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
| 1204 | * |
||
| 1205 | * @deprecated 利用している箇所なし |
||
| 1206 | */ |
||
| 1207 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
| 1231 | |||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * フォームデータを更新 |
||
| 1235 | * |
||
| 1236 | * @param Order $Order |
||
| 1237 | * @param array $data |
||
| 1238 | * |
||
| 1239 | * @deprecated |
||
| 1240 | */ |
||
| 1241 | 1 | public function setFormData(Order $Order, array $data) |
|
| 1259 | |||
| 1260 | /** |
||
| 1261 | * 配送料の合計金額を計算 |
||
| 1262 | * |
||
| 1263 | * @param Order $Order |
||
| 1264 | * @return Order |
||
| 1265 | */ |
||
| 1266 | public function calculateDeliveryFee(Order $Order) |
||
| 1267 | { |
||
| 1268 | |||
| 1269 | // 配送業者を取得 |
||
| 1270 | $shippings = $Order->getShippings(); |
||
| 1271 | |||
| 1272 | // 配送料合計金額 |
||
| 1273 | // TODO CalculateDeliveryFeeStrategy でセットする |
||
| 1274 | // $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings)); |
||
| 1275 | |||
| 1276 | // 配送料無料条件(合計金額) |
||
| 1277 | $this->setDeliveryFreeAmount($Order); |
||
| 1278 | |||
| 1279 | // 配送料無料条件(合計数量) |
||
| 1280 | $this->setDeliveryFreeQuantity($Order); |
||
| 1281 | |||
| 1282 | return $Order; |
||
| 1283 | |||
| 1284 | } |
||
| 1285 | |||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * 購入処理を行う |
||
| 1289 | * |
||
| 1290 | * @param Order $Order |
||
| 1291 | * @throws ShoppingException |
||
| 1292 | */ |
||
| 1293 | public function processPurchase(Order $Order) |
||
| 1294 | { |
||
| 1295 | |||
| 1296 | $em = $this->entityManager; |
||
| 1297 | |||
| 1298 | // TODO PurchaseFlowでやる |
||
| 1299 | // // 合計金額の再計算 |
||
| 1300 | // $this->calculatePrice($Order); |
||
| 1301 | // |
||
| 1302 | // // 商品公開ステータスチェック、商品制限数チェック、在庫チェック |
||
| 1303 | // $check = $this->isOrderProduct($em, $Order); |
||
| 1304 | // if (!$check) { |
||
| 1305 | // throw new ShoppingException('front.shopping.stock.error'); |
||
| 1306 | // } |
||
| 1307 | |||
| 1308 | // 受注情報、配送情報を更新 |
||
| 1309 | $Order = $this->calculateDeliveryFee($Order); |
||
| 1310 | $this->setOrderUpdateData($Order); |
||
| 1311 | // 在庫情報を更新 |
||
| 1312 | $this->setStockUpdate($em, $Order); |
||
| 1313 | |||
| 1314 | if ($this->app->isGranted('ROLE_USER')) { |
||
| 1315 | // 会員の場合、購入金額を更新 |
||
| 1316 | $this->setCustomerUpdate($Order, $this->app->user()); |
||
| 1317 | } |
||
| 1318 | |||
| 1319 | } |
||
| 1320 | |||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * 値引き可能かチェック |
||
| 1324 | * |
||
| 1325 | * @param Order $Order |
||
| 1326 | * @param $discount |
||
| 1327 | * @return bool |
||
| 1328 | */ |
||
| 1329 | public function isDiscount(Order $Order, $discount) |
||
| 1338 | |||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * 値引き金額をセット |
||
| 1342 | * |
||
| 1343 | * @param Order $Order |
||
| 1344 | * @param $discount |
||
| 1345 | */ |
||
| 1346 | public function setDiscount(Order $Order, $discount) |
||
| 1352 | |||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * 合計金額を計算 |
||
| 1356 | * |
||
| 1357 | * @param Order $Order |
||
| 1358 | * @return Order |
||
| 1359 | */ |
||
| 1360 | 1 | public function calculatePrice(Order $Order) |
|
| 1376 | |||
| 1377 | /** |
||
| 1378 | * 受注ステータスをセット |
||
| 1379 | * |
||
| 1380 | * @param Order $Order |
||
| 1381 | * @param $status |
||
| 1382 | * @return Order |
||
| 1383 | */ |
||
| 1384 | 1 | public function setOrderStatus(Order $Order, $status) |
|
| 1401 | |||
| 1402 | /** |
||
| 1403 | * 受注メール送信を行う |
||
| 1404 | * |
||
| 1405 | * @param Order $Order |
||
| 1406 | * @return MailHistory |
||
| 1407 | */ |
||
| 1408 | public function sendOrderMail(Order $Order) |
||
| 1409 | { |
||
| 1410 | |||
| 1411 | // メール送信 |
||
| 1412 | $message = $this->mailService->sendOrderMail($Order); |
||
| 1413 | |||
| 1414 | // 送信履歴を保存. |
||
| 1415 | $MailTemplate = $this->mailTemplateRepository->find(1); |
||
| 1416 | |||
| 1417 | $MailHistory = new MailHistory(); |
||
| 1418 | $MailHistory |
||
| 1419 | ->setSubject($message->getSubject()) |
||
| 1420 | ->setMailBody($message->getBody()) |
||
| 1421 | ->setMailTemplate($MailTemplate) |
||
| 1422 | ->setSendDate(new \DateTime()) |
||
| 1423 | ->setOrder($Order); |
||
| 1424 | |||
| 1425 | $this->entityManager->persist($MailHistory); |
||
| 1426 | $this->entityManager->flush($MailHistory); |
||
| 1427 | |||
| 1428 | return $MailHistory; |
||
| 1429 | |||
| 1430 | } |
||
| 1431 | |||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * 受注処理完了通知 |
||
| 1435 | * |
||
| 1436 | * @param Order $Order |
||
| 1437 | */ |
||
| 1438 | public function notifyComplete(Order $Order) |
||
| 1450 | |||
| 1451 | |||
| 1452 | } |
||
| 1453 |