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 EditController 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 EditController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class EditController extends AbstractController |
||
|
|
|||
| 38 | { |
||
| 39 | 17 | public function index(Application $app, Request $request, $id = null) |
|
| 40 | { |
||
| 41 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 42 | 17 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
|
| 43 | 17 | $softDeleteFilter->setExcludes(array( |
|
| 44 | 17 | 'Eccube\Entity\ProductClass', |
|
| 45 | 17 | 'Eccube\Entity\Product', |
|
| 46 | 17 | )); |
|
| 47 | |||
| 48 | 17 | $TargetOrder = null; |
|
| 49 | 17 | $OriginOrder = null; |
|
| 50 | |||
| 51 | 17 | if (is_null($id)) { |
|
| 52 | // 空のエンティティを作成. |
||
| 53 | 7 | $TargetOrder = $this->newOrder(); |
|
| 54 | 7 | } else { |
|
| 55 | 10 | $TargetOrder = $app['eccube.repository.order']->find($id); |
|
| 56 | 10 | if (is_null($TargetOrder)) { |
|
| 57 | 11 | throw new NotFoundHttpException(); |
|
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // 編集前の受注情報を保持 |
||
| 62 | 17 | $OriginOrder = clone $TargetOrder; |
|
| 63 | 17 | $OriginalOrderDetails = new ArrayCollection(); |
|
| 64 | |||
| 65 | 17 | foreach ($TargetOrder->getOrderDetails() as $OrderDetail) { |
|
| 66 | 10 | $OriginalOrderDetails->add($OrderDetail); |
|
| 67 | 17 | } |
|
| 68 | |||
| 69 | 17 | $builder = $app['form.factory'] |
|
| 70 | 17 | ->createBuilder('order', $TargetOrder); |
|
| 71 | |||
| 72 | 17 | $event = new EventArgs( |
|
| 73 | array( |
||
| 74 | 17 | 'builder' => $builder, |
|
| 75 | 17 | 'OriginOrder' => $OriginOrder, |
|
| 76 | 17 | 'TargetOrder' => $TargetOrder, |
|
| 77 | 17 | 'OriginOrderDetails' => $OriginalOrderDetails, |
|
| 78 | 17 | ), |
|
| 79 | 7 | $request |
|
| 80 | 17 | ); |
|
| 81 | 17 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE, $event); |
|
| 82 | |||
| 83 | 17 | $form = $builder->getForm(); |
|
| 84 | |||
| 85 | 17 | if ('POST' === $request->getMethod()) { |
|
| 86 | 11 | $form->handleRequest($request); |
|
| 87 | |||
| 88 | 11 | $event = new EventArgs( |
|
| 89 | array( |
||
| 90 | 11 | 'builder' => $builder, |
|
| 91 | 11 | 'OriginOrder' => $OriginOrder, |
|
| 92 | 11 | 'TargetOrder' => $TargetOrder, |
|
| 93 | 11 | 'OriginOrderDetails' => $OriginalOrderDetails, |
|
| 94 | 11 | ), |
|
| 95 | $request |
||
| 96 | 11 | ); |
|
| 97 | 11 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS, $event); |
|
| 98 | |||
| 99 | // 入力情報にもとづいて再計算. |
||
| 100 | 11 | $this->calculate($app, $TargetOrder); |
|
| 101 | |||
| 102 | // 登録ボタン押下 |
||
| 103 | 11 | switch ($request->get('mode')) { |
|
| 104 | 11 | case 'register': |
|
| 105 | 11 | if ($TargetOrder->getTotal() > $app['config']['max_total_fee']) { |
|
| 106 | $form['charge']->addError(new FormError('合計金額の上限を超えております。')); |
||
| 107 | 11 | } elseif ($form->isValid()) { |
|
| 108 | |||
| 109 | 11 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 110 | |||
| 111 | // お支払い方法の更新 |
||
| 112 | 11 | $TargetOrder->setPaymentMethod($TargetOrder->getPayment()->getMethod()); |
|
| 113 | |||
| 114 | // 配送業者・お届け時間の更新 |
||
| 115 | 11 | $Shippings = $TargetOrder->getShippings(); |
|
| 116 | 11 | foreach ($Shippings as $Shipping) { |
|
| 117 | 11 | $Shipping->setShippingDeliveryName($Shipping->getDelivery()->getName()); |
|
| 118 | 11 | if (!is_null($Shipping->getDeliveryTime())) { |
|
| 119 | 11 | $Shipping->setShippingDeliveryTime($Shipping->getDeliveryTime()->getDeliveryTime()); |
|
| 120 | 11 | } else { |
|
| 121 | $Shipping->setShippingDeliveryTime(null); |
||
| 122 | } |
||
| 123 | 11 | } |
|
| 124 | |||
| 125 | |||
| 126 | // 受注日/発送日/入金日の更新. |
||
| 127 | 11 | $this->updateDate($app, $TargetOrder, $OriginOrder); |
|
| 128 | |||
| 129 | // 受注明細で削除されているものをremove |
||
| 130 | 11 | foreach ($OriginalOrderDetails as $OrderDetail) { |
|
| 131 | 7 | if (false === $TargetOrder->getOrderDetails()->contains($OrderDetail)) { |
|
| 132 | $app['orm.em']->remove($OrderDetail); |
||
| 133 | } |
||
| 134 | 11 | } |
|
| 135 | |||
| 136 | |||
| 137 | 11 | if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) { |
|
| 138 | 4 | foreach ($TargetOrder->getOrderDetails() as $OrderDetail) { |
|
| 139 | /** @var $OrderDetail \Eccube\Entity\OrderDetail */ |
||
| 140 | 4 | $OrderDetail->setOrder($TargetOrder); |
|
| 141 | 4 | } |
|
| 142 | |||
| 143 | /** @var \Eccube\Entity\Shipping $Shipping */ |
||
| 144 | 4 | View Code Duplication | foreach ($Shippings as $Shipping) { |
| 145 | 4 | $shipmentItems = $Shipping->getShipmentItems(); |
|
| 146 | /** @var \Eccube\Entity\ShipmentItem $ShipmentItem */ |
||
| 147 | 4 | foreach ($shipmentItems as $ShipmentItem) { |
|
| 148 | 4 | $ShipmentItem->setOrder($TargetOrder); |
|
| 149 | 4 | $ShipmentItem->setShipping($Shipping); |
|
| 150 | 4 | $app['orm.em']->persist($ShipmentItem); |
|
| 151 | 4 | } |
|
| 152 | 4 | $Shipping->setOrder($TargetOrder); |
|
| 153 | 4 | $app['orm.em']->persist($Shipping); |
|
| 154 | 4 | } |
|
| 155 | 4 | } else { |
|
| 156 | |||
| 157 | 7 | $NewShipmentItems = new ArrayCollection(); |
|
| 158 | |||
| 159 | 7 | foreach ($TargetOrder->getOrderDetails() as $OrderDetail) { |
|
| 160 | /** @var $OrderDetail \Eccube\Entity\OrderDetail */ |
||
| 161 | 7 | $OrderDetail->setOrder($TargetOrder); |
|
| 162 | |||
| 163 | 7 | $NewShipmentItem = new ShipmentItem(); |
|
| 164 | $NewShipmentItem |
||
| 165 | 7 | ->setProduct($OrderDetail->getProduct()) |
|
| 166 | 7 | ->setProductClass($OrderDetail->getProductClass()) |
|
| 167 | 7 | ->setProductName($OrderDetail->getProduct()->getName()) |
|
| 168 | 7 | ->setProductCode($OrderDetail->getProductClass()->getCode()) |
|
| 169 | 7 | ->setClassCategoryName1($OrderDetail->getClassCategoryName1()) |
|
| 170 | 7 | ->setClassCategoryName2($OrderDetail->getClassCategoryName2()) |
|
| 171 | 7 | ->setClassName1($OrderDetail->getClassName1()) |
|
| 172 | 7 | ->setClassName2($OrderDetail->getClassName2()) |
|
| 173 | 7 | ->setPrice($OrderDetail->getPrice()) |
|
| 174 | 7 | ->setQuantity($OrderDetail->getQuantity()) |
|
| 175 | 7 | ->setOrder($TargetOrder); |
|
| 176 | 7 | $NewShipmentItems[] = $NewShipmentItem; |
|
| 177 | |||
| 178 | 7 | } |
|
| 179 | // 配送商品の更新. delete/insert. |
||
| 180 | 7 | $Shippings = $TargetOrder->getShippings(); |
|
| 181 | 7 | View Code Duplication | foreach ($Shippings as $Shipping) { |
| 182 | 7 | $ShipmentItems = $Shipping->getShipmentItems(); |
|
| 183 | 7 | foreach ($ShipmentItems as $ShipmentItem) { |
|
| 184 | 4 | $app['orm.em']->remove($ShipmentItem); |
|
| 185 | 7 | } |
|
| 186 | 7 | $ShipmentItems->clear(); |
|
| 187 | 7 | foreach ($NewShipmentItems as $NewShipmentItem) { |
|
| 188 | 7 | $NewShipmentItem->setShipping($Shipping); |
|
| 189 | 7 | $ShipmentItems->add($NewShipmentItem); |
|
| 190 | 7 | } |
|
| 191 | 7 | } |
|
| 192 | } |
||
| 193 | |||
| 194 | 11 | $Customer = $TargetOrder->getCustomer(); |
|
| 195 | 11 | if ($Customer) { |
|
| 196 | // 受注情報の会員情報を更新 |
||
| 197 | 11 | $TargetOrder->setSex($Customer->getSex()); |
|
| 198 | 11 | $TargetOrder->setJob($Customer->getJob()); |
|
| 199 | 11 | $TargetOrder->setBirth($Customer->getBirth()); |
|
| 200 | 11 | } |
|
| 201 | |||
| 202 | 11 | $app['orm.em']->persist($TargetOrder); |
|
| 203 | 11 | $app['orm.em']->flush(); |
|
| 204 | |||
| 205 | 11 | if ($Customer) { |
|
| 206 | // 会員の場合、購入回数、購入金額などを更新 |
||
| 207 | 11 | $app['eccube.repository.customer']->updateBuyData($app, $Customer, $TargetOrder->getOrderStatus()->getId()); |
|
| 208 | 11 | } |
|
| 209 | |||
| 210 | 11 | $event = new EventArgs( |
|
| 211 | array( |
||
| 212 | 11 | 'form' => $form, |
|
| 213 | 11 | 'OriginOrder' => $OriginOrder, |
|
| 214 | 11 | 'TargetOrder' => $TargetOrder, |
|
| 215 | 11 | 'OriginOrderDetails' => $OriginalOrderDetails, |
|
| 216 | 11 | 'Customer' => $Customer, |
|
| 217 | 11 | ), |
|
| 218 | $request |
||
| 219 | 11 | ); |
|
| 220 | 11 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE, $event); |
|
| 221 | |||
| 222 | 11 | $app->addSuccess('admin.order.save.complete', 'admin'); |
|
| 223 | |||
| 224 | 11 | return $app->redirect($app->url('admin_order_edit', array('id' => $TargetOrder->getId()))); |
|
| 225 | 11 | } |
|
| 226 | |||
| 227 | 2 | break; |
|
| 228 | |||
| 229 | case 'add_delivery': |
||
| 230 | // お届け先情報の新規追加 |
||
| 231 | |||
| 232 | $form = $builder->getForm(); |
||
| 233 | |||
| 234 | $Shipping = new \Eccube\Entity\Shipping(); |
||
| 235 | $Shipping->setDelFlg(Constant::DISABLED); |
||
| 236 | |||
| 237 | $TargetOrder->addShipping($Shipping); |
||
| 238 | |||
| 239 | 11 | $Shipping->setOrder($TargetOrder); |
|
| 240 | |||
| 241 | 11 | $form->setData($TargetOrder); |
|
| 242 | |||
| 243 | break; |
||
| 244 | |||
| 245 | default: |
||
| 246 | break; |
||
| 247 | 2 | } |
|
| 248 | 2 | } |
|
| 249 | |||
| 250 | // 会員検索フォーム |
||
| 251 | 8 | $builder = $app['form.factory'] |
|
| 252 | 8 | ->createBuilder('admin_search_customer'); |
|
| 253 | |||
| 254 | 8 | $event = new EventArgs( |
|
| 255 | array( |
||
| 256 | 8 | 'builder' => $builder, |
|
| 257 | 8 | 'OriginOrder' => $OriginOrder, |
|
| 258 | 8 | 'TargetOrder' => $TargetOrder, |
|
| 259 | 8 | 'OriginOrderDetails' => $OriginalOrderDetails, |
|
| 260 | 8 | ), |
|
| 261 | $request |
||
| 262 | 8 | ); |
|
| 263 | 8 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_INITIALIZE, $event); |
|
| 264 | |||
| 265 | 8 | $searchCustomerModalForm = $builder->getForm(); |
|
| 266 | |||
| 267 | // 商品検索フォーム |
||
| 268 | 8 | $builder = $app['form.factory'] |
|
| 269 | 8 | ->createBuilder('admin_search_product'); |
|
| 270 | |||
| 271 | 8 | $event = new EventArgs( |
|
| 272 | array( |
||
| 273 | 8 | 'builder' => $builder, |
|
| 274 | 8 | 'OriginOrder' => $OriginOrder, |
|
| 275 | 8 | 'TargetOrder' => $TargetOrder, |
|
| 276 | 8 | 'OriginOrderDetails' => $OriginalOrderDetails, |
|
| 277 | 8 | ), |
|
| 278 | $request |
||
| 279 | 8 | ); |
|
| 280 | 8 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE, $event); |
|
| 281 | |||
| 282 | 8 | $searchProductModalForm = $builder->getForm(); |
|
| 283 | |||
| 284 | // 配送業者のお届け時間 |
||
| 285 | 8 | $times = array(); |
|
| 286 | 8 | $deliveries = $app['eccube.repository.delivery']->findAll(); |
|
| 287 | 8 | foreach ($deliveries as $Delivery) { |
|
| 288 | 8 | $deliveryTiems = $Delivery->getDeliveryTimes(); |
|
| 289 | 8 | foreach ($deliveryTiems as $DeliveryTime) { |
|
| 290 | 8 | $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime(); |
|
| 291 | 8 | } |
|
| 292 | 8 | } |
|
| 293 | |||
| 294 | 8 | return $app->render('Order/edit.twig', array( |
|
| 295 | 8 | 'form' => $form->createView(), |
|
| 296 | 8 | 'searchCustomerModalForm' => $searchCustomerModalForm->createView(), |
|
| 297 | 8 | 'searchProductModalForm' => $searchProductModalForm->createView(), |
|
| 298 | 8 | 'Order' => $TargetOrder, |
|
| 299 | 8 | 'id' => $id, |
|
| 300 | 17 | 'shippingDeliveryTimes' => $app['serializer']->serialize($times, 'json'), |
|
| 301 | 8 | )); |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * 顧客情報を検索する. |
||
| 306 | * |
||
| 307 | * @param Application $app |
||
| 308 | * @param Request $request |
||
| 309 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 310 | */ |
||
| 311 | 3 | public function searchCustomer(Application $app, Request $request) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * 顧客情報を検索する. |
||
| 368 | * |
||
| 369 | * @param Application $app |
||
| 370 | * @param Request $request |
||
| 371 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 372 | */ |
||
| 373 | 3 | public function searchCustomerById(Application $app, Request $request) |
|
| 432 | |||
| 433 | 14 | public function searchProduct(Application $app, Request $request, $page_no = null) |
|
| 518 | |||
| 519 | 7 | protected function newOrder() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * フォームからの入直内容に基づいて、受注情報の再計算を行う |
||
| 532 | * |
||
| 533 | * @param $app |
||
| 534 | * @param $Order |
||
| 535 | */ |
||
| 536 | 11 | protected function calculate($app, \Eccube\Entity\Order $Order) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * 受注ステータスに応じて, 受注日/入金日/発送日を更新する, |
||
| 611 | * 発送済ステータスが設定された場合は, お届け先情報の発送日も更新を行う. |
||
| 612 | * |
||
| 613 | * 編集の場合 |
||
| 614 | * - 受注ステータスが他のステータスから発送済へ変更された場合に発送日を更新 |
||
| 615 | * - 受注ステータスが他のステータスから入金済へ変更された場合に入金日を更新 |
||
| 616 | * |
||
| 617 | * 新規登録の場合 |
||
| 618 | * - 受注日を更新 |
||
| 619 | * - 受注ステータスが発送済に設定された場合に発送日を更新 |
||
| 620 | * - 受注ステータスが入金済に設定された場合に入金日を更新 |
||
| 621 | * |
||
| 622 | * |
||
| 623 | * @param $app |
||
| 624 | * @param $TargetOrder |
||
| 625 | * @param $OriginOrder |
||
| 626 | */ |
||
| 627 | 11 | protected function updateDate($app, $TargetOrder, $OriginOrder) |
|
| 669 | } |
||
| 670 |