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 |
||
| 35 | class EditController extends AbstractController |
||
|
|
|||
| 36 | { |
||
| 37 | 4 | public function index(Application $app, Request $request, $id = null) |
|
| 38 | { |
||
| 39 | 4 | $TargetOrder = null; |
|
| 40 | 4 | $OriginOrder = null; |
|
| 41 | |||
| 42 | if (is_null($id)) { |
||
| 43 | // 空のエンティティを作成. |
||
| 44 | $TargetOrder = $this->newOrder(); |
||
| 45 | } else { |
||
| 46 | $TargetOrder = $app['eccube.repository.order']->find($id); |
||
| 47 | if (is_null($TargetOrder)) { |
||
| 48 | throw new NotFoundHttpException(); |
||
| 49 | } |
||
| 50 | 2 | } |
|
| 51 | |||
| 52 | // 編集前の受注情報を保持 |
||
| 53 | 4 | $OriginOrder = clone $TargetOrder; |
|
| 54 | $OriginalOrderDetails = new ArrayCollection(); |
||
| 55 | |||
| 56 | 4 | foreach ($TargetOrder->getOrderDetails() as $OrderDetail) { |
|
| 57 | $OriginalOrderDetails->add($OrderDetail); |
||
| 58 | 2 | } |
|
| 59 | |||
| 60 | $builder = $app['form.factory'] |
||
| 61 | ->createBuilder('order', $TargetOrder); |
||
| 62 | |||
| 63 | $form = $builder->getForm(); |
||
| 64 | |||
| 65 | if ('POST' === $request->getMethod()) { |
||
| 66 | $form->handleRequest($request); |
||
| 67 | |||
| 68 | // 入力情報にもとづいて再計算. |
||
| 69 | $this->calculate($app, $TargetOrder); |
||
| 70 | |||
| 71 | // 登録ボタン押下 |
||
| 72 | 2 | switch ($request->get('mode')) { |
|
| 73 | 2 | case 'register': |
|
| 74 | if ($TargetOrder->getTotal() > $app['config']['max_total_fee']) { |
||
| 75 | $form['charge']->addError(new FormError('合計金額の上限を超えております。')); |
||
| 76 | } elseif ($form->isValid()) { |
||
| 77 | |||
| 78 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 79 | |||
| 80 | // お支払い方法の更新 |
||
| 81 | $TargetOrder->setPaymentMethod($TargetOrder->getPayment()->getMethod()); |
||
| 82 | |||
| 83 | // 配送業者・お届け時間の更新 |
||
| 84 | $Shippings = $TargetOrder->getShippings(); |
||
| 85 | foreach ($Shippings as $Shipping) { |
||
| 86 | $Shipping->setShippingDeliveryName($Shipping->getDelivery()->getName()); |
||
| 87 | if (!is_null($Shipping->getDeliveryTime())) { |
||
| 88 | $Shipping->setShippingDeliveryTime($Shipping->getDeliveryTime()->getDeliveryTime()); |
||
| 89 | } else { |
||
| 90 | $Shipping->setShippingDeliveryTime(null); |
||
| 91 | 2 | } |
|
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | // 受注日/発送日/入金日の更新. |
||
| 96 | $this->updateDate($app, $TargetOrder, $OriginOrder); |
||
| 97 | |||
| 98 | // 受注明細で削除されているものをremove |
||
| 99 | foreach ($OriginalOrderDetails as $OrderDetail) { |
||
| 100 | if (false === $TargetOrder->getOrderDetails()->contains($OrderDetail)) { |
||
| 101 | $app['orm.em']->remove($OrderDetail); |
||
| 102 | } |
||
| 103 | 1 | } |
|
| 104 | |||
| 105 | |||
| 106 | if ($BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) { |
||
| 107 | foreach ($TargetOrder->getOrderDetails() as $OrderDetail) { |
||
| 108 | /** @var $OrderDetail \Eccube\Entity\OrderDetail */ |
||
| 109 | $OrderDetail->setOrder($TargetOrder); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** @var \Eccube\Entity\Shipping $Shipping */ |
||
| 113 | View Code Duplication | foreach ($Shippings as $Shipping) { |
|
| 114 | $shipmentItems = $Shipping->getShipmentItems(); |
||
| 115 | /** @var \Eccube\Entity\ShipmentItem $ShipmentItem */ |
||
| 116 | foreach ($shipmentItems as $ShipmentItem) { |
||
| 117 | $ShipmentItem->setOrder($TargetOrder); |
||
| 118 | $ShipmentItem->setShipping($Shipping); |
||
| 119 | $app['orm.em']->persist($ShipmentItem); |
||
| 120 | } |
||
| 121 | $Shipping->setOrder($TargetOrder); |
||
| 122 | $app['orm.em']->persist($Shipping); |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | |||
| 126 | $NewShipmentItems = new ArrayCollection(); |
||
| 127 | |||
| 128 | 2 | foreach ($TargetOrder->getOrderDetails() as $OrderDetail) { |
|
| 129 | /** @var $OrderDetail \Eccube\Entity\OrderDetail */ |
||
| 130 | $OrderDetail->setOrder($TargetOrder); |
||
| 131 | |||
| 132 | $NewShipmentItem = new ShipmentItem(); |
||
| 133 | $NewShipmentItem |
||
| 134 | ->setProduct($OrderDetail->getProduct()) |
||
| 135 | ->setProductClass($OrderDetail->getProductClass()) |
||
| 136 | ->setProductName($OrderDetail->getProduct()->getName()) |
||
| 137 | ->setProductCode($OrderDetail->getProductClass()->getCode()) |
||
| 138 | ->setClassCategoryName1($OrderDetail->getClassCategoryName1()) |
||
| 139 | ->setClassCategoryName2($OrderDetail->getClassCategoryName2()) |
||
| 140 | ->setClassName1($OrderDetail->getClassName1()) |
||
| 141 | ->setClassName2($OrderDetail->getClassName2()) |
||
| 142 | ->setPrice($OrderDetail->getPrice()) |
||
| 143 | ->setQuantity($OrderDetail->getQuantity()) |
||
| 144 | ->setOrder($TargetOrder); |
||
| 145 | $NewShipmentItems[] = $NewShipmentItem; |
||
| 146 | |||
| 147 | } |
||
| 148 | // 配送商品の更新. delete/insert. |
||
| 149 | $Shippings = $TargetOrder->getShippings(); |
||
| 150 | View Code Duplication | foreach ($Shippings as $Shipping) { |
|
| 151 | $ShipmentItems = $Shipping->getShipmentItems(); |
||
| 152 | foreach ($ShipmentItems as $ShipmentItem) { |
||
| 153 | $app['orm.em']->remove($ShipmentItem); |
||
| 154 | 1 | } |
|
| 155 | $ShipmentItems->clear(); |
||
| 156 | foreach ($NewShipmentItems as $NewShipmentItem) { |
||
| 157 | $NewShipmentItem->setShipping($Shipping); |
||
| 158 | $ShipmentItems->add($NewShipmentItem); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $app['orm.em']->persist($TargetOrder); |
||
| 164 | $app['orm.em']->flush(); |
||
| 165 | |||
| 166 | $Customer = $TargetOrder->getCustomer(); |
||
| 167 | 2 | if ($Customer) { |
|
| 168 | // 会員の場合、購入回数、購入金額などを更新 |
||
| 169 | $app['eccube.repository.customer']->updateBuyData($app, $Customer, $TargetOrder->getOrderStatus()->getId()); |
||
| 170 | } |
||
| 171 | |||
| 172 | $app->addSuccess('admin.order.save.complete', 'admin'); |
||
| 173 | |||
| 174 | return $app->redirect($app->url('admin_order_edit', array('id' => $TargetOrder->getId()))); |
||
| 175 | } |
||
| 176 | |||
| 177 | break; |
||
| 178 | |||
| 179 | case 'add_delivery': |
||
| 180 | // お届け先情報の新規追加 |
||
| 181 | |||
| 182 | $form = $builder->getForm(); |
||
| 183 | |||
| 184 | $Shipping = new \Eccube\Entity\Shipping(); |
||
| 185 | $Shipping->setDelFlg(Constant::DISABLED); |
||
| 186 | |||
| 187 | $TargetOrder->addShipping($Shipping); |
||
| 188 | |||
| 189 | $Shipping->setOrder($TargetOrder); |
||
| 190 | |||
| 191 | $form->setData($TargetOrder); |
||
| 192 | |||
| 193 | break; |
||
| 194 | |||
| 195 | default: |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | // 会員検索フォーム |
||
| 201 | $searchCustomerModalForm = $app['form.factory'] |
||
| 202 | ->createBuilder('admin_search_customer') |
||
| 203 | ->getForm(); |
||
| 204 | |||
| 205 | // 商品検索フォーム |
||
| 206 | $searchProductModalForm = $app['form.factory'] |
||
| 207 | ->createBuilder('admin_search_product') |
||
| 208 | ->getForm(); |
||
| 209 | |||
| 210 | // 配送業者のお届け時間 |
||
| 211 | 2 | $times = array(); |
|
| 212 | $deliveries = $app['eccube.repository.delivery']->findAll(); |
||
| 213 | foreach ($deliveries as $Delivery) { |
||
| 214 | $deliveryTiems = $Delivery->getDeliveryTimes(); |
||
| 215 | foreach ($deliveryTiems as $DeliveryTime) { |
||
| 216 | $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime(); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | 2 | return $app->render('Order/edit.twig', array( |
|
| 221 | 2 | 'form' => $form->createView(), |
|
| 222 | 2 | 'searchCustomerModalForm' => $searchCustomerModalForm->createView(), |
|
| 223 | 2 | 'searchProductModalForm' => $searchProductModalForm->createView(), |
|
| 224 | 'Order' => $TargetOrder, |
||
| 225 | 'id' => $id, |
||
| 226 | 'shippingDeliveryTimes' => $app['serializer']->serialize($times, 'json'), |
||
| 227 | )); |
||
| 228 | 4 | } |
|
| 229 | |||
| 230 | /** |
||
| 231 | * 顧客情報を検索する. |
||
| 232 | * |
||
| 233 | * @param Application $app |
||
| 234 | * @param Request $request |
||
| 235 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 236 | */ |
||
| 237 | 1 | public function searchCustomer(Application $app, Request $request) |
|
| 238 | { |
||
| 239 | if ($request->isXmlHttpRequest()) { |
||
| 240 | $app['monolog']->addDebug('search customer start.'); |
||
| 241 | |||
| 242 | $searchData = array( |
||
| 243 | 1 | 'multi' => $request->get('search_word'), |
|
| 244 | 1 | ); |
|
| 245 | |||
| 246 | $Customers = $app['eccube.repository.customer'] |
||
| 247 | ->getQueryBuilderBySearchData($searchData) |
||
| 248 | 1 | ->getQuery() |
|
| 249 | ->getResult(); |
||
| 250 | |||
| 251 | 1 | if (empty($Customers)) { |
|
| 252 | $app['monolog']->addDebug('search customer not found.'); |
||
| 253 | } |
||
| 254 | |||
| 255 | 1 | $data = array(); |
|
| 256 | |||
| 257 | 1 | $formatTel = '%s-%s-%s'; |
|
| 258 | 1 | $formatName = '%s%s(%s%s)'; |
|
| 259 | foreach ($Customers as $Customer) { |
||
| 260 | $data[] = array( |
||
| 261 | 1 | 'id' => $Customer->getId(), |
|
| 262 | 'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(), $Customer->getKana01(), |
||
| 263 | $Customer->getKana02()), |
||
| 264 | 'tel' => sprintf($formatTel, $Customer->getTel01(), $Customer->getTel02(), $Customer->getTel03()), |
||
| 265 | ); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $app->json($data); |
||
| 269 | } |
||
| 270 | 1 | } |
|
| 271 | |||
| 272 | /** |
||
| 273 | * 顧客情報を検索する. |
||
| 274 | * |
||
| 275 | * @param Application $app |
||
| 276 | * @param Request $request |
||
| 277 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 278 | */ |
||
| 279 | 1 | public function searchCustomerById(Application $app, Request $request) |
|
| 280 | { |
||
| 281 | if ($request->isXmlHttpRequest()) { |
||
| 282 | $app['monolog']->addDebug('search customer by id start.'); |
||
| 283 | |||
| 284 | /** @var $Customer \Eccube\Entity\Customer */ |
||
| 285 | $Customer = $app['eccube.repository.customer'] |
||
| 286 | ->find($request->get('id')); |
||
| 287 | |||
| 288 | if (is_null($Customer)) { |
||
| 289 | $app['monolog']->addDebug('search customer by id not found.'); |
||
| 290 | |||
| 291 | return $app->json(array(), 404); |
||
| 292 | } |
||
| 293 | |||
| 294 | $app['monolog']->addDebug('search customer by id found.'); |
||
| 295 | |||
| 296 | $data = array( |
||
| 297 | 1 | 'id' => $Customer->getId(), |
|
| 298 | 1 | 'name01' => $Customer->getName01(), |
|
| 299 | 1 | 'name02' => $Customer->getName02(), |
|
| 300 | 1 | 'kana01' => $Customer->getKana01(), |
|
| 301 | 1 | 'kana02' => $Customer->getKana02(), |
|
| 302 | 1 | 'zip01' => $Customer->getZip01(), |
|
| 303 | 1 | 'zip02' => $Customer->getZip02(), |
|
| 304 | 'pref' => is_null($Customer->getPref()) ? null : $Customer->getPref()->getId(), |
||
| 305 | 1 | 'addr01' => $Customer->getAddr01(), |
|
| 306 | 1 | 'addr02' => $Customer->getAddr02(), |
|
| 307 | 1 | 'email' => $Customer->getEmail(), |
|
| 308 | 1 | 'tel01' => $Customer->getTel01(), |
|
| 309 | 1 | 'tel02' => $Customer->getTel02(), |
|
| 310 | 1 | 'tel03' => $Customer->getTel03(), |
|
| 311 | 1 | 'fax01' => $Customer->getFax01(), |
|
| 312 | 1 | 'fax02' => $Customer->getFax02(), |
|
| 313 | 1 | 'fax03' => $Customer->getFax03(), |
|
| 314 | 1 | 'company_name' => $Customer->getCompanyName(), |
|
| 315 | ); |
||
| 316 | |||
| 317 | return $app->json($data); |
||
| 318 | } |
||
| 319 | 1 | } |
|
| 320 | |||
| 321 | 1 | public function searchProduct(Application $app, Request $request) |
|
| 322 | { |
||
| 323 | if ($request->isXmlHttpRequest()) { |
||
| 324 | $app['monolog']->addDebug('search product start.'); |
||
| 325 | |||
| 326 | $searchData = array( |
||
| 327 | 1 | 'name' => $request->get('id'), |
|
| 328 | 1 | ); |
|
| 329 | |||
| 330 | if ($categoryId = $request->get('category_id')) { |
||
| 331 | $Category = $app['eccube.repository.category']->find($categoryId); |
||
| 332 | $searchData['category_id'] = $Category; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** @var $Products \Eccube\Entity\Product[] */ |
||
| 336 | $Products = $app['eccube.repository.product'] |
||
| 337 | ->getQueryBuilderBySearchData($searchData) |
||
| 338 | 1 | ->getQuery() |
|
| 339 | ->getResult(); |
||
| 340 | |||
| 341 | 1 | if (empty($Products)) { |
|
| 342 | $app['monolog']->addDebug('search product not found.'); |
||
| 343 | } |
||
| 344 | |||
| 345 | 1 | $forms = array(); |
|
| 346 | foreach ($Products as $Product) { |
||
| 347 | /* @var $builder \Symfony\Component\Form\FormBuilderInterface */ |
||
| 348 | $builder = $app['form.factory']->createNamedBuilder('', 'add_cart', null, array( |
||
| 349 | 'product' => $Product, |
||
| 350 | )); |
||
| 351 | $addCartForm = $builder->getForm(); |
||
| 352 | $forms[$Product->getId()] = $addCartForm->createView(); |
||
| 353 | 1 | } |
|
| 354 | |||
| 355 | 1 | return $app->render('Order/search_product.twig', array( |
|
| 356 | 'forms' => $forms, |
||
| 357 | 'Products' => $Products, |
||
| 358 | )); |
||
| 359 | } |
||
| 360 | 1 | } |
|
| 361 | |||
| 362 | 2 | protected function newOrder() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * フォームからの入直内容に基づいて、受注情報の再計算を行う |
||
| 375 | * |
||
| 376 | * @param $app |
||
| 377 | * @param $Order |
||
| 378 | */ |
||
| 379 | 2 | protected function calculate($app, \Eccube\Entity\Order $Order) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * 受注ステータスに応じて, 受注日/入金日/発送日を更新する, |
||
| 454 | * 発送済ステータスが設定された場合は, お届け先情報の発送日も更新を行う. |
||
| 455 | * |
||
| 456 | * 編集の場合 |
||
| 457 | * - 受注ステータスが他のステータスから発送済へ変更された場合に発送日を更新 |
||
| 458 | * - 受注ステータスが他のステータスから入金済へ変更された場合に入金日を更新 |
||
| 459 | * |
||
| 460 | * 新規登録の場合 |
||
| 461 | * - 受注日を更新 |
||
| 462 | * - 受注ステータスが発送済に設定された場合に発送日を更新 |
||
| 463 | * - 受注ステータスが入金済に設定された場合に入金日を更新 |
||
| 464 | * |
||
| 465 | * |
||
| 466 | * @param $app |
||
| 467 | * @param $TargetOrder |
||
| 468 | * @param $OriginOrder |
||
| 469 | */ |
||
| 470 | 2 | protected function updateDate($app, $TargetOrder, $OriginOrder) |
|
| 512 | } |
||
| 513 |