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:
| 1 | <?php |
||
| 33 | class CartController extends AbstractController |
||
|
|
|||
| 34 | { |
||
| 35 | /** |
||
| 36 | * カート画面. |
||
| 37 | * |
||
| 38 | * @param Application $app |
||
| 39 | * @param Request $request |
||
| 40 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 41 | */ |
||
| 42 | 2 | public function index(Application $app, Request $request) |
|
| 43 | { |
||
| 44 | 2 | $Cart = $app['eccube.service.cart']->getCart(); |
|
| 45 | |||
| 46 | // FRONT_CART_INDEX_INITIALIZE |
||
| 47 | 2 | $event = new EventArgs( |
|
| 48 | 2 | array(), |
|
| 49 | $request |
||
| 50 | ); |
||
| 51 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_INDEX_INITIALIZE, $event); |
|
| 52 | |||
| 53 | /* @var $BaseInfo \Eccube\Entity\BaseInfo */ |
||
| 54 | /* @var $Cart \Eccube\Entity\Cart */ |
||
| 55 | 2 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 56 | |||
| 57 | 2 | $isDeliveryFree = false; |
|
| 58 | 2 | $least = 0; |
|
| 59 | 2 | $quantity = 0; |
|
| 60 | 2 | if ($BaseInfo->getDeliveryFreeAmount()) { |
|
| 61 | if ($BaseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) { |
||
| 62 | // 送料無料(金額)を超えている |
||
| 63 | $isDeliveryFree = true; |
||
| 64 | } else { |
||
| 65 | $least = $BaseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice(); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | 2 | if ($BaseInfo->getDeliveryFreeQuantity()) { |
|
| 70 | if ($BaseInfo->getDeliveryFreeQuantity() <= $Cart->getTotalQuantity()) { |
||
| 71 | // 送料無料(個数)を超えている |
||
| 72 | $isDeliveryFree = true; |
||
| 73 | } else { |
||
| 74 | $quantity = $BaseInfo->getDeliveryFreeQuantity() - $Cart->getTotalQuantity(); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // FRONT_CART_INDEX_COMPLETE |
||
| 79 | 2 | $event = new EventArgs( |
|
| 80 | 2 | array(), |
|
| 81 | $request |
||
| 82 | ); |
||
| 83 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_INDEX_COMPLETE, $event); |
|
| 84 | |||
| 85 | 2 | if ($event->hasResponse()) { |
|
| 86 | return $event->getResponse(); |
||
| 87 | } |
||
| 88 | |||
| 89 | 2 | return $app->render( |
|
| 90 | 2 | 'Cart/index.twig', |
|
| 91 | array( |
||
| 92 | 2 | 'Cart' => $Cart, |
|
| 93 | 2 | 'least' => $least, |
|
| 94 | 2 | 'quantity' => $quantity, |
|
| 95 | 2 | 'is_delivery_free' => $isDeliveryFree, |
|
| 96 | ) |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * カートに商品を追加する. |
||
| 102 | * |
||
| 103 | * @param Application $app |
||
| 104 | * @param Request $request |
||
| 105 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 106 | */ |
||
| 107 | 34 | public function add(Application $app, Request $request) |
|
| 108 | { |
||
| 109 | 34 | $productClassId = $request->get('product_class_id'); |
|
| 110 | 34 | $quantity = $request->request->has('quantity') ? $request->get('quantity') : 1; |
|
| 111 | |||
| 112 | // FRONT_CART_ADD_INITIALIZE |
||
| 113 | 34 | $event = new EventArgs( |
|
| 114 | array( |
||
| 115 | 34 | 'productClassId' => $productClassId, |
|
| 116 | 34 | 'quantity' => $quantity, |
|
| 117 | ), |
||
| 118 | $request |
||
| 119 | ); |
||
| 120 | 34 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_ADD_INITIALIZE, $event); |
|
| 121 | |||
| 122 | try { |
||
| 123 | |||
| 124 | 34 | $productClassId = $event->getArgument('productClassId'); |
|
| 125 | 34 | $quantity = $event->getArgument('quantity'); |
|
| 126 | |||
| 127 | 34 | $app['eccube.service.cart']->addProduct($productClassId, $quantity)->save(); |
|
| 128 | |||
| 129 | // FRONT_CART_ADD_COMPLETE |
||
| 130 | 33 | $event = new EventArgs( |
|
| 131 | array( |
||
| 132 | 33 | 'productClassId' => $productClassId, |
|
| 133 | 33 | 'quantity' => $quantity, |
|
| 134 | ), |
||
| 135 | $request |
||
| 136 | ); |
||
| 137 | 33 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_ADD_COMPLETE, $event); |
|
| 138 | |||
| 139 | 33 | if ($event->hasResponse()) { |
|
| 140 | 33 | return $event->getResponse(); |
|
| 141 | } |
||
| 142 | |||
| 143 | 1 | } catch (CartException $e) { |
|
| 144 | |||
| 145 | // FRONT_CART_ADD_EXCEPTION |
||
| 146 | 1 | $event = new EventArgs( |
|
| 147 | array( |
||
| 148 | 1 | 'exception' => $e, |
|
| 149 | ), |
||
| 150 | $request |
||
| 151 | ); |
||
| 152 | 1 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_ADD_EXCEPTION, $event); |
|
| 153 | |||
| 154 | 1 | if ($event->hasResponse()) { |
|
| 155 | return $event->getResponse(); |
||
| 156 | } |
||
| 157 | |||
| 158 | 1 | $app->addRequestError($e->getMessage()); |
|
| 159 | } |
||
| 160 | |||
| 161 | 34 | return $app->redirect($app->url('cart')); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * カートに入っている商品の個数を1増やす. |
||
| 166 | * |
||
| 167 | * @param Application $app |
||
| 168 | * @param Request $request |
||
| 169 | * @param $productClassId |
||
| 170 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 171 | */ |
||
| 172 | 3 | View Code Duplication | public function up(Application $app, Request $request, $productClassId) |
| 173 | { |
||
| 174 | 3 | $this->isTokenValid($app); |
|
| 175 | |||
| 176 | // FRONT_CART_UP_INITIALIZE |
||
| 177 | 3 | $event = new EventArgs( |
|
| 178 | array( |
||
| 179 | 3 | 'productClassId' => $productClassId, |
|
| 180 | ), |
||
| 181 | $request |
||
| 182 | ); |
||
| 183 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_UP_INITIALIZE, $event); |
|
| 184 | |||
| 185 | try { |
||
| 186 | |||
| 187 | 3 | $productClassId = $event->getArgument('productClassId'); |
|
| 188 | |||
| 189 | 3 | $app['eccube.service.cart']->upProductQuantity($productClassId)->save(); |
|
| 190 | |||
| 191 | // FRONT_CART_UP_COMPLETE |
||
| 192 | 2 | $event = new EventArgs( |
|
| 193 | array( |
||
| 194 | 2 | 'productClassId' => $productClassId, |
|
| 195 | ), |
||
| 196 | $request |
||
| 197 | ); |
||
| 198 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_UP_COMPLETE, $event); |
|
| 199 | |||
| 200 | 2 | if ($event->hasResponse()) { |
|
| 201 | 2 | return $event->getResponse(); |
|
| 202 | } |
||
| 203 | |||
| 204 | 1 | } catch (CartException $e) { |
|
| 205 | |||
| 206 | // FRONT_CART_UP_EXCEPTION |
||
| 207 | 1 | $event = new EventArgs( |
|
| 208 | array( |
||
| 209 | 1 | 'exception' => $e, |
|
| 210 | ), |
||
| 211 | $request |
||
| 212 | ); |
||
| 213 | 1 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_UP_EXCEPTION, $event); |
|
| 214 | |||
| 215 | 1 | if ($event->hasResponse()) { |
|
| 216 | return $event->getResponse(); |
||
| 217 | } |
||
| 218 | |||
| 219 | 1 | $app->addRequestError($e->getMessage()); |
|
| 220 | } |
||
| 221 | |||
| 222 | 3 | return $app->redirect($app->url('cart')); |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * カートに入っている商品の個数を1減らす. |
||
| 227 | * マイナスになる場合は, 商品をカートから削除する. |
||
| 228 | * |
||
| 229 | * @param Application $app |
||
| 230 | * @param Request $request |
||
| 231 | * @param $productClassId |
||
| 232 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 233 | */ |
||
| 234 | 3 | View Code Duplication | public function down(Application $app, Request $request, $productClassId) |
| 235 | { |
||
| 236 | 3 | $this->isTokenValid($app); |
|
| 237 | |||
| 238 | // FRONT_CART_DOWN_INITIALIZE |
||
| 239 | 3 | $event = new EventArgs( |
|
| 240 | array( |
||
| 241 | 3 | 'productClassId' => $productClassId, |
|
| 242 | ), |
||
| 243 | $request |
||
| 244 | ); |
||
| 245 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_DOWN_INITIALIZE, $event); |
|
| 246 | |||
| 247 | try { |
||
| 248 | 3 | $productClassId = $event->getArgument('productClassId'); |
|
| 249 | 3 | $app['eccube.service.cart']->downProductQuantity($productClassId)->save(); |
|
| 250 | |||
| 251 | // FRONT_CART_UP_COMPLETE |
||
| 252 | 3 | $event = new EventArgs( |
|
| 253 | array( |
||
| 254 | 3 | 'productClassId' => $productClassId, |
|
| 255 | ), |
||
| 256 | $request |
||
| 257 | ); |
||
| 258 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_DOWN_COMPLETE, $event); |
|
| 259 | |||
| 260 | 3 | if ($event->hasResponse()) { |
|
| 261 | 3 | return $event->getResponse(); |
|
| 262 | } |
||
| 263 | |||
| 264 | } catch (CartException $e) { |
||
| 265 | |||
| 266 | // FRONT_CART_DOWN_EXCEPTION |
||
| 267 | $event = new EventArgs( |
||
| 268 | array( |
||
| 269 | 'exception' => $e, |
||
| 270 | ), |
||
| 271 | $request |
||
| 272 | ); |
||
| 273 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_DOWN_EXCEPTION, $event); |
||
| 274 | |||
| 275 | if ($event->hasResponse()) { |
||
| 276 | return $event->getResponse(); |
||
| 277 | } |
||
| 278 | |||
| 279 | $app->addRequestError($e->getMessage()); |
||
| 280 | } |
||
| 281 | |||
| 282 | 3 | return $app->redirect($app->url('cart')); |
|
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * カートに入っている商品を削除する. |
||
| 287 | * |
||
| 288 | * @param Application $app |
||
| 289 | * @param Request $request |
||
| 290 | * @param $productClassId |
||
| 291 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 292 | */ |
||
| 293 | 2 | public function remove(Application $app, Request $request, $productClassId) |
|
| 294 | { |
||
| 295 | 2 | $this->isTokenValid($app); |
|
| 296 | |||
| 297 | // FRONT_CART_REMOVE_INITIALIZE |
||
| 298 | 2 | $event = new EventArgs( |
|
| 299 | array( |
||
| 300 | 2 | 'productClassId' => $productClassId, |
|
| 301 | ), |
||
| 302 | $request |
||
| 303 | ); |
||
| 304 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_REMOVE_INITIALIZE, $event); |
|
| 305 | |||
| 306 | 2 | $productClassId = $event->getArgument('productClassId'); |
|
| 307 | 2 | $app['eccube.service.cart']->removeProduct($productClassId)->save(); |
|
| 308 | |||
| 309 | // FRONT_CART_REMOVE_COMPLETE |
||
| 310 | 2 | $event = new EventArgs( |
|
| 311 | array( |
||
| 312 | 2 | 'productClassId' => $productClassId, |
|
| 313 | ), |
||
| 314 | $request |
||
| 315 | ); |
||
| 316 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_REMOVE_COMPLETE, $event); |
|
| 317 | |||
| 318 | 2 | if ($event->hasResponse()) { |
|
| 319 | return $event->getResponse(); |
||
| 320 | } |
||
| 321 | |||
| 322 | 2 | return $app->redirect($app->url('cart')); |
|
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * カートに商品を個数を指定して設定する. |
||
| 327 | * |
||
| 328 | * @param Application $app |
||
| 329 | * @param Request $request |
||
| 330 | * @param $productClassId |
||
| 331 | * @param $quantity |
||
| 332 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 333 | * @throws CartException |
||
| 334 | * |
||
| 335 | * @deprecated since 3.0.0, to be removed in 3.1 |
||
| 336 | */ |
||
| 337 | public function setQuantity(Application $app, Request $request, $productClassId, $quantity) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * カートをロック状態に設定し、購入確認画面へ遷移する. |
||
| 348 | * |
||
| 349 | * @param Application $app |
||
| 350 | * @param Request $request |
||
| 351 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 352 | */ |
||
| 353 | public function buystep(Application $app, Request $request) |
||
| 378 | } |
||
| 379 |