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