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 CsvImportController 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 CsvImportController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class CsvImportController extends AbstractCsvImportController |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var DeliveryDurationRepository |
||
| 52 | */ |
||
| 53 | protected $deliveryDurationRepository; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var SaleTypeRepository |
||
| 57 | */ |
||
| 58 | protected $saleTypeRepository; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var TagRepository |
||
| 62 | */ |
||
| 63 | protected $tagRepository; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var CategoryRepository |
||
| 67 | */ |
||
| 68 | protected $categoryRepository; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ClassCategoryRepository |
||
| 72 | */ |
||
| 73 | protected $classCategoryRepository; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ProductStatusRepository |
||
| 77 | */ |
||
| 78 | protected $productStatusRepository; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var ProductRepository |
||
| 82 | */ |
||
| 83 | protected $productRepository; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var BaseInfo |
||
| 87 | */ |
||
| 88 | protected $BaseInfo; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var ValidatorInterface |
||
| 92 | */ |
||
| 93 | protected $validator; |
||
| 94 | |||
| 95 | private $errors = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * CsvImportController constructor. |
||
| 99 | * |
||
| 100 | * @param DeliveryDurationRepository $deliveryDurationRepository |
||
| 101 | 16 | * @param SaleTypeRepository $saleTypeRepository |
|
| 102 | * @param TagRepository $tagRepository |
||
| 103 | 16 | * @param CategoryRepository $categoryRepository |
|
| 104 | 16 | * @param ClassCategoryRepository $classCategoryRepository |
|
| 105 | 16 | * @param ProductStatusRepository $productStatusRepository |
|
| 106 | 16 | * @param ProductRepository $productRepository |
|
| 107 | 16 | * @param BaseInfoRepository $baseInfoRepository |
|
| 108 | 16 | * @param ValidatorInterface $validator |
|
| 109 | 16 | * |
|
| 110 | 16 | * @throws \Doctrine\ORM\NoResultException |
|
| 111 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 112 | */ |
||
| 113 | public function __construct( |
||
| 134 | |||
| 135 | 10 | /** |
|
| 136 | 10 | * 商品登録CSVアップロード |
|
| 137 | 10 | * |
|
| 138 | 10 | * @Route("/%eccube_admin_route%/product/product_csv_upload", name="admin_product_csv_import") |
|
| 139 | 10 | * @Template("@admin/Product/csv_product.twig") |
|
| 140 | 10 | */ |
|
| 141 | public function csvProduct(Request $request, CacheUtil $cacheUtil) |
||
| 142 | 10 | { |
|
| 143 | $form = $this->formFactory->createBuilder(CsvImportType::class)->getForm(); |
||
| 144 | 10 | $headers = $this->getProductCsvHeader(); |
|
| 145 | if ('POST' === $request->getMethod()) { |
||
| 146 | $form->handleRequest($request); |
||
| 147 | if ($form->isValid()) { |
||
| 148 | $formFile = $form['import_file']->getData(); |
||
| 149 | if (!empty($formFile)) { |
||
| 150 | 10 | log_info('商品CSV登録開始'); |
|
| 151 | $data = $this->getImportData($formFile); |
||
| 152 | 10 | View Code Duplication | if ($data === false) { |
| 153 | $this->addErrors(trans('admin.common.csv_invalid_format')); |
||
| 154 | |||
| 155 | return $this->renderWithError($form, $headers, false); |
||
| 156 | } |
||
| 157 | $getId = function ($item) { |
||
| 158 | 10 | return $item['id']; |
|
| 159 | 10 | }; |
|
| 160 | $requireHeader = array_keys(array_map($getId, array_filter($headers, function ($value) { |
||
| 161 | 10 | return $value['required']; |
|
| 162 | 10 | }))); |
|
| 163 | |||
| 164 | 10 | $columnHeaders = $data->getColumnHeaders(); |
|
| 165 | 10 | ||
| 166 | 10 | View Code Duplication | if (count(array_diff($requireHeader, $columnHeaders)) > 0) { |
| 167 | $this->addErrors(trans('admin.common.csv_invalid_format')); |
||
| 168 | |||
| 169 | return $this->renderWithError($form, $headers, false); |
||
| 170 | } |
||
| 171 | |||
| 172 | $size = count($data); |
||
| 173 | 10 | ||
| 174 | 7 | View Code Duplication | if ($size < 1) { |
| 175 | 7 | $this->addErrors(trans('admin.common.csv_invalid_no_data')); |
|
| 176 | |||
| 177 | 4 | return $this->renderWithError($form, $headers, false); |
|
| 178 | 3 | } |
|
| 179 | 3 | ||
| 180 | 1 | $headerSize = count($columnHeaders); |
|
| 181 | 1 | $headerByKey = array_flip(array_map($getId, $headers)); |
|
| 182 | $deleteImages = []; |
||
| 183 | 3 | ||
| 184 | $this->entityManager->getConfiguration()->setSQLLogger(null); |
||
| 185 | $this->entityManager->getConnection()->beginTransaction(); |
||
| 186 | 1 | // CSVファイルの登録処理 |
|
| 187 | 1 | foreach ($data as $row) { |
|
| 188 | $line = $data->key() + 1; |
||
| 189 | 1 | if ($headerSize != count($row)) { |
|
| 190 | $message = trans('admin.common.csv_invalid_format_line', ['%line%' => $line]); |
||
| 191 | $this->addErrors($message); |
||
| 192 | |||
| 193 | 8 | return $this->renderWithError($form, $headers); |
|
| 194 | 1 | } |
|
| 195 | 1 | ||
| 196 | if (!isset($row[$headerByKey['id']]) || StringUtil::isBlank($row[$headerByKey['id']])) { |
||
| 197 | 7 | $Product = new Product(); |
|
| 198 | 6 | $this->entityManager->persist($Product); |
|
| 199 | 6 | } else { |
|
| 200 | 1 | if (preg_match('/^\d+$/', $row[$headerByKey['id']])) { |
|
| 201 | 1 | $Product = $this->productRepository->find($row[$headerByKey['id']]); |
|
| 202 | if (!$Product) { |
||
| 203 | 6 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['id']]); |
|
| 204 | $this->addErrors($message); |
||
| 205 | |||
| 206 | 1 | return $this->renderWithError($form, $headers); |
|
| 207 | 1 | } |
|
| 208 | } else { |
||
| 209 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['id']]); |
||
| 210 | $this->addErrors($message); |
||
| 211 | 8 | ||
| 212 | return $this->renderWithError($form, $headers); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (isset($row[$headerByKey['product_del_flg']])) { |
||
| 216 | if (StringUtil::isNotBlank($row[$headerByKey['product_del_flg']]) && $row[$headerByKey['product_del_flg']] == (string) Constant::ENABLED) { |
||
| 217 | 8 | // 商品を物理削除 |
|
| 218 | $deleteImages[] = $Product->getProductImage(); |
||
| 219 | |||
| 220 | 8 | try { |
|
| 221 | 3 | $this->productRepository->delete($Product); |
|
| 222 | $this->entityManager->flush(); |
||
| 223 | 5 | ||
| 224 | continue; |
||
| 225 | } catch (ForeignKeyConstraintViolationException $e) { |
||
| 226 | 8 | $message = trans('admin.common.csv_invalid_foreign_key', ['%line%' => $line, '%name%' => $Product->getName()]); |
|
| 227 | 4 | $this->addErrors($message); |
|
| 228 | |||
| 229 | 5 | return $this->renderWithError($form, $headers); |
|
| 230 | } |
||
| 231 | } |
||
| 232 | 8 | } |
|
| 233 | 4 | } |
|
| 234 | |||
| 235 | 4 | if (StringUtil::isBlank($row[$headerByKey['status']])) { |
|
| 236 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['status']]); |
||
| 237 | $this->addErrors($message); |
||
| 238 | 8 | } else { |
|
| 239 | 3 | if (preg_match('/^\d+$/', $row[$headerByKey['status']])) { |
|
| 240 | $ProductStatus = $this->productStatusRepository->find($row[$headerByKey['status']]); |
||
| 241 | 5 | if (!$ProductStatus) { |
|
| 242 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['status']]); |
||
| 243 | $this->addErrors($message); |
||
| 244 | 8 | } else { |
|
| 245 | 3 | $Product->setStatus($ProductStatus); |
|
| 246 | } |
||
| 247 | 5 | } else { |
|
| 248 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['status']]); |
||
| 249 | $this->addErrors($message); |
||
| 250 | } |
||
| 251 | 8 | } |
|
| 252 | |||
| 253 | 8 | if (StringUtil::isBlank($row[$headerByKey['name']])) { |
|
| 254 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['name']]); |
||
| 255 | $this->addErrors($message); |
||
| 256 | 8 | ||
| 257 | return $this->renderWithError($form, $headers); |
||
| 258 | } else { |
||
| 259 | 8 | $Product->setName(StringUtil::trimAll($row[$headerByKey['name']])); |
|
| 260 | } |
||
| 261 | |||
| 262 | View Code Duplication | if (isset($row[$headerByKey['note']]) && StringUtil::isNotBlank($row[$headerByKey['note']])) { |
|
| 263 | 8 | $Product->setNote(StringUtil::trimAll($row[$headerByKey['note']])); |
|
| 264 | 8 | } else { |
|
| 265 | $Product->setNote(null); |
||
| 266 | 7 | } |
|
| 267 | 7 | ||
| 268 | View Code Duplication | if (isset($row[$headerByKey['description_list']]) && StringUtil::isNotBlank($row[$headerByKey['description_list']])) { |
|
| 269 | $Product->setDescriptionList(StringUtil::trimAll($row[$headerByKey['description_list']])); |
||
| 270 | } else { |
||
| 271 | $Product->setDescriptionList(null); |
||
| 272 | } |
||
| 273 | |||
| 274 | View Code Duplication | if (isset($row[$headerByKey['description_detail']]) && StringUtil::isNotBlank($row[$headerByKey['description_detail']])) { |
|
| 275 | $Product->setDescriptionDetail(StringUtil::trimAll($row[$headerByKey['description_detail']])); |
||
| 276 | } else { |
||
| 277 | $Product->setDescriptionDetail(null); |
||
| 278 | } |
||
| 279 | 7 | ||
| 280 | 2 | View Code Duplication | if (isset($row[$headerByKey['search_word']]) && StringUtil::isNotBlank($row[$headerByKey['search_word']])) { |
| 281 | $Product->setSearchWord(StringUtil::trimAll($row[$headerByKey['search_word']])); |
||
| 282 | } else { |
||
| 283 | $Product->setSearchWord(null); |
||
| 284 | } |
||
| 285 | |||
| 286 | View Code Duplication | if (isset($row[$headerByKey['free_area']]) && StringUtil::isNotBlank($row[$headerByKey['free_area']])) { |
|
| 287 | $Product->setFreeArea(StringUtil::trimAll($row[$headerByKey['free_area']])); |
||
| 288 | } else { |
||
| 289 | $Product->setFreeArea(null); |
||
| 290 | 2 | } |
|
| 291 | 2 | ||
| 292 | // 商品画像登録 |
||
| 293 | $this->createProductImage($row, $Product, $data, $headerByKey); |
||
| 294 | 2 | ||
| 295 | $this->entityManager->flush(); |
||
| 296 | |||
| 297 | 2 | // 商品カテゴリ登録 |
|
| 298 | 2 | $this->createProductCategory($row, $Product, $data, $headerByKey); |
|
| 299 | 2 | ||
| 300 | 2 | //タグ登録 |
|
| 301 | $this->createProductTag($row, $Product, $data, $headerByKey); |
||
| 302 | |||
| 303 | // 商品規格が存在しなければ新規登録 |
||
| 304 | 2 | /** @var ProductClass[] $ProductClasses */ |
|
| 305 | $ProductClasses = $Product->getProductClasses(); |
||
| 306 | if ($ProductClasses->count() < 1) { |
||
|
|
|||
| 307 | // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成 |
||
| 308 | $ProductClassOrg = $this->createProductClass($row, $Product, $data, $headerByKey); |
||
| 309 | View Code Duplication | if ($this->BaseInfo->isOptionProductDeliveryFee()) { |
|
| 310 | if (isset($row[$headerByKey['delivery_fee']]) && StringUtil::isBlank($row[$headerByKey['delivery_fee']])) { |
||
| 311 | 2 | $deliveryFee = str_replace(',', '', $row[$headerByKey['delivery_fee']]); |
|
| 312 | 2 | $errors = $this->validator->validate($deliveryFee, new GreaterThanOrEqual(['value' => 0])); |
|
| 313 | 2 | if ($errors->count() === 0) { |
|
| 314 | 2 | $ProductClassOrg->setDeliveryFee($deliveryFee); |
|
| 315 | } else { |
||
| 316 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['delivery_fee']]); |
||
| 317 | $this->addErrors($message); |
||
| 318 | 2 | } |
|
| 319 | 2 | } |
|
| 320 | } |
||
| 321 | |||
| 322 | if (isset($row[$headerByKey['class_category1']]) && StringUtil::isNotBlank($row[$headerByKey['class_category1']])) { |
||
| 323 | if (isset($row[$headerByKey['class_category2']]) && $row[$headerByKey['class_category1']] == $row[$headerByKey['class_category2']]) { |
||
| 324 | 2 | $message = trans('admin.common.csv_invalid_not_same', [ |
|
| 325 | '%line%' => $line, |
||
| 326 | '%name1%' => $headerByKey['class_category1'], |
||
| 327 | '%name2%' => $headerByKey['class_category2'], |
||
| 328 | ]); |
||
| 329 | $this->addErrors($message); |
||
| 330 | } else { |
||
| 331 | // 商品規格あり |
||
| 332 | 2 | // 規格分類あり商品を作成 |
|
| 333 | 2 | $ProductClass = clone $ProductClassOrg; |
|
| 334 | $ProductStock = clone $ProductClassOrg->getProductStock(); |
||
| 335 | 2 | ||
| 336 | 2 | // 規格分類1、規格分類2がnullであるデータを非表示 |
|
| 337 | $ProductClassOrg->setVisible(false); |
||
| 338 | |||
| 339 | 5 | // 規格分類1、2をそれぞれセットし作成 |
|
| 340 | $ClassCategory1 = null; |
||
| 341 | 7 | if (preg_match('/^\d+$/', $row[$headerByKey['class_category1']])) { |
|
| 342 | $ClassCategory1 = $this->classCategoryRepository->find($row[$headerByKey['class_category1']]); |
||
| 343 | if (!$ClassCategory1) { |
||
| 344 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category1']]); |
||
| 345 | $this->addErrors($message); |
||
| 346 | 2 | } else { |
|
| 347 | 2 | $ProductClass->setClassCategory1($ClassCategory1); |
|
| 348 | 2 | } |
|
| 349 | } else { |
||
| 350 | 2 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category1']]); |
|
| 351 | 2 | $this->addErrors($message); |
|
| 352 | 2 | } |
|
| 353 | |||
| 354 | if (isset($row[$headerByKey['class_category2']]) && StringUtil::isNotBlank($row[$headerByKey['class_category2']])) { |
||
| 355 | 2 | if (preg_match('/^\d+$/', $row[$headerByKey['class_category2']])) { |
|
| 356 | 2 | $ClassCategory2 = $this->classCategoryRepository->find($row[$headerByKey['class_category2']]); |
|
| 357 | View Code Duplication | if (!$ClassCategory2) { |
|
| 358 | 1 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
|
| 359 | $this->addErrors($message); |
||
| 360 | 1 | } else { |
|
| 361 | if ($ClassCategory1 && |
||
| 362 | ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
||
| 363 | ) { |
||
| 364 | $message = trans('admin.common.csv_invalid_not_same', ['%line%' => $line, '%name1%' => $headerByKey['class_category1'], '%name2%' => $headerByKey['class_category2']]); |
||
| 365 | $this->addErrors($message); |
||
| 366 | } else { |
||
| 367 | $ProductClass->setClassCategory2($ClassCategory2); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | } else { |
||
| 371 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
||
| 372 | 1 | $this->addErrors($message); |
|
| 373 | 2 | } |
|
| 374 | } |
||
| 375 | $ProductClass->setProductStock($ProductStock); |
||
| 376 | $ProductStock->setProductClass($ProductClass); |
||
| 377 | |||
| 378 | 2 | $this->entityManager->persist($ProductClass); |
|
| 379 | 1 | $this->entityManager->persist($ProductStock); |
|
| 380 | 1 | } |
|
| 381 | 1 | } else { |
|
| 382 | if (isset($row[$headerByKey['class_category2']]) && StringUtil::isNotBlank($row[$headerByKey['class_category2']])) { |
||
| 383 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
||
| 384 | 1 | $this->addErrors($message); |
|
| 385 | } |
||
| 386 | } |
||
| 387 | 1 | } else { |
|
| 388 | 1 | // 商品規格の更新 |
|
| 389 | $flag = false; |
||
| 390 | $classCategoryId1 = StringUtil::isBlank($row[$headerByKey['class_category1']]) ? null : $row[$headerByKey['class_category1']]; |
||
| 391 | $classCategoryId2 = StringUtil::isBlank($row[$headerByKey['class_category2']]) ? null : $row[$headerByKey['class_category2']]; |
||
| 392 | |||
| 393 | foreach ($ProductClasses as $pc) { |
||
| 394 | $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId(); |
||
| 395 | $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId(); |
||
| 396 | |||
| 397 | // 登録されている商品規格を更新 |
||
| 398 | 1 | if ($classCategory1 == $classCategoryId1 && |
|
| 399 | 1 | $classCategory2 == $classCategoryId2 |
|
| 400 | 1 | ) { |
|
| 401 | 1 | $this->updateProductClass($row, $Product, $pc, $data, $headerByKey); |
|
| 402 | |||
| 403 | 1 | View Code Duplication | if ($this->BaseInfo->isOptionProductDeliveryFee()) { |
| 404 | $headerByKey['delivery_fee'] = trans('csvimport.label.delivery_fee'); |
||
| 405 | if (isset($row[$headerByKey['delivery_fee']]) && StringUtil::isNotBlank($row[$headerByKey['delivery_fee']])) { |
||
| 406 | $deliveryFee = str_replace(',', '', $row[$headerByKey['delivery_fee']]); |
||
| 407 | $errors = $this->validator->validate($deliveryFee, new GreaterThanOrEqual(['value' => 0])); |
||
| 408 | if ($errors->count() === 0) { |
||
| 409 | $pc->setDeliveryFee($deliveryFee); |
||
| 410 | 1 | } else { |
|
| 411 | 1 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['delivery_fee']]); |
|
| 412 | 1 | $this->addErrors($message); |
|
| 413 | } |
||
| 414 | } |
||
| 415 | } |
||
| 416 | 1 | $flag = true; |
|
| 417 | 1 | break; |
|
| 418 | 1 | } |
|
| 419 | } |
||
| 420 | |||
| 421 | // 商品規格を登録 |
||
| 422 | 1 | if (!$flag) { |
|
| 423 | 1 | $pc = $ProductClasses[0]; |
|
| 424 | if ($pc->getClassCategory1() == null && |
||
| 425 | $pc->getClassCategory2() == null |
||
| 426 | ) { |
||
| 427 | // 規格分類1、規格分類2がnullであるデータを非表示 |
||
| 428 | $pc->setVisible(false); |
||
| 429 | } |
||
| 430 | 1 | ||
| 431 | if (isset($row[$headerByKey['class_category1']]) && isset($row[$headerByKey['class_category2']]) |
||
| 432 | && $row[$headerByKey['class_category1']] == $row[$headerByKey['class_category2']]) { |
||
| 433 | $message = trans('admin.common.csv_invalid_not_same', [ |
||
| 434 | '%line%' => $line, |
||
| 435 | 1 | '%name1%' => $headerByKey['class_category1'], |
|
| 436 | '%name2%' => $headerByKey['class_category2'], |
||
| 437 | ]); |
||
| 438 | $this->addErrors($message); |
||
| 439 | } else { |
||
| 440 | // 必ず規格分類1がセットされている |
||
| 441 | // 規格分類1、2をそれぞれセットし作成 |
||
| 442 | $ClassCategory1 = null; |
||
| 443 | if (preg_match('/^\d+$/', $classCategoryId1)) { |
||
| 444 | 1 | $ClassCategory1 = $this->classCategoryRepository->find($classCategoryId1); |
|
| 445 | if (!$ClassCategory1) { |
||
| 446 | 1 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category1']]); |
|
| 447 | $this->addErrors($message); |
||
| 448 | } |
||
| 449 | } else { |
||
| 450 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category1']]); |
||
| 451 | $this->addErrors($message); |
||
| 452 | } |
||
| 453 | |||
| 454 | $ClassCategory2 = null; |
||
| 455 | if (isset($row[$headerByKey['class_category2']]) && StringUtil::isNotBlank($row[$headerByKey['class_category2']])) { |
||
| 456 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) { |
||
| 457 | 1 | $message = trans('admin.common.csv_invalid_can_not', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
|
| 458 | $this->addErrors($message); |
||
| 459 | } else { |
||
| 460 | if (preg_match('/^\d+$/', $classCategoryId2)) { |
||
| 461 | 8 | $ClassCategory2 = $this->classCategoryRepository->find($classCategoryId2); |
|
| 462 | 3 | View Code Duplication | if (!$ClassCategory2) { |
| 463 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
||
| 464 | 5 | $this->addErrors($message); |
|
| 465 | } else { |
||
| 466 | 5 | if ($ClassCategory1 && |
|
| 467 | 5 | ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
|
| 468 | 5 | ) { |
|
| 469 | 5 | $message = trans('admin.common.csv_invalid_not_same', [ |
|
| 470 | 5 | '%line%' => $line, |
|
| 471 | '%name1%' => $headerByKey['class_category1'], |
||
| 472 | '%name2%' => $headerByKey['class_category2'], |
||
| 473 | ]); |
||
| 474 | $this->addErrors($message); |
||
| 475 | 5 | } |
|
| 476 | } |
||
| 477 | } else { |
||
| 478 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
||
| 479 | $this->addErrors($message); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | } else { |
||
| 483 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) { |
||
| 484 | 6 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
|
| 485 | $this->addErrors($message); |
||
| 486 | 6 | } |
|
| 487 | } |
||
| 488 | 6 | $ProductClass = $this->createProductClass($row, $Product, $data, $headerByKey, $ClassCategory1, $ClassCategory2); |
|
| 489 | 6 | ||
| 490 | 6 | View Code Duplication | if ($this->BaseInfo->isOptionProductDeliveryFee()) { |
| 491 | 6 | if (isset($row[$headerByKey['delivery_fee']]) && StringUtil::isNotBlank($row[$headerByKey['delivery_fee']])) { |
|
| 492 | 6 | $deliveryFee = str_replace(',', '', $row[$headerByKey['delivery_fee']]); |
|
| 493 | 6 | $errors = $this->validator->validate($deliveryFee, new GreaterThanOrEqual(['value' => 0])); |
|
| 494 | 6 | if ($errors->count() === 0) { |
|
| 495 | 6 | $ProductClass->setDeliveryFee($deliveryFee); |
|
| 496 | 6 | } else { |
|
| 497 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['delivery_fee']]); |
||
| 498 | $this->addErrors($message); |
||
| 499 | } |
||
| 500 | } |
||
| 501 | } |
||
| 502 | $Product->addProductClass($ProductClass); |
||
| 503 | } |
||
| 504 | } |
||
| 505 | 6 | } |
|
| 506 | if ($this->hasErrors()) { |
||
| 507 | 6 | return $this->renderWithError($form, $headers); |
|
| 508 | 6 | } |
|
| 509 | 1 | $this->entityManager->persist($Product); |
|
| 510 | } |
||
| 511 | 1 | $this->entityManager->flush(); |
|
| 512 | $this->entityManager->getConnection()->commit(); |
||
| 513 | |||
| 514 | 5 | // 画像ファイルの削除(commit後に削除させる) |
|
| 515 | 5 | View Code Duplication | foreach ($deleteImages as $images) { |
| 516 | foreach ($images as $image) { |
||
| 517 | try { |
||
| 518 | $fs = new Filesystem(); |
||
| 519 | $fs->remove($this->eccubeConfig['eccube_save_image_dir'].'/'.$image); |
||
| 520 | 5 | } catch (\Exception $e) { |
|
| 521 | 5 | // エラーが発生しても無視する |
|
| 522 | } |
||
| 523 | 5 | } |
|
| 524 | } |
||
| 525 | 5 | ||
| 526 | 5 | log_info('商品CSV登録完了'); |
|
| 527 | 1 | $message = 'admin.common.csv_upload_complete'; |
|
| 528 | $this->session->getFlashBag()->add('eccube.admin.success', $message); |
||
| 529 | |||
| 530 | $cacheUtil->clearDoctrineCache(); |
||
| 531 | } |
||
| 532 | 1 | } |
|
| 533 | 1 | } |
|
| 534 | |||
| 535 | return $this->renderWithError($form, $headers); |
||
| 536 | } |
||
| 537 | |||
| 538 | 1 | /** |
|
| 539 | * カテゴリ登録CSVアップロード |
||
| 540 | * |
||
| 541 | * @Route("/%eccube_admin_route%/product/category_csv_upload", name="admin_product_category_csv_import") |
||
| 542 | * @Template("@admin/Product/csv_category.twig") |
||
| 543 | */ |
||
| 544 | public function csvCategory(Request $request, CacheUtil $cacheUtil) |
||
| 697 | |||
| 698 | 4 | /** |
|
| 699 | * アップロード用CSV雛形ファイルダウンロード |
||
| 700 | 4 | * |
|
| 701 | * @Route("/%eccube_admin_route%/product/csv_template/{type}", requirements={"type" = "\w+"}, name="admin_product_csv_template") |
||
| 702 | 4 | */ |
|
| 703 | 4 | public function csvTemplate(Request $request, $type) |
|
| 717 | |||
| 718 | 4 | /** |
|
| 719 | 4 | * 登録、更新時のエラー画面表示 |
|
| 720 | 4 | * |
|
| 721 | * @param FormInterface $form |
||
| 722 | * @param array $headers |
||
| 723 | * @param bool $rollback |
||
| 724 | * |
||
| 725 | * @return array |
||
| 726 | * |
||
| 727 | * @throws \Doctrine\DBAL\ConnectionException |
||
| 728 | */ |
||
| 729 | protected function renderWithError($form, $headers, $rollback = true) |
||
| 745 | 8 | ||
| 746 | /** |
||
| 747 | 4 | * 商品画像の削除、登録 |
|
| 748 | 4 | * |
|
| 749 | 4 | * @param $row |
|
| 750 | 4 | * @param Product $Product |
|
| 751 | 4 | * @param CsvImportService $data |
|
| 752 | 4 | */ |
|
| 753 | 4 | protected function createProductImage($row, Product $Product, $data, $headerByKey) |
|
| 792 | |||
| 793 | /** |
||
| 794 | 4 | * 商品カテゴリの削除、登録 |
|
| 795 | * |
||
| 796 | * @param $row |
||
| 797 | * @param Product $Product |
||
| 798 | * @param CsvImportService $data |
||
| 799 | * @param $headerByKey |
||
| 800 | */ |
||
| 801 | protected function createProductCategory($row, Product $Product, $data, $headerByKey) |
||
| 857 | |||
| 858 | /** |
||
| 859 | 8 | * タグの登録 |
|
| 860 | * |
||
| 861 | * @param array $row |
||
| 862 | 8 | * @param Product $Product |
|
| 863 | 8 | * @param CsvImportService $data |
|
| 864 | 8 | */ |
|
| 865 | protected function createProductTag($row, Product $Product, $data, $headerByKey) |
||
| 904 | 3 | ||
| 905 | /** |
||
| 906 | 5 | * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成 |
|
| 907 | * |
||
| 908 | * @param $row |
||
| 909 | 8 | * @param Product $Product |
|
| 910 | * @param CsvImportService $data |
||
| 911 | * @param $headerByKey |
||
| 912 | * @param null $ClassCategory1 |
||
| 913 | 8 | * @param null $ClassCategory2 |
|
| 914 | 4 | * |
|
| 915 | * @return ProductClass |
||
| 916 | 4 | */ |
|
| 917 | 4 | protected function createProductClass($row, Product $Product, $data, $headerByKey, $ClassCategory1 = null, $ClassCategory2 = null) |
|
| 1056 | |||
| 1057 | /** |
||
| 1058 | * 商品規格情報を更新 |
||
| 1059 | * |
||
| 1060 | * @param $row |
||
| 1061 | * @param Product $Product |
||
| 1062 | 1 | * @param ProductClass $ProductClass |
|
| 1063 | * @param CsvImportService $data |
||
| 1064 | * |
||
| 1065 | * @return ProductClass |
||
| 1066 | */ |
||
| 1067 | protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $data, $headerByKey) |
||
| 1214 | 10 | ||
| 1215 | /** |
||
| 1216 | * 登録、更新時のエラー画面表示 |
||
| 1217 | */ |
||
| 1218 | protected function addErrors($message) |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | 10 | * @return array |
|
| 1225 | */ |
||
| 1226 | protected function getErrors() |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * @return boolean |
||
| 1233 | */ |
||
| 1234 | 10 | protected function hasErrors() |
|
| 1238 | |||
| 1239 | 10 | /** |
|
| 1240 | * 商品登録CSVヘッダー定義 |
||
| 1241 | * |
||
| 1242 | * @return array |
||
| 1243 | */ |
||
| 1244 | 10 | private function getProductCsvHeader() |
|
| 1364 | |||
| 1365 | /** |
||
| 1366 | * カテゴリCSVヘッダー定義 |
||
| 1367 | */ |
||
| 1368 | private function getCategoryCsvHeader() |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * ProductCategory作成 |
||
| 1396 | * |
||
| 1397 | * @param \Eccube\Entity\Product $Product |
||
| 1398 | * @param \Eccube\Entity\Category $Category |
||
| 1399 | * @param int $sortNo |
||
| 1400 | * |
||
| 1401 | * @return ProductCategory |
||
| 1402 | */ |
||
| 1403 | View Code Duplication | private function makeProductCategory($Product, $Category, $sortNo) |
|
| 1413 | } |
||
| 1414 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.