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 |
||
41 | class ShoppingService |
||
|
|||
42 | { |
||
43 | /** @var \Eccube\Application */ |
||
44 | public $app; |
||
45 | |||
46 | /** @var \Eccube\Service\CartService */ |
||
47 | protected $cartService; |
||
48 | |||
49 | /** @var \Eccube\Service\OrderService */ |
||
50 | protected $orderService; |
||
51 | |||
52 | /** @var \Eccube\Entity\BaseInfo */ |
||
53 | protected $BaseInfo; |
||
54 | |||
55 | /** @var \Doctrine\ORM\EntityManager */ |
||
56 | protected $em; |
||
57 | |||
58 | 43 | public function __construct(Application $app, $cartService, $orderService) |
|
65 | |||
66 | /** |
||
67 | * セッションにセットされた受注情報を取得 |
||
68 | * |
||
69 | * @param null $status |
||
70 | * @return null|object |
||
71 | */ |
||
72 | 6 | public function getOrder($status = null) |
|
73 | { |
||
74 | |||
75 | // 受注データを取得 |
||
76 | $preOrderId = $this->cartService->getPreOrderId(); |
||
77 | if (!$preOrderId) { |
||
78 | return null; |
||
79 | } |
||
80 | 6 | ||
81 | $condition = array( |
||
82 | 'pre_order_id' => $preOrderId, |
||
83 | ); |
||
84 | |||
85 | 3 | if (!is_null($status)) { |
|
86 | $condition += array( |
||
87 | 'OrderStatus' => $status, |
||
88 | ); |
||
89 | } |
||
90 | 6 | ||
91 | $Order = $this->app['eccube.repository.order']->findOneBy($condition); |
||
92 | 3 | ||
93 | return $Order; |
||
94 | |||
95 | } |
||
96 | |||
97 | /** |
||
98 | * 非会員情報を取得 |
||
99 | * |
||
100 | 8 | * @param $sesisonKey |
|
101 | * @return $Customer|null |
||
102 | */ |
||
103 | public function getNonMember($sesisonKey) |
||
104 | { |
||
105 | |||
106 | 1 | // 非会員でも一度会員登録されていればショッピング画面へ遷移 |
|
107 | $nonMember = $this->app['session']->get($sesisonKey); |
||
108 | if (is_null($nonMember)) { |
||
109 | 8 | return null; |
|
110 | } |
||
111 | |||
112 | 8 | $Customer = $nonMember['customer']; |
|
113 | $Customer->setPref($this->app['eccube.repository.master.pref']->find($nonMember['pref'])); |
||
114 | |||
115 | return $Customer; |
||
116 | |||
117 | } |
||
118 | |||
119 | /** |
||
120 | * 受注情報を作成 |
||
121 | * |
||
122 | 16 | * @param $Customer |
|
123 | * @return \Eccube\Entity\Order |
||
124 | */ |
||
125 | public function createOrder($Customer) |
||
126 | { |
||
127 | // ランダムなpre_order_idを作成 |
||
128 | 16 | $preOrderId = sha1(Str::random(32)); |
|
129 | |||
130 | // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成 |
||
131 | $Order = $this->registerPreOrder( |
||
132 | $Customer, |
||
133 | $preOrderId); |
||
134 | |||
135 | 16 | $this->cartService->setPreOrderId($preOrderId); |
|
136 | $this->cartService->save(); |
||
137 | |||
138 | return $Order; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * 仮受注情報作成 |
||
143 | * |
||
144 | * @param $Customer |
||
145 | * @param $preOrderId |
||
146 | * @return mixed |
||
147 | 16 | * @throws \Doctrine\ORM\NoResultException |
|
148 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
149 | */ |
||
150 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
151 | { |
||
152 | |||
153 | $this->em = $this->app['orm.em']; |
||
154 | |||
155 | // 受注情報を作成 |
||
156 | $Order = $this->getNewOrder($Customer); |
||
157 | $Order->setPreOrderId($preOrderId); |
||
158 | |||
159 | $this->em->persist($Order); |
||
160 | |||
161 | // 配送業者情報を取得 |
||
162 | $deliveries = $this->getDeliveriesCart(); |
||
163 | |||
164 | // お届け先情報を作成 |
||
165 | $Order = $this->getNewShipping($Order, $Customer, $deliveries); |
||
166 | |||
167 | // 受注明細情報、配送商品情報を作成 |
||
168 | $Order = $this->getNewDetails($Order); |
||
169 | |||
170 | // 小計 |
||
171 | $subTotal = $this->orderService->getSubTotal($Order); |
||
172 | |||
173 | // 消費税のみの小計 |
||
174 | $tax = $this->orderService->getTotalTax($Order); |
||
175 | |||
176 | // 配送料合計金額 |
||
177 | $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings())); |
||
178 | |||
179 | // 小計 |
||
180 | $Order->setSubTotal($subTotal); |
||
181 | |||
182 | // 配送料無料条件(合計金額) |
||
183 | $this->setDeliveryFreeAmount($Order); |
||
184 | |||
185 | // 配送料無料条件(合計数量) |
||
186 | $this->setDeliveryFreeQuantity($Order); |
||
187 | |||
188 | // 初期選択の支払い方法をセット |
||
189 | $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries); |
||
190 | 16 | $payments = $this->getPayments($payments, $subTotal); |
|
191 | |||
192 | if (count($payments) > 0) { |
||
193 | $payment = $payments[0]; |
||
194 | $Order->setPayment($payment); |
||
195 | $Order->setPaymentMethod($payment->getMethod()); |
||
196 | 16 | $Order->setCharge($payment->getCharge()); |
|
197 | } else { |
||
198 | $Order->setCharge(0); |
||
199 | } |
||
200 | |||
201 | $Order->setTax($tax); |
||
202 | |||
203 | $total = $subTotal + $Order->getCharge() + $Order->getDeliveryFeeTotal(); |
||
204 | |||
205 | $Order->setTotal($total); |
||
206 | $Order->setPaymentTotal($total); |
||
207 | 16 | ||
208 | $this->em->flush(); |
||
209 | 16 | ||
210 | return $Order; |
||
211 | |||
212 | } |
||
213 | |||
214 | /** |
||
215 | * 受注情報を作成 |
||
216 | 16 | * @param $Customer |
|
217 | * @return \Eccube\Entity\Order |
||
218 | */ |
||
219 | public function getNewOrder(Customer $Customer) |
||
220 | { |
||
221 | 16 | $Order = $this->newOrder(); |
|
222 | 16 | $this->copyToOrderFromCustomer($Order, $Customer); |
|
223 | |||
224 | return $Order; |
||
225 | } |
||
226 | |||
227 | |||
228 | /** |
||
229 | 16 | * 受注情報を作成 |
|
230 | * @return \Eccube\Entity\Order |
||
231 | */ |
||
232 | public function newOrder() |
||
233 | 16 | { |
|
234 | $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_processing']); |
||
235 | $Order = new \Eccube\Entity\Order($OrderStatus); |
||
236 | return $Order; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * 受注情報を作成 |
||
241 | * |
||
242 | * @param \Eccube\Entity\Order $Order |
||
243 | 16 | * @param \Eccube\Entity\Customer|null $Customer |
|
244 | * @return \Eccube\Entity\Order |
||
245 | */ |
||
246 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
247 | { |
||
248 | if (is_null($Customer)) { |
||
249 | return $Order; |
||
250 | } |
||
251 | |||
252 | if ($Customer->getId()) { |
||
253 | $Order->setCustomer($Customer); |
||
254 | } |
||
255 | $Order |
||
256 | ->setName01($Customer->getName01()) |
||
257 | ->setName02($Customer->getName02()) |
||
258 | ->setKana01($Customer->getKana01()) |
||
259 | ->setKana02($Customer->getKana02()) |
||
260 | ->setCompanyName($Customer->getCompanyName()) |
||
261 | ->setEmail($Customer->getEmail()) |
||
262 | ->setTel01($Customer->getTel01()) |
||
263 | ->setTel02($Customer->getTel02()) |
||
264 | ->setTel03($Customer->getTel03()) |
||
265 | ->setFax01($Customer->getFax01()) |
||
266 | ->setFax02($Customer->getFax02()) |
||
267 | ->setFax03($Customer->getFax03()) |
||
268 | ->setZip01($Customer->getZip01()) |
||
269 | ->setZip02($Customer->getZip02()) |
||
270 | ->setZipCode($Customer->getZip01() . $Customer->getZip02()) |
||
271 | ->setPref($Customer->getPref()) |
||
272 | ->setAddr01($Customer->getAddr01()) |
||
273 | ->setAddr02($Customer->getAddr02()) |
||
274 | ->setSex($Customer->getSex()) |
||
275 | 16 | ->setBirth($Customer->getBirth()) |
|
276 | 16 | ->setJob($Customer->getJob()); |
|
277 | |||
278 | return $Order; |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * 配送業者情報を取得 |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function getDeliveriesCart() |
||
288 | { |
||
289 | |||
290 | // カートに保持されている商品種別を取得 |
||
291 | $productTypes = $this->cartService->getProductTypes(); |
||
292 | |||
293 | return $this->getDeliveries($productTypes); |
||
294 | |||
295 | } |
||
296 | |||
297 | /** |
||
298 | * 配送業者情報を取得 |
||
299 | * |
||
300 | * @param Order $Order |
||
301 | * @return array |
||
302 | */ |
||
303 | public function getDeliveriesOrder(Order $Order) |
||
312 | |||
313 | /** |
||
314 | * 配送業者情報を取得 |
||
315 | * |
||
316 | 10 | * @param $productTypes |
|
317 | * @return array |
||
318 | */ |
||
319 | public function getDeliveries($productTypes) |
||
320 | { |
||
341 | |||
342 | |||
343 | /** |
||
344 | * お届け先情報を作成 |
||
345 | * |
||
346 | * @param Order $Order |
||
347 | * @param Customer $Customer |
||
348 | 16 | * @param $deliveries |
|
349 | * @return Order |
||
350 | 16 | */ |
|
351 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
375 | |||
376 | /** |
||
377 | * お届け先情報を作成 |
||
378 | * |
||
379 | * @param \Eccube\Entity\Shipping $Shipping |
||
380 | 17 | * @param \Eccube\Entity\Customer|null $Customer |
|
381 | * @return \Eccube\Entity\Shipping |
||
382 | */ |
||
383 | 1 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
|
436 | |||
437 | |||
438 | /** |
||
439 | * 受注明細情報、配送商品情報を作成 |
||
440 | * |
||
441 | 16 | * @param Order $Order |
|
442 | * @return Order |
||
443 | */ |
||
444 | public function getNewDetails(Order $Order) |
||
468 | |||
469 | /** |
||
470 | * 受注明細情報を作成 |
||
471 | * |
||
472 | * @param Product $Product |
||
473 | * @param ProductClass $ProductClass |
||
474 | 16 | * @param $quantity |
|
475 | * @return \Eccube\Entity\OrderDetail |
||
476 | */ |
||
477 | public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity) |
||
505 | |||
506 | /** |
||
507 | * 配送商品情報を作成 |
||
508 | * |
||
509 | * @param Order $Order |
||
510 | * @param Product $Product |
||
511 | * @param ProductClass $ProductClass |
||
512 | 16 | * @param $quantity |
|
513 | * @return \Eccube\Entity\ShipmentItem |
||
514 | */ |
||
515 | public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
569 | |||
570 | /** |
||
571 | * お届け先ごとの送料合計を取得 |
||
572 | * |
||
573 | 16 | * @param $shippings |
|
574 | * @return int |
||
575 | 16 | */ |
|
576 | public function getShippingDeliveryFeeTotal($shippings) |
||
586 | |||
587 | /** |
||
588 | * 商品ごとの配送料を取得 |
||
589 | * |
||
590 | 14 | * @param Shipping $Shipping |
|
591 | * @return int |
||
592 | 14 | */ |
|
593 | public function getProductDeliveryFee(Shipping $Shipping) |
||
602 | |||
603 | /** |
||
604 | * 住所などの情報が変更された時に金額の再計算を行う |
||
605 | * |
||
606 | 1 | * @param Order $Order |
|
607 | * @return Order |
||
608 | */ |
||
609 | public function getAmount(Order $Order) |
||
633 | |||
634 | /** |
||
635 | * 配送料金の設定 |
||
636 | * |
||
637 | 15 | * @param Shipping $Shipping |
|
638 | * @param Delivery|null $Delivery |
||
639 | */ |
||
640 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
660 | |||
661 | /** |
||
662 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
663 | * |
||
664 | * @param Order $Order |
||
665 | */ |
||
666 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
|
682 | |||
683 | /** |
||
684 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
685 | * |
||
686 | * @param Order $Order |
||
687 | */ |
||
688 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
|
704 | |||
705 | |||
706 | /** |
||
707 | * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする |
||
708 | * |
||
709 | * @param $em トランザクション制御されているEntityManager |
||
710 | 8 | * @param Order $Order 受注情報 |
|
711 | * @return bool true : 成功、false : 失敗 |
||
712 | */ |
||
713 | public function isOrderProduct($em, \Eccube\Entity\Order $Order) |
||
752 | |||
753 | /** |
||
754 | * 受注情報、お届け先情報の更新 |
||
755 | * |
||
756 | 5 | * @param Order $Order 受注情報 |
|
757 | * @param $data フォームデータ |
||
758 | */ |
||
759 | public function setOrderUpdate(Order $Order, $data) |
||
801 | |||
802 | |||
803 | /** |
||
804 | * 在庫情報の更新 |
||
805 | * |
||
806 | 1 | * @param $em トランザクション制御されているEntityManager |
|
807 | * @param Order $Order 受注情報 |
||
808 | */ |
||
809 | public function setStockUpdate($em, Order $Order) |
||
834 | |||
835 | |||
836 | /** |
||
837 | * 会員情報の更新 |
||
838 | * |
||
839 | 3 | * @param Order $Order 受注情報 |
|
840 | * @param Customer $user ログインユーザ |
||
841 | */ |
||
842 | public function setCustomerUpdate(Order $Order, Customer $user) |
||
859 | |||
860 | |||
861 | /** |
||
862 | * 支払方法選択の表示設定 |
||
863 | * |
||
864 | * @param $payments 支払選択肢情報 |
||
865 | 10 | * @param $subTotal 小計 |
|
866 | * @return array |
||
867 | 10 | */ |
|
868 | public function getPayments($payments, $subTotal) |
||
886 | |||
887 | /** |
||
888 | * お届け日を取得 |
||
889 | * |
||
890 | 3 | * @param Order $Order |
|
891 | * @return array |
||
892 | */ |
||
893 | public function getFormDeliveryDates(Order $Order) |
||
931 | |||
932 | /** |
||
933 | * 支払方法を取得 |
||
934 | * |
||
935 | * @param $deliveries |
||
936 | 4 | * @param Order $Order |
|
937 | * @return array |
||
938 | */ |
||
939 | public function getFormPayments($deliveries, Order $Order) |
||
961 | |||
962 | /** |
||
963 | * お届け先ごとにFormを作成 |
||
964 | * |
||
965 | 2 | * @param Order $Order |
|
966 | * @return \Symfony\Component\Form\Form |
||
967 | * @deprecated since 3.0, to be removed in 3.1 |
||
968 | */ |
||
969 | View Code Duplication | public function getShippingForm(Order $Order) |
|
995 | |||
996 | /** |
||
997 | * お届け先ごとにFormBuilderを作成 |
||
998 | * |
||
999 | * @param Order $Order |
||
1000 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
1001 | */ |
||
1002 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
1026 | |||
1027 | } |
||
1028 |