| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 239 |
| Code Lines | 159 |
| Lines | 21 |
| Ratio | 8.79 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 207 | public function edit(Application $app, Request $request, $id = null) |
||
| 208 | { |
||
| 209 | $has_class = false; |
||
| 210 | if (is_null($id)) { |
||
| 211 | $Product = new \Eccube\Entity\Product(); |
||
| 212 | $ProductClass = new \Eccube\Entity\ProductClass(); |
||
| 213 | $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE); |
||
| 214 | $Product |
||
| 215 | ->setDelFlg(Constant::DISABLED) |
||
| 216 | ->addProductClass($ProductClass) |
||
| 217 | ->setStatus($Disp); |
||
| 218 | $ProductClass |
||
| 219 | ->setDelFlg(Constant::DISABLED) |
||
| 220 | ->setStockUnlimited(true) |
||
| 221 | ->setProduct($Product); |
||
| 222 | $ProductStock = new \Eccube\Entity\ProductStock(); |
||
| 223 | $ProductClass->setProductStock($ProductStock); |
||
| 224 | $ProductStock->setProductClass($ProductClass); |
||
| 225 | } else { |
||
| 226 | $Product = $app['eccube.repository.product']->find($id); |
||
| 227 | if (!$Product) { |
||
| 228 | throw new NotFoundHttpException(); |
||
| 229 | } |
||
| 230 | // 規格あり商品か |
||
| 231 | $has_class = $Product->hasProductClass(); |
||
| 232 | if (!$has_class) { |
||
| 233 | $ProductClasses = $Product->getProductClasses(); |
||
| 234 | $ProductClass = $ProductClasses[0]; |
||
| 235 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 236 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
|
| 237 | $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate()); |
||
| 238 | } |
||
| 239 | $ProductStock = $ProductClasses[0]->getProductStock(); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | $builder = $app['form.factory'] |
||
| 244 | ->createBuilder('admin_product', $Product); |
||
| 245 | |||
| 246 | // 規格あり商品の場合、規格関連情報をFormから除外 |
||
| 247 | if ($has_class) { |
||
| 248 | $builder->remove('class'); |
||
| 249 | } |
||
| 250 | |||
| 251 | $event = new EventArgs( |
||
| 252 | array( |
||
| 253 | 'builder' => $builder, |
||
| 254 | 'Product' => $Product, |
||
| 255 | ), |
||
| 256 | $request |
||
| 257 | ); |
||
| 258 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE, $event); |
||
| 259 | |||
| 260 | $form = $builder->getForm(); |
||
| 261 | |||
| 262 | if (!$has_class) { |
||
| 263 | $ProductClass->setStockUnlimited((boolean)$ProductClass->getStockUnlimited()); |
||
| 264 | $form['class']->setData($ProductClass); |
||
| 265 | } |
||
| 266 | |||
| 267 | // ファイルの登録 |
||
| 268 | $images = array(); |
||
| 269 | $ProductImages = $Product->getProductImage(); |
||
| 270 | foreach ($ProductImages as $ProductImage) { |
||
| 271 | $images[] = $ProductImage->getFileName(); |
||
| 272 | } |
||
| 273 | $form['images']->setData($images); |
||
| 274 | |||
| 275 | $categories = array(); |
||
| 276 | $ProductCategories = $Product->getProductCategories(); |
||
| 277 | foreach ($ProductCategories as $ProductCategory) { |
||
| 278 | /* @var $ProductCategory \Eccube\Entity\ProductCategory */ |
||
| 279 | $categories[] = $ProductCategory->getCategory(); |
||
| 280 | } |
||
| 281 | $form['Category']->setData($categories); |
||
| 282 | |||
| 283 | if ('POST' === $request->getMethod()) { |
||
| 284 | $form->handleRequest($request); |
||
| 285 | if ($form->isValid()) { |
||
| 286 | $Product = $form->getData(); |
||
| 287 | |||
| 288 | if (!$has_class) { |
||
| 289 | $ProductClass = $form['class']->getData(); |
||
| 290 | |||
| 291 | // 個別消費税 |
||
| 292 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 293 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { |
|
| 294 | if ($ProductClass->getTaxRate()) { |
||
| 295 | if ($ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
||
| 296 | $ProductClass->getTaxRule()->setTaxRate($ProductClass->getTaxRate()); |
||
| 297 | } else { |
||
| 298 | $taxrule = $app['eccube.repository.tax_rule']->newTaxRule(); |
||
| 299 | $taxrule->setTaxRate($ProductClass->getTaxRate()); |
||
| 300 | $taxrule->setApplyDate(new \DateTime()); |
||
| 301 | $taxrule->setProduct($Product); |
||
| 302 | $taxrule->setProductClass($ProductClass); |
||
| 303 | $ProductClass->setTaxRule($taxrule); |
||
| 304 | } |
||
| 305 | } else { |
||
| 306 | if ($ProductClass->getTaxRule()) { |
||
| 307 | $ProductClass->getTaxRule()->setDelFlg(Constant::ENABLED); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | $app['orm.em']->persist($ProductClass); |
||
| 312 | |||
| 313 | // 在庫情報を作成 |
||
| 314 | if (!$ProductClass->getStockUnlimited()) { |
||
| 315 | $ProductStock->setStock($ProductClass->getStock()); |
||
| 316 | } else { |
||
| 317 | // 在庫無制限時はnullを設定 |
||
| 318 | $ProductStock->setStock(null); |
||
| 319 | } |
||
| 320 | $app['orm.em']->persist($ProductStock); |
||
| 321 | } |
||
| 322 | |||
| 323 | // カテゴリの登録 |
||
| 324 | // 一度クリア |
||
| 325 | /* @var $Product \Eccube\Entity\Product */ |
||
| 326 | foreach ($Product->getProductCategories() as $ProductCategory) { |
||
| 327 | $Product->removeProductCategory($ProductCategory); |
||
| 328 | $app['orm.em']->remove($ProductCategory); |
||
| 329 | } |
||
| 330 | $app['orm.em']->persist($Product); |
||
| 331 | $app['orm.em']->flush(); |
||
| 332 | |||
| 333 | $count = 1; |
||
| 334 | $Categories = $form->get('Category')->getData(); |
||
| 335 | foreach ($Categories as $Category) { |
||
| 336 | $ProductCategory = new \Eccube\Entity\ProductCategory(); |
||
| 337 | $ProductCategory |
||
| 338 | ->setProduct($Product) |
||
| 339 | ->setProductId($Product->getId()) |
||
| 340 | ->setCategory($Category) |
||
| 341 | ->setCategoryId($Category->getId()) |
||
| 342 | ->setRank($count); |
||
| 343 | $app['orm.em']->persist($ProductCategory); |
||
| 344 | $count++; |
||
| 345 | /* @var $Product \Eccube\Entity\Product */ |
||
| 346 | $Product->addProductCategory($ProductCategory); |
||
| 347 | } |
||
| 348 | |||
| 349 | // 画像の登録 |
||
| 350 | $add_images = $form->get('add_images')->getData(); |
||
| 351 | foreach ($add_images as $add_image) { |
||
| 352 | $ProductImage = new \Eccube\Entity\ProductImage(); |
||
| 353 | $ProductImage |
||
| 354 | ->setFileName($add_image) |
||
| 355 | ->setProduct($Product) |
||
| 356 | ->setRank(1); |
||
| 357 | $Product->addProductImage($ProductImage); |
||
| 358 | $app['orm.em']->persist($ProductImage); |
||
| 359 | |||
| 360 | // 移動 |
||
| 361 | $file = new File($app['config']['image_temp_realdir'] . '/' . $add_image); |
||
| 362 | $file->move($app['config']['image_save_realdir']); |
||
| 363 | } |
||
| 364 | |||
| 365 | // 画像の削除 |
||
| 366 | $delete_images = $form->get('delete_images')->getData(); |
||
| 367 | foreach ($delete_images as $delete_image) { |
||
| 368 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 369 | ->findOneBy(array('file_name' => $delete_image)); |
||
| 370 | |||
| 371 | // 追加してすぐに削除した画像は、Entityに追加されない |
||
| 372 | if ($ProductImage instanceof \Eccube\Entity\ProductImage) { |
||
| 373 | $Product->removeProductImage($ProductImage); |
||
| 374 | $app['orm.em']->remove($ProductImage); |
||
| 375 | |||
| 376 | } |
||
| 377 | $app['orm.em']->persist($Product); |
||
| 378 | |||
| 379 | // 削除 |
||
| 380 | $fs = new Filesystem(); |
||
| 381 | $fs->remove($app['config']['image_save_realdir'] . '/' . $delete_image); |
||
| 382 | } |
||
| 383 | $app['orm.em']->persist($Product); |
||
| 384 | $app['orm.em']->flush(); |
||
| 385 | |||
| 386 | |||
| 387 | $ranks = $request->get('rank_images'); |
||
| 388 | if ($ranks) { |
||
| 389 | foreach ($ranks as $rank) { |
||
| 390 | list($filename, $rank_val) = explode('//', $rank); |
||
| 391 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 392 | ->findOneBy(array( |
||
| 393 | 'file_name' => $filename, |
||
| 394 | 'Product' => $Product, |
||
| 395 | )); |
||
| 396 | $ProductImage->setRank($rank_val); |
||
| 397 | $app['orm.em']->persist($ProductImage); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | $app['orm.em']->flush(); |
||
| 401 | |||
| 402 | $event = new EventArgs( |
||
| 403 | array( |
||
| 404 | 'form' => $form, |
||
| 405 | 'Product' => $Product, |
||
| 406 | ), |
||
| 407 | $request |
||
| 408 | ); |
||
| 409 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event); |
||
| 410 | |||
| 411 | $app->addSuccess('admin.register.complete', 'admin'); |
||
| 412 | |||
| 413 | return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId()))); |
||
| 414 | } else { |
||
| 415 | $app->addError('admin.register.failed', 'admin'); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | // 検索結果の保持 |
||
| 420 | $builder = $app['form.factory'] |
||
| 421 | ->createBuilder('admin_search_product'); |
||
| 422 | |||
| 423 | $event = new EventArgs( |
||
| 424 | array( |
||
| 425 | 'builder' => $builder, |
||
| 426 | 'Product' => $Product, |
||
| 427 | ), |
||
| 428 | $request |
||
| 429 | ); |
||
| 430 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event); |
||
| 431 | |||
| 432 | $searchForm = $builder->getForm(); |
||
| 433 | |||
| 434 | if ('POST' === $request->getMethod()) { |
||
| 435 | $searchForm->handleRequest($request); |
||
| 436 | } |
||
| 437 | |||
| 438 | return $app->render('Product/product.twig', array( |
||
| 439 | 'Product' => $Product, |
||
| 440 | 'form' => $form->createView(), |
||
| 441 | 'searchForm' => $searchForm->createView(), |
||
| 442 | 'has_class' => $has_class, |
||
| 443 | 'id' => $id, |
||
| 444 | )); |
||
| 445 | } |
||
| 446 | |||
| 717 |