| Conditions | 70 |
| Paths | > 20000 |
| Total Lines | 347 |
| Code Lines | 206 |
| Lines | 51 |
| Ratio | 14.7 % |
| Changes | 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 |
||
| 61 | public function csvProduct(Application $app, Request $request) |
||
| 62 | { |
||
| 63 | $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm(); |
||
| 64 | |||
| 65 | $headers = $this->getProductCsvHeader(); |
||
| 66 | |||
| 67 | if ('POST' === $request->getMethod()) { |
||
| 68 | |||
| 69 | $form->handleRequest($request); |
||
| 70 | |||
| 71 | if ($form->isValid()) { |
||
| 72 | |||
| 73 | $formFile = $form['import_file']->getData(); |
||
| 74 | |||
| 75 | if (!empty($formFile)) { |
||
| 76 | |||
| 77 | log_info('商品CSV登録開始'); |
||
| 78 | |||
| 79 | $data = $this->getImportData($app, $formFile); |
||
| 80 | if ($data === false) { |
||
| 81 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
| 82 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 83 | } |
||
| 84 | |||
| 85 | $keys = array_keys($headers); |
||
| 86 | $columnHeaders = $data->getColumnHeaders(); |
||
| 87 | if ($keys !== $columnHeaders) { |
||
| 88 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
| 89 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 90 | } |
||
| 91 | |||
| 92 | $size = count($data); |
||
| 93 | if ($size < 1) { |
||
| 94 | $this->addErrors('CSVデータが存在しません。'); |
||
| 95 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 96 | } |
||
| 97 | |||
| 98 | $headerSize = count($keys); |
||
| 99 | |||
| 100 | $this->em = $app['orm.em']; |
||
| 101 | $this->em->getConfiguration()->setSQLLogger(null); |
||
| 102 | |||
| 103 | $this->em->getConnection()->beginTransaction(); |
||
| 104 | |||
| 105 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 106 | |||
| 107 | // CSVファイルの登録処理 |
||
| 108 | foreach ($data as $row) { |
||
| 109 | |||
| 110 | if ($headerSize != count($row)) { |
||
| 111 | $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
||
| 112 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($row['商品ID'] == '') { |
||
| 116 | $Product = new Product(); |
||
| 117 | $this->em->persist($Product); |
||
| 118 | } else { |
||
| 119 | if (preg_match('/^\d+$/', $row['商品ID'])) { |
||
| 120 | $Product = $app['eccube.repository.product']->find($row['商品ID']); |
||
| 121 | if (!$Product) { |
||
| 122 | $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
||
| 123 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 124 | } |
||
| 125 | } else { |
||
| 126 | $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
||
| 127 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 128 | } |
||
| 129 | |||
| 130 | } |
||
| 131 | |||
| 132 | if ($row['公開ステータス(ID)'] == '') { |
||
| 133 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。'); |
||
| 134 | } else { |
||
| 135 | if (preg_match('/^\d+$/', $row['公開ステータス(ID)'])) { |
||
| 136 | $Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']); |
||
| 137 | if (!$Disp) { |
||
| 138 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
||
| 139 | } else { |
||
| 140 | $Product->setStatus($Disp); |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | View Code Duplication | if (Str::isBlank($row['商品名'])) { |
|
| 148 | $this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。'); |
||
| 149 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 150 | } else { |
||
| 151 | $Product->setName(Str::trimAll($row['商品名'])); |
||
| 152 | } |
||
| 153 | |||
| 154 | View Code Duplication | if (Str::isNotBlank($row['ショップ用メモ欄'])) { |
|
| 155 | $Product->setNote(Str::trimAll($row['ショップ用メモ欄'])); |
||
| 156 | } else { |
||
| 157 | $Product->setNote(null); |
||
| 158 | } |
||
| 159 | |||
| 160 | View Code Duplication | if (Str::isNotBlank($row['商品説明(一覧)'])) { |
|
| 161 | $Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)'])); |
||
| 162 | } else { |
||
| 163 | $Product->setDescriptionList(null); |
||
| 164 | } |
||
| 165 | |||
| 166 | View Code Duplication | if (Str::isNotBlank($row['商品説明(詳細)'])) { |
|
| 167 | $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)'])); |
||
| 168 | } else { |
||
| 169 | $Product->setDescriptionDetail(null); |
||
| 170 | } |
||
| 171 | |||
| 172 | View Code Duplication | if (Str::isNotBlank($row['検索ワード'])) { |
|
| 173 | $Product->setSearchWord(Str::trimAll($row['検索ワード'])); |
||
| 174 | } else { |
||
| 175 | $Product->setSearchWord(null); |
||
| 176 | } |
||
| 177 | |||
| 178 | View Code Duplication | if (Str::isNotBlank($row['フリーエリア'])) { |
|
| 179 | $Product->setFreeArea(Str::trimAll($row['フリーエリア'])); |
||
| 180 | } else { |
||
| 181 | $Product->setFreeArea(null); |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($row['商品削除フラグ'] == '') { |
||
| 185 | $Product->setDelFlg(Constant::DISABLED); |
||
| 186 | } else { |
||
| 187 | if ($row['商品削除フラグ'] == (string)Constant::DISABLED || $row['商品削除フラグ'] == (string)Constant::ENABLED) { |
||
| 188 | $Product->setDelFlg($row['商品削除フラグ']); |
||
| 189 | } else { |
||
| 190 | $this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。'); |
||
| 191 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // 商品画像登録 |
||
| 196 | $this->createProductImage($row, $Product, $data); |
||
| 197 | |||
| 198 | $this->em->flush($Product); |
||
| 199 | |||
| 200 | // 商品カテゴリ登録 |
||
| 201 | $this->createProductCategory($row, $Product, $app, $data); |
||
| 202 | |||
| 203 | //タグ登録 |
||
| 204 | $this->createProductTag($row, $Product, $app, $data); |
||
| 205 | |||
| 206 | // 商品規格が存在しなければ新規登録 |
||
| 207 | $ProductClasses = $Product->getProductClasses(); |
||
| 208 | if ($ProductClasses->count() < 1) { |
||
| 209 | // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成 |
||
| 210 | $ProductClassOrg = $this->createProductClass($row, $Product, $app, $data); |
||
| 211 | if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
||
| 212 | if ($row['送料'] != '') { |
||
| 213 | $deliveryFee = str_replace(',', '', $row['送料']); |
||
| 214 | if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) { |
||
| 215 | $ProductClassOrg->setDeliveryFee($deliveryFee); |
||
| 216 | } else { |
||
| 217 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | if ($row['規格分類1(ID)'] != '') { |
||
| 223 | |||
| 224 | if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
||
| 225 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
||
| 226 | } else { |
||
| 227 | // 商品規格あり |
||
| 228 | // 企画分類あり商品を作成 |
||
| 229 | $ProductClass = clone $ProductClassOrg; |
||
| 230 | $ProductStock = clone $ProductClassOrg->getProductStock(); |
||
| 231 | |||
| 232 | // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット |
||
| 233 | $ProductClassOrg->setDelFlg(Constant::ENABLED); |
||
| 234 | |||
| 235 | // 規格分類1、2をそれぞれセットし作成 |
||
| 236 | $ClassCategory1 = null; |
||
| 237 | if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) { |
||
| 238 | $ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']); |
||
| 239 | if (!$ClassCategory1) { |
||
| 240 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 241 | } else { |
||
| 242 | $ProductClass->setClassCategory1($ClassCategory1); |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 246 | } |
||
| 247 | |||
| 248 | if ($row['規格分類2(ID)'] != '') { |
||
| 249 | if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) { |
||
| 250 | $ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']); |
||
| 251 | View Code Duplication | if (!$ClassCategory2) { |
|
| 252 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 253 | } else { |
||
| 254 | if ($ClassCategory1 && |
||
| 255 | ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
||
| 256 | ) { |
||
| 257 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。'); |
||
| 258 | } else { |
||
| 259 | $ProductClass->setClassCategory2($ClassCategory2); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | $ProductClass->setProductStock($ProductStock); |
||
| 267 | $ProductStock->setProductClass($ProductClass); |
||
| 268 | |||
| 269 | $this->em->persist($ProductClass); |
||
| 270 | $this->em->persist($ProductStock); |
||
| 271 | } |
||
| 272 | |||
| 273 | } else { |
||
| 274 | if ($row['規格分類2(ID)'] != '') { |
||
| 275 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | } else { |
||
| 280 | // 商品規格の更新 |
||
| 281 | |||
| 282 | $flag = false; |
||
| 283 | $classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)']; |
||
| 284 | $classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)']; |
||
| 285 | |||
| 286 | foreach ($ProductClasses as $pc) { |
||
| 287 | |||
| 288 | $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId(); |
||
| 289 | $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId(); |
||
| 290 | |||
| 291 | // 登録されている商品規格を更新 |
||
| 292 | if ($classCategory1 == $classCategoryId1 && |
||
| 293 | $classCategory2 == $classCategoryId2 |
||
| 294 | ) { |
||
| 295 | $this->updateProductClass($row, $Product, $pc, $app, $data); |
||
| 296 | |||
| 297 | if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
||
| 298 | if ($row['送料'] != '') { |
||
| 299 | $deliveryFee = str_replace(',', '', $row['送料']); |
||
| 300 | if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) { |
||
| 301 | $pc->setDeliveryFee($deliveryFee); |
||
| 302 | } else { |
||
| 303 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $flag = true; |
||
| 309 | break; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | // 商品規格を登録 |
||
| 314 | if (!$flag) { |
||
| 315 | $pc = $ProductClasses[0]; |
||
| 316 | if ($pc->getClassCategory1() == null && |
||
| 317 | $pc->getClassCategory2() == null |
||
| 318 | ) { |
||
| 319 | |||
| 320 | // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット |
||
| 321 | $pc->setDelFlg(Constant::ENABLED); |
||
| 322 | } |
||
| 323 | |||
| 324 | if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
||
| 325 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
||
| 326 | } else { |
||
| 327 | |||
| 328 | // 必ず規格分類1がセットされている |
||
| 329 | // 規格分類1、2をそれぞれセットし作成 |
||
| 330 | $ClassCategory1 = null; |
||
| 331 | if (preg_match('/^\d+$/', $classCategoryId1)) { |
||
| 332 | $ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1); |
||
| 333 | if (!$ClassCategory1) { |
||
| 334 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 335 | } |
||
| 336 | } else { |
||
| 337 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 338 | } |
||
| 339 | |||
| 340 | $ClassCategory2 = null; |
||
| 341 | if ($row['規格分類2(ID)'] != '') { |
||
| 342 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) { |
||
| 343 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)は設定できません。'); |
||
| 344 | } else { |
||
| 345 | if (preg_match('/^\d+$/', $classCategoryId2)) { |
||
| 346 | $ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2); |
||
| 347 | View Code Duplication | if (!$ClassCategory2) { |
|
| 348 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 349 | } else { |
||
| 350 | if ($ClassCategory1 && |
||
| 351 | ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
||
| 352 | ) { |
||
| 353 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。'); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } else { |
||
| 357 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 358 | } |
||
| 359 | |||
| 360 | } |
||
| 361 | } else { |
||
| 362 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) { |
||
| 363 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)に値を設定してください。'); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | $ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2); |
||
| 367 | |||
| 368 | if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
||
| 369 | if ($row['送料'] != '') { |
||
| 370 | $deliveryFee = str_replace(',', '', $row['送料']); |
||
| 371 | if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) { |
||
| 372 | $ProductClass->setDeliveryFee($deliveryFee); |
||
| 373 | } else { |
||
| 374 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | $Product->addProductClass($ProductClass); |
||
| 380 | } |
||
| 381 | |||
| 382 | } |
||
| 383 | |||
| 384 | } |
||
| 385 | |||
| 386 | |||
| 387 | if ($this->hasErrors()) { |
||
| 388 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 389 | } |
||
| 390 | |||
| 391 | $this->em->persist($Product); |
||
| 392 | |||
| 393 | } |
||
| 394 | |||
| 395 | $this->em->flush(); |
||
| 396 | $this->em->getConnection()->commit(); |
||
| 397 | |||
| 398 | log_info('商品CSV登録完了'); |
||
| 399 | |||
| 400 | $app->addSuccess('admin.product.csv_import.save.complete', 'admin'); |
||
| 401 | } |
||
| 402 | |||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 407 | } |
||
| 408 | |||
| 1187 |