| Conditions | 24 |
| Paths | 637 |
| Total Lines | 192 |
| Code Lines | 112 |
| Lines | 22 |
| Ratio | 11.46 % |
| Tests | 20 |
| CRAP Score | 79.8829 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 513 |