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 ShoppingController 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 ShoppingController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class ShoppingController extends AbstractController |
||
|
|||
40 | { |
||
41 | |||
42 | /** |
||
43 | * @var string 非会員用セッションキー |
||
44 | */ |
||
45 | private $sessionKey = 'eccube.front.shopping.nonmember'; |
||
46 | |||
47 | /** |
||
48 | * @var string 非会員用セッションキー |
||
49 | */ |
||
50 | private $sessionCustomerAddressKey = 'eccube.front.shopping.nonmember.customeraddress'; |
||
51 | |||
52 | /** |
||
53 | * @var string 複数配送警告メッセージ |
||
54 | */ |
||
55 | private $sessionMultipleKey = 'eccube.front.shopping.multiple'; |
||
56 | |||
57 | /** |
||
58 | * @var string 受注IDキー |
||
59 | */ |
||
60 | private $sessionOrderKey = 'eccube.front.shopping.order.id'; |
||
61 | |||
62 | /** |
||
63 | * 購入画面表示 |
||
64 | * |
||
65 | * @param Application $app |
||
66 | * @param Request $request |
||
67 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
68 | */ |
||
69 | 5 | public function index(Application $app, Request $request) |
|
70 | { |
||
71 | $cartService = $app['eccube.service.cart']; |
||
72 | |||
73 | // カートチェック |
||
74 | if (!$cartService->isLocked()) { |
||
75 | // カートが存在しない、カートがロックされていない時はエラー |
||
76 | return $app->redirect($app->url('cart')); |
||
77 | } |
||
78 | |||
79 | // カートチェック |
||
80 | if (count($cartService->getCart()->getCartItems()) <= 0) { |
||
81 | // カートが存在しない時はエラー |
||
82 | return $app->redirect($app->url('cart')); |
||
83 | } |
||
84 | |||
85 | // 登録済みの受注情報を取得 |
||
86 | $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']); |
||
87 | |||
88 | // 初回アクセス(受注情報がない)の場合は, 受注情報を作成 |
||
89 | if (is_null($Order)) { |
||
90 | |||
91 | // 未ログインの場合, ログイン画面へリダイレクト. |
||
92 | if (!$app->isGranted('IS_AUTHENTICATED_FULLY')) { |
||
93 | |||
94 | // 非会員でも一度会員登録されていればショッピング画面へ遷移 |
||
95 | $Customer = $app['eccube.service.shopping']->getNonMember($this->sessionKey); |
||
96 | |||
97 | if (is_null($Customer)) { |
||
98 | return $app->redirect($app->url('shopping_login')); |
||
99 | } |
||
100 | |||
101 | } else { |
||
102 | $Customer = $app->user(); |
||
103 | 2 | } |
|
104 | |||
105 | // 受注情報を作成 |
||
106 | $Order = $app['eccube.service.shopping']->createOrder($Customer); |
||
107 | |||
108 | // セッション情報を削除 |
||
109 | $app['session']->remove($this->sessionOrderKey); |
||
110 | $app['session']->remove($this->sessionMultipleKey); |
||
111 | |||
112 | } else { |
||
113 | // 計算処理 |
||
114 | $Order = $app['eccube.service.shopping']->getAmount($Order); |
||
115 | 2 | } |
|
116 | |||
117 | // 受注関連情報を最新状態に更新 |
||
118 | $app['orm.em']->refresh($Order); |
||
119 | |||
120 | // form作成 |
||
121 | $form = $app['eccube.service.shopping']->getShippingForm($Order); |
||
122 | |||
123 | // 複数配送の場合、エラーメッセージを一度だけ表示 |
||
124 | if (!$app['session']->has($this->sessionMultipleKey)) { |
||
125 | if (count($Order->getShippings()) > 1) { |
||
126 | $app->addRequestError('shopping.multiple.delivery'); |
||
127 | } |
||
128 | $app['session']->set($this->sessionMultipleKey, 'multiple'); |
||
129 | } |
||
130 | |||
131 | |||
132 | 2 | return $app->render('Shopping/index.twig', array( |
|
133 | 2 | 'form' => $form->createView(), |
|
134 | 'Order' => $Order, |
||
135 | )); |
||
136 | 5 | } |
|
137 | |||
138 | /** |
||
139 | * 購入処理 |
||
140 | */ |
||
141 | 1 | public function confirm(Application $app, Request $request) |
|
142 | { |
||
143 | |||
144 | $cartService = $app['eccube.service.cart']; |
||
145 | |||
146 | // カートチェック |
||
147 | if (!$cartService->isLocked()) { |
||
148 | // カートが存在しない、カートがロックされていない時はエラー |
||
149 | return $app->redirect($app->url('cart')); |
||
150 | } |
||
151 | |||
152 | $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']); |
||
153 | 1 | if (!$Order) { |
|
154 | $app->addError('front.shopping.order.error'); |
||
155 | return $app->redirect($app->url('shopping_error')); |
||
156 | } |
||
157 | |||
158 | // form作成 |
||
159 | $form = $app['eccube.service.shopping']->getShippingForm($Order); |
||
160 | |||
161 | if ('POST' === $request->getMethod()) { |
||
162 | $form->handleRequest($request); |
||
163 | |||
164 | if ($form->isValid()) { |
||
165 | $data = $form->getData(); |
||
166 | |||
167 | // トランザクション制御 |
||
168 | $em = $app['orm.em']; |
||
169 | $em->getConnection()->beginTransaction(); |
||
170 | try { |
||
171 | // 商品公開ステータスチェック、商品制限数チェック、在庫チェック |
||
172 | $check = $app['eccube.service.shopping']->isOrderProduct($em, $Order); |
||
173 | 1 | if (!$check) { |
|
174 | $em->getConnection()->rollback(); |
||
175 | $em->close(); |
||
176 | |||
177 | $app->addError('front.shopping.stock.error'); |
||
178 | return $app->redirect($app->url('shopping_error')); |
||
179 | } |
||
180 | |||
181 | // 受注情報、配送情報を更新 |
||
182 | $app['eccube.service.shopping']->setOrderUpdate($Order, $data); |
||
183 | // 在庫情報を更新 |
||
184 | $app['eccube.service.shopping']->setStockUpdate($em, $Order); |
||
185 | |||
186 | if ($app->isGranted('ROLE_USER')) { |
||
187 | // 会員の場合、購入金額を更新 |
||
188 | $app['eccube.service.shopping']->setCustomerUpdate($Order, $app->user()); |
||
189 | } |
||
190 | |||
191 | $em->getConnection()->commit(); |
||
192 | $em->flush(); |
||
193 | |||
194 | } catch (\Exception $e) { |
||
195 | $em->getConnection()->rollback(); |
||
196 | $em->close(); |
||
197 | |||
198 | $app->log($e); |
||
199 | |||
200 | $app->addError('front.shopping.system.error'); |
||
201 | return $app->redirect($app->url('shopping_error')); |
||
202 | 1 | } |
|
203 | |||
204 | // カート削除 |
||
205 | $app['eccube.service.cart']->clear()->save(); |
||
206 | |||
207 | // メール送信 |
||
208 | $app['eccube.service.mail']->sendOrderMail($Order); |
||
209 | |||
210 | // 受注IDをセッションにセット |
||
211 | $app['session']->set($this->sessionOrderKey, $Order->getId()); |
||
212 | |||
213 | // 送信履歴を保存. |
||
214 | $MailTemplate = $app['eccube.repository.mail_template']->find(1); |
||
215 | |||
216 | 1 | $body = $app->renderView($MailTemplate->getFileName(), array( |
|
217 | 1 | 'header' => $MailTemplate->getHeader(), |
|
218 | 1 | 'footer' => $MailTemplate->getFooter(), |
|
219 | 'Order' => $Order, |
||
220 | )); |
||
221 | |||
222 | $MailHistory = new MailHistory(); |
||
223 | $MailHistory |
||
224 | ->setSubject('[' . $app['eccube.repository.base_info']->get()->getShopName() . '] ' . $MailTemplate->getSubject()) |
||
225 | 1 | ->setMailBody($body) |
|
226 | 1 | ->setMailTemplate($MailTemplate) |
|
227 | ->setSendDate(new \DateTime()) |
||
228 | ->setOrder($Order); |
||
229 | $app['orm.em']->persist($MailHistory); |
||
230 | $app['orm.em']->flush($MailHistory); |
||
231 | |||
232 | $em->close(); |
||
233 | |||
234 | // 完了画面表示 |
||
235 | return $app->redirect($app->url('shopping_complete')); |
||
236 | |||
237 | } else { |
||
238 | return $app->render('Shopping/index.twig', array( |
||
239 | 'form' => $form->createView(), |
||
240 | 'Order' => $Order, |
||
241 | )); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return $app->redirect($app->url('cart')); |
||
246 | |||
247 | 1 | } |
|
248 | |||
249 | |||
250 | /** |
||
251 | * 購入完了画面表示 |
||
252 | */ |
||
253 | public function complete(Application $app) |
||
266 | |||
267 | |||
268 | /** |
||
269 | * 配送業者選択処理 |
||
270 | */ |
||
271 | public function delivery(Application $app, Request $request) |
||
350 | |||
351 | /** |
||
352 | * 支払い方法選択処理 |
||
353 | */ |
||
354 | public function payment(Application $app, Request $request) |
||
398 | |||
399 | /** |
||
400 | * お届け先変更がクリックされた場合の処理 |
||
401 | */ |
||
402 | View Code Duplication | public function shippingChange(Application $app, Request $request, $id) |
|
433 | |||
434 | /** |
||
435 | * お届け先の設定一覧からの選択 |
||
436 | */ |
||
437 | public function shipping(Application $app, Request $request, $id) |
||
438 | { |
||
439 | |||
440 | // カートチェック |
||
441 | if (!$app['eccube.service.cart']->isLocked()) { |
||
442 | // カートが存在しない、カートがロックされていない時はエラー |
||
443 | return $app->redirect($app->url('cart')); |
||
444 | } |
||
445 | |||
446 | if ('POST' === $request->getMethod()) { |
||
447 | $address = $request->get('address'); |
||
448 | |||
449 | if (is_null($address)) { |
||
450 | // 選択されていなければエラー |
||
451 | return $app->render( |
||
452 | 'Shopping/shipping.twig', |
||
453 | array( |
||
454 | 'Customer' => $app->user(), |
||
455 | 'shippingId' => $id, |
||
456 | ) |
||
457 | ); |
||
458 | } |
||
459 | |||
460 | // 選択されたお届け先情報を取得 |
||
461 | $CustomerAddress = $app['eccube.repository.customer_address']->findOneBy(array( |
||
462 | 'Customer' => $app->user(), |
||
463 | 'id' => $address, |
||
464 | )); |
||
465 | if (is_null($CustomerAddress)) { |
||
466 | throw new NotFoundHttpException(); |
||
467 | } |
||
468 | |||
469 | $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']); |
||
470 | if (!$Order) { |
||
471 | $app->addError('front.shopping.order.error'); |
||
472 | |||
473 | return $app->redirect($app->url('shopping_error')); |
||
474 | } |
||
475 | |||
476 | $Shipping = $Order->findShipping($id); |
||
477 | if (!$Shipping) { |
||
478 | throw new NotFoundHttpException(); |
||
479 | } |
||
480 | |||
481 | // お届け先情報を更新 |
||
482 | $Shipping |
||
483 | ->setName01($CustomerAddress->getName01()) |
||
484 | ->setName02($CustomerAddress->getName02()) |
||
485 | ->setKana01($CustomerAddress->getKana01()) |
||
486 | ->setKana02($CustomerAddress->getKana02()) |
||
487 | ->setCompanyName($CustomerAddress->getCompanyName()) |
||
488 | ->setTel01($CustomerAddress->getTel01()) |
||
489 | ->setTel02($CustomerAddress->getTel02()) |
||
490 | ->setTel03($CustomerAddress->getTel03()) |
||
491 | ->setFax01($CustomerAddress->getFax01()) |
||
492 | ->setFax02($CustomerAddress->getFax02()) |
||
493 | ->setFax03($CustomerAddress->getFax03()) |
||
494 | ->setZip01($CustomerAddress->getZip01()) |
||
495 | ->setZip02($CustomerAddress->getZip02()) |
||
496 | ->setZipCode($CustomerAddress->getZip01() . $CustomerAddress->getZip02()) |
||
497 | ->setPref($CustomerAddress->getPref()) |
||
498 | ->setAddr01($CustomerAddress->getAddr01()) |
||
499 | ->setAddr02($CustomerAddress->getAddr02()); |
||
500 | |||
501 | // 配送料金の設定 |
||
502 | $app['eccube.service.shopping']->setShippingDeliveryFee($Shipping); |
||
503 | |||
504 | // 配送先を更新 |
||
505 | $app['orm.em']->flush(); |
||
506 | |||
507 | return $app->redirect($app->url('shopping')); |
||
508 | |||
509 | } |
||
510 | |||
511 | return $app->render( |
||
512 | 'Shopping/shipping.twig', |
||
513 | array( |
||
514 | 'Customer' => $app->user(), |
||
515 | 'shippingId' => $id, |
||
516 | ) |
||
517 | ); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * お届け先の設定(非会員)がクリックされた場合の処理 |
||
522 | */ |
||
523 | View Code Duplication | public function shippingEditChange(Application $app, Request $request, $id) |
|
554 | |||
555 | |||
556 | /** |
||
557 | * お届け先の設定(非会員でも使用する) |
||
558 | */ |
||
559 | public function shippingEdit(Application $app, Request $request, $id) |
||
665 | |||
666 | /** |
||
667 | * お客様情報の変更(非会員) |
||
668 | */ |
||
669 | public function customer(Application $app, Request $request) |
||
737 | |||
738 | |||
739 | /** |
||
740 | * ログイン |
||
741 | */ |
||
742 | 2 | public function login(Application $app, Request $request) |
|
770 | |||
771 | /** |
||
772 | * 非会員処理 |
||
773 | */ |
||
774 | 6 | public function nonmember(Application $app, Request $request) |
|
775 | { |
||
776 | |||
777 | $cartService = $app['eccube.service.cart']; |
||
778 | |||
779 | // カートチェック |
||
780 | if (!$cartService->isLocked()) { |
||
781 | // カートが存在しない、カートがロックされていない時はエラー |
||
782 | return $app->redirect($app->url('cart')); |
||
783 | } |
||
784 | |||
785 | // ログイン済みの場合は, 購入画面へリダイレクト. |
||
786 | if ($app->isGranted('ROLE_USER')) { |
||
787 | return $app->redirect($app->url('shopping')); |
||
788 | } |
||
789 | |||
790 | // カートチェック |
||
791 | if (count($cartService->getCart()->getCartItems()) <= 0) { |
||
792 | // カートが存在しない時はエラー |
||
793 | return $app->redirect($app->url('cart')); |
||
794 | } |
||
795 | |||
796 | $form = $app['form.factory']->createBuilder('nonmember')->getForm(); |
||
797 | |||
798 | if ('POST' === $request->getMethod()) { |
||
799 | $form->handleRequest($request); |
||
800 | if ($form->isValid()) { |
||
801 | $data = $form->getData(); |
||
802 | $Customer = new Customer(); |
||
803 | $Customer |
||
804 | 3 | ->setName01($data['name01']) |
|
805 | 3 | ->setName02($data['name02']) |
|
806 | 3 | ->setKana01($data['kana01']) |
|
807 | 3 | ->setKana02($data['kana02']) |
|
808 | 3 | ->setCompanyName($data['company_name']) |
|
809 | 3 | ->setEmail($data['email']) |
|
810 | 3 | ->setTel01($data['tel01']) |
|
811 | 3 | ->setTel02($data['tel02']) |
|
812 | 3 | ->setTel03($data['tel03']) |
|
813 | 3 | ->setZip01($data['zip01']) |
|
814 | 3 | ->setZip02($data['zip02']) |
|
815 | 3 | ->setZipCode($data['zip01'] . $data['zip02']) |
|
816 | 3 | ->setPref($data['pref']) |
|
817 | 3 | ->setAddr01($data['addr01']) |
|
818 | ->setAddr02($data['addr02']); |
||
819 | |||
820 | // 非会員複数配送用 |
||
821 | $CustomerAddress = new CustomerAddress(); |
||
822 | $CustomerAddress |
||
823 | 3 | ->setCustomer($Customer) |
|
824 | 3 | ->setName01($data['name01']) |
|
825 | 3 | ->setName02($data['name02']) |
|
826 | 3 | ->setKana01($data['kana01']) |
|
827 | 3 | ->setKana02($data['kana02']) |
|
828 | 3 | ->setCompanyName($data['company_name']) |
|
829 | 3 | ->setTel01($data['tel01']) |
|
830 | 3 | ->setTel02($data['tel02']) |
|
831 | 3 | ->setTel03($data['tel03']) |
|
832 | 3 | ->setZip01($data['zip01']) |
|
833 | 3 | ->setZip02($data['zip02']) |
|
834 | 3 | ->setZipCode($data['zip01'] . $data['zip02']) |
|
835 | 3 | ->setPref($data['pref']) |
|
836 | 3 | ->setAddr01($data['addr01']) |
|
837 | 3 | ->setAddr02($data['addr02']) |
|
838 | ->setDelFlg(Constant::DISABLED); |
||
839 | $Customer->addCustomerAddress($CustomerAddress); |
||
840 | |||
841 | // 受注情報を取得 |
||
842 | $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']); |
||
843 | |||
844 | // 初回アクセス(受注データがない)の場合は, 受注情報を作成 |
||
845 | if (is_null($Order)) { |
||
846 | // 受注情報を作成 |
||
847 | $app['eccube.service.shopping']->createOrder($Customer); |
||
848 | } |
||
849 | |||
850 | // 非会員用セッションを作成 |
||
851 | 3 | $nonMember = array(); |
|
852 | 3 | $nonMember['customer'] = $Customer; |
|
853 | $nonMember['pref'] = $Customer->getPref()->getId(); |
||
854 | $app['session']->set($this->sessionKey, $nonMember); |
||
855 | |||
856 | 3 | $customerAddresses = array(); |
|
857 | 3 | $customerAddresses[] = $CustomerAddress; |
|
858 | $app['session']->set($this->sessionCustomerAddressKey, serialize($customerAddresses)); |
||
859 | |||
860 | return $app->redirect($app->url('shopping')); |
||
861 | |||
862 | } |
||
863 | } |
||
864 | |||
865 | 1 | return $app->render('Shopping/nonmember.twig', array( |
|
866 | 1 | 'form' => $form->createView(), |
|
867 | )); |
||
868 | 6 | } |
|
869 | |||
870 | /** |
||
871 | * 複数配送処理がクリックされた場合の処理 |
||
872 | */ |
||
873 | View Code Duplication | public function shippingMultipleChange(Application $app, Request $request) |
|
904 | |||
905 | |||
906 | /** |
||
907 | * 複数配送処理 |
||
908 | */ |
||
909 | public function shippingMultiple(Application $app, Request $request) |
||
1118 | |||
1119 | /** |
||
1120 | * 非会員用複数配送設定時の新規お届け先の設定 |
||
1121 | */ |
||
1122 | public function shippingMultipleEdit(Application $app, Request $request) |
||
1180 | |||
1181 | /** |
||
1182 | * 購入エラー画面表示 |
||
1183 | */ |
||
1184 | public function shoppingError(Application $app) |
||
1188 | |||
1189 | /** |
||
1190 | * 非会員でのお客様情報変更時の入力チェック |
||
1191 | */ |
||
1192 | private function customerValidation($app, $data) { |
||
1261 | |||
1262 | } |
||
1263 |