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 |
||
| 55 | class CsvImportController extends AbstractCsvImportController |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * @var DeliveryDurationRepository |
||
| 59 | */ |
||
| 60 | protected $deliveryDurationRepository; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var SaleTypeRepository |
||
| 64 | */ |
||
| 65 | protected $saleTypeRepository; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var TagRepository |
||
| 69 | */ |
||
| 70 | protected $tagRepository; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var CategoryRepository |
||
| 74 | */ |
||
| 75 | protected $categoryRepository; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var ClassCategoryRepository |
||
| 79 | */ |
||
| 80 | protected $classCategoryRepository; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ProductImageRepository |
||
| 84 | */ |
||
| 85 | protected $productImageRepository; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var ProductStatusRepository |
||
| 89 | */ |
||
| 90 | protected $productStatusRepository; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var ProductRepository |
||
| 94 | */ |
||
| 95 | protected $productRepository; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var TaxRuleRepository |
||
| 99 | */ |
||
| 100 | private $taxRuleRepository; |
||
| 101 | 16 | ||
| 102 | /** |
||
| 103 | 16 | * @var BaseInfo |
|
| 104 | 16 | */ |
|
| 105 | 16 | protected $BaseInfo; |
|
| 106 | 16 | ||
| 107 | 16 | /** |
|
| 108 | 16 | * @var ValidatorInterface |
|
| 109 | 16 | */ |
|
| 110 | 16 | protected $validator; |
|
| 111 | |||
| 112 | private $errors = []; |
||
| 113 | |||
| 114 | protected $isXmlHttpRequest = false; |
||
| 115 | |||
| 116 | protected $csvFileNo = 1; |
||
| 117 | |||
| 118 | protected $currentLineNo = 1; |
||
| 119 | 10 | ||
| 120 | /** |
||
| 121 | 10 | * CsvImportController constructor. |
|
| 122 | 10 | * |
|
| 123 | 10 | * @param DeliveryDurationRepository $deliveryDurationRepository |
|
| 124 | 10 | * @param SaleTypeRepository $saleTypeRepository |
|
| 125 | 10 | * @param TagRepository $tagRepository |
|
| 126 | 10 | * @param CategoryRepository $categoryRepository |
|
| 127 | 10 | * @param ClassCategoryRepository $classCategoryRepository |
|
| 128 | 10 | * @param ProductImageRepository $productImageRepository |
|
| 129 | 10 | * @param ProductStatusRepository $productStatusRepository |
|
| 130 | 10 | * @param ProductRepository $productRepository |
|
| 131 | * @param TaxRuleRepository $taxRuleRepository |
||
| 132 | * @param BaseInfoRepository $baseInfoRepository |
||
| 133 | * @param ValidatorInterface $validator |
||
| 134 | * @throws \Exception |
||
| 135 | 10 | */ |
|
| 136 | 10 | View Code Duplication | public function __construct( |
| 161 | 10 | ||
| 162 | 10 | /** |
|
| 163 | * 商品登録CSVアップロード |
||
| 164 | 10 | * |
|
| 165 | 10 | * @Route("/%eccube_admin_route%/product/product_csv_upload", name="admin_product_csv_import") |
|
| 166 | 10 | * @Template("@admin/Product/csv_product.twig") |
|
| 167 | * |
||
| 168 | * @return array |
||
| 169 | * |
||
| 170 | * @throws \Doctrine\DBAL\ConnectionException |
||
| 171 | * @throws \Doctrine\ORM\NoResultException |
||
| 172 | */ |
||
| 173 | 10 | public function csvProduct(Request $request, CacheUtil $cacheUtil) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * カテゴリ登録CSVアップロード |
||
| 685 | * |
||
| 686 | * @Route("/%eccube_admin_route%/product/category_csv_upload", name="admin_product_category_csv_import") |
||
| 687 | 8 | * @Template("@admin/Product/csv_category.twig") |
|
| 688 | */ |
||
| 689 | 8 | public function csvCategory(Request $request, CacheUtil $cacheUtil) |
|
| 842 | |||
| 843 | /** |
||
| 844 | * アップロード用CSV雛形ファイルダウンロード |
||
| 845 | * |
||
| 846 | * @Route("/%eccube_admin_route%/product/csv_template/{type}", requirements={"type" = "\w+"}, name="admin_product_csv_template") |
||
| 847 | * |
||
| 848 | * @param $type |
||
| 849 | * |
||
| 850 | * @return StreamedResponse |
||
| 851 | */ |
||
| 852 | public function csvTemplate(Request $request, $type) |
||
| 866 | 8 | ||
| 867 | 8 | /** |
|
| 868 | 8 | * 登録、更新時のエラー画面表示 |
|
| 869 | 8 | * |
|
| 870 | 8 | * @param FormInterface $form |
|
| 871 | * @param array $headers |
||
| 872 | * @param bool $rollback |
||
| 873 | * |
||
| 874 | 8 | * @return array |
|
| 875 | * |
||
| 876 | * @throws \Doctrine\DBAL\ConnectionException |
||
| 877 | */ |
||
| 878 | 8 | protected function renderWithError($form, $headers, $rollback = true) |
|
| 879 | { |
||
| 880 | if ($this->hasErrors()) { |
||
| 881 | if ($rollback) { |
||
| 882 | $this->entityManager->getConnection()->rollback(); |
||
| 883 | } |
||
| 884 | } |
||
| 885 | 8 | ||
| 886 | 8 | $this->removeUploadedFile(); |
|
| 887 | |||
| 888 | 8 | if ($this->isXmlHttpRequest) { |
|
| 889 | 3 | return $this->json([ |
|
| 890 | 3 | 'success' => !$this->hasErrors(), |
|
| 891 | 3 | 'success_message' => trans('admin.common.csv_upload_line_success', [ |
|
| 892 | '%from%' => $this->convertLineNo(2), |
||
| 893 | '%to%' => $this->currentLineNo]), |
||
| 894 | 'errors' => $this->errors, |
||
| 895 | 3 | 'error_message' => trans('admin.common.csv_upload_line_error',[ |
|
| 896 | '%from%' => $this->convertLineNo(2)]) |
||
| 897 | ]); |
||
| 898 | } |
||
| 899 | |||
| 900 | return [ |
||
| 901 | 'form' => $form->createView(), |
||
| 902 | 'headers' => $headers, |
||
| 903 | 8 | 'errors' => $this->errors, |
|
| 904 | 3 | ]; |
|
| 905 | } |
||
| 906 | 5 | ||
| 907 | /** |
||
| 908 | * 商品画像の削除、登録 |
||
| 909 | 8 | * |
|
| 910 | * @param $row |
||
| 911 | * @param Product $Product |
||
| 912 | * @param CsvImportService $data |
||
| 913 | 8 | * @param $headerByKey |
|
| 914 | 4 | */ |
|
| 915 | protected function createProductImage($row, Product $Product, $data, $headerByKey) |
||
| 916 | 4 | { |
|
| 917 | 4 | if (!isset($row[$headerByKey['product_image']])) { |
|
| 918 | 4 | return; |
|
| 919 | 4 | } |
|
| 920 | if (StringUtil::isNotBlank($row[$headerByKey['product_image']])) { |
||
| 921 | // 画像の削除 |
||
| 922 | 4 | $ProductImages = $Product->getProductImage(); |
|
| 923 | foreach ($ProductImages as $ProductImage) { |
||
| 924 | $Product->removeProductImage($ProductImage); |
||
| 925 | $this->entityManager->remove($ProductImage); |
||
| 926 | 4 | } |
|
| 927 | |||
| 928 | 4 | // 画像の登録 |
|
| 929 | 4 | $images = explode(',', $row[$headerByKey['product_image']]); |
|
| 930 | 4 | ||
| 931 | $sortNo = 1; |
||
| 932 | |||
| 933 | $pattern = "/\\$|^.*.\.\\\.*|\/$|^.*.\.\/\.*/"; |
||
| 934 | foreach ($images as $image) { |
||
| 935 | $fileName = StringUtil::trimAll($image); |
||
| 936 | |||
| 937 | 8 | // 商品画像名のフォーマットチェック |
|
| 938 | if (strlen($fileName) > 0 && preg_match($pattern, $fileName)) { |
||
| 939 | $message = trans('admin.common.csv_invalid_image', ['%line%' => $data->key() + 1, '%name%' => $headerByKey['product_image']]); |
||
| 940 | $this->addErrors($message); |
||
| 941 | } else { |
||
| 942 | // 空文字は登録対象外 |
||
| 943 | if (!empty($fileName)) { |
||
| 944 | $ProductImage = new ProductImage(); |
||
| 945 | $ProductImage->setFileName($fileName); |
||
| 946 | $ProductImage->setProduct($Product); |
||
| 947 | 8 | $ProductImage->setSortNo($sortNo); |
|
| 948 | 3 | ||
| 949 | 3 | $Product->addProductImage($ProductImage); |
|
| 950 | 3 | $sortNo++; |
|
| 951 | $this->entityManager->persist($ProductImage); |
||
| 952 | } |
||
| 953 | } |
||
| 954 | } |
||
| 955 | } |
||
| 956 | } |
||
| 957 | 8 | ||
| 958 | 8 | /** |
|
| 959 | 8 | * 商品カテゴリの削除、登録 |
|
| 960 | 8 | * |
|
| 961 | * @param $row |
||
| 962 | * @param Product $Product |
||
| 963 | 8 | * @param CsvImportService $data |
|
| 964 | * @param $headerByKey |
||
| 965 | */ |
||
| 966 | protected function createProductCategory($row, Product $Product, $data, $headerByKey) |
||
| 967 | { |
||
| 968 | if (!isset($row[$headerByKey['product_category']])) { |
||
| 969 | return; |
||
| 970 | 8 | } |
|
| 971 | 3 | // カテゴリの削除 |
|
| 972 | 3 | $ProductCategories = $Product->getProductCategories(); |
|
| 973 | 3 | foreach ($ProductCategories as $ProductCategory) { |
|
| 974 | $Product->removeProductCategory($ProductCategory); |
||
| 975 | $this->entityManager->remove($ProductCategory); |
||
| 976 | $this->entityManager->flush(); |
||
| 977 | } |
||
| 978 | |||
| 979 | if (StringUtil::isNotBlank($row[$headerByKey['product_category']])) { |
||
| 980 | 8 | // カテゴリの登録 |
|
| 981 | 8 | $categories = explode(',', $row[$headerByKey['product_category']]); |
|
| 982 | 8 | $sortNo = 1; |
|
| 983 | 8 | $categoriesIdList = []; |
|
| 984 | foreach ($categories as $category) { |
||
| 985 | 8 | $line = $data->key() + 1; |
|
| 986 | 4 | if (preg_match('/^\d+$/', $category)) { |
|
| 987 | $Category = $this->categoryRepository->find($category); |
||
| 988 | if (!$Category) { |
||
| 989 | 4 | $message = trans('admin.common.csv_invalid_not_found_target', [ |
|
| 990 | '%line%' => $line, |
||
| 991 | '%name%' => $headerByKey['product_category'], |
||
| 992 | 8 | '%target_name%' => $category, |
|
| 993 | 8 | ]); |
|
| 994 | $this->addErrors($message); |
||
| 995 | 8 | } else { |
|
| 996 | View Code Duplication | foreach ($Category->getPath() as $ParentCategory) { |
|
| 997 | if (!isset($categoriesIdList[$ParentCategory->getId()])) { |
||
| 998 | $ProductCategory = $this->makeProductCategory($Product, $ParentCategory, $sortNo); |
||
| 999 | $this->entityManager->persist($ProductCategory); |
||
| 1000 | $sortNo++; |
||
| 1001 | |||
| 1002 | $Product->addProductCategory($ProductCategory); |
||
| 1003 | $categoriesIdList[$ParentCategory->getId()] = true; |
||
| 1004 | } |
||
| 1005 | } |
||
| 1006 | if (!isset($categoriesIdList[$Category->getId()])) { |
||
| 1007 | $ProductCategory = $this->makeProductCategory($Product, $Category, $sortNo); |
||
| 1008 | 1 | $sortNo++; |
|
| 1009 | $this->entityManager->persist($ProductCategory); |
||
| 1010 | 1 | $Product->addProductCategory($ProductCategory); |
|
| 1011 | $categoriesIdList[$Category->getId()] = true; |
||
| 1012 | 1 | } |
|
| 1013 | 1 | } |
|
| 1014 | View Code Duplication | } else { |
|
| 1015 | $message = trans('admin.common.csv_invalid_not_found_target', [ |
||
| 1016 | '%line%' => $line, |
||
| 1017 | 1 | '%name%' => $headerByKey['product_category'], |
|
| 1018 | 1 | '%target_name%' => $category, |
|
| 1019 | 1 | ]); |
|
| 1020 | $this->addErrors($message); |
||
| 1021 | } |
||
| 1022 | } |
||
| 1023 | 1 | } |
|
| 1024 | } |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * タグの登録 |
||
| 1028 | * |
||
| 1029 | * @param array $row |
||
| 1030 | * @param Product $Product |
||
| 1031 | * @param CsvImportService $data |
||
| 1032 | 1 | */ |
|
| 1033 | 1 | protected function createProductTag($row, Product $Product, $data, $headerByKey) |
|
| 1034 | 1 | { |
|
| 1035 | 1 | if (!isset($row[$headerByKey['product_tag']])) { |
|
| 1036 | return; |
||
| 1037 | } |
||
| 1038 | // タグの削除 |
||
| 1039 | 1 | $ProductTags = $Product->getProductTag(); |
|
| 1040 | foreach ($ProductTags as $ProductTag) { |
||
| 1041 | $Product->removeProductTag($ProductTag); |
||
| 1042 | $this->entityManager->remove($ProductTag); |
||
| 1043 | } |
||
| 1044 | |||
| 1045 | if (StringUtil::isNotBlank($row[$headerByKey['product_tag']])) { |
||
| 1046 | // タグの登録 |
||
| 1047 | 1 | $tags = explode(',', $row[$headerByKey['product_tag']]); |
|
| 1048 | 1 | foreach ($tags as $tag_id) { |
|
| 1049 | 1 | $Tag = null; |
|
| 1050 | 1 | if (preg_match('/^\d+$/', $tag_id)) { |
|
| 1051 | $Tag = $this->tagRepository->find($tag_id); |
||
| 1052 | |||
| 1053 | if ($Tag) { |
||
| 1054 | 1 | $ProductTags = new ProductTag(); |
|
| 1055 | $ProductTags |
||
| 1056 | ->setProduct($Product) |
||
| 1057 | ->setTag($Tag); |
||
| 1058 | |||
| 1059 | $Product->addProductTag($ProductTags); |
||
| 1060 | |||
| 1061 | $this->entityManager->persist($ProductTags); |
||
| 1062 | 1 | } |
|
| 1063 | } |
||
| 1064 | View Code Duplication | if (!$Tag) { |
|
| 1065 | $message = trans('admin.common.csv_invalid_not_found_target', [ |
||
| 1066 | '%line%' => $data->key() + 1, |
||
| 1067 | '%name%' => $headerByKey['product_tag'], |
||
| 1068 | '%target_name%' => $tag_id, |
||
| 1069 | ]); |
||
| 1070 | $this->addErrors($message); |
||
| 1071 | } |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | 1 | * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成 |
|
| 1078 | 1 | * |
|
| 1079 | * @param $row |
||
| 1080 | * @param Product $Product |
||
| 1081 | * @param CsvImportService $data |
||
| 1082 | * @param $headerByKey |
||
| 1083 | 1 | * @param null $ClassCategory1 |
|
| 1084 | * @param null $ClassCategory2 |
||
| 1085 | * |
||
| 1086 | * @return ProductClass |
||
| 1087 | 1 | */ |
|
| 1088 | 1 | protected function createProductClass($row, Product $Product, $data, $headerByKey, $ClassCategory1 = null, $ClassCategory2 = null) |
|
| 1089 | { |
||
| 1090 | 1 | // 規格分類1、規格分類2がnullとなる商品を作成 |
|
| 1091 | $ProductClass = new ProductClass(); |
||
| 1092 | $ProductClass->setProduct($Product); |
||
| 1093 | $ProductClass->setVisible(true); |
||
| 1094 | 1 | ||
| 1095 | 1 | $line = $data->key() + 1; |
|
| 1096 | 1 | View Code Duplication | if (isset($row[$headerByKey['sale_type']]) && StringUtil::isNotBlank($row[$headerByKey['sale_type']])) { |
| 1097 | if (preg_match('/^\d+$/', $row[$headerByKey['sale_type']])) { |
||
| 1098 | $SaleType = $this->saleTypeRepository->find($row[$headerByKey['sale_type']]); |
||
| 1099 | 1 | if (!$SaleType) { |
|
| 1100 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['sale_type']]); |
||
| 1101 | $this->addErrors($message); |
||
| 1102 | 1 | } else { |
|
| 1103 | 1 | $ProductClass->setSaleType($SaleType); |
|
| 1104 | 1 | } |
|
| 1105 | } else { |
||
| 1106 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['sale_type']]); |
||
| 1107 | $this->addErrors($message); |
||
| 1108 | } |
||
| 1109 | } else { |
||
| 1110 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['sale_type']]); |
||
| 1111 | 1 | $this->addErrors($message); |
|
| 1112 | 1 | } |
|
| 1113 | 1 | ||
| 1114 | 1 | $ProductClass->setClassCategory1($ClassCategory1); |
|
| 1115 | $ProductClass->setClassCategory2($ClassCategory2); |
||
| 1116 | |||
| 1117 | View Code Duplication | if (isset($row[$headerByKey['delivery_date']]) && StringUtil::isNotBlank($row[$headerByKey['delivery_date']])) { |
|
| 1118 | if (preg_match('/^\d+$/', $row[$headerByKey['delivery_date']])) { |
||
| 1119 | $DeliveryDuration = $this->deliveryDurationRepository->find($row[$headerByKey['delivery_date']]); |
||
| 1120 | if (!$DeliveryDuration) { |
||
| 1121 | 1 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['delivery_date']]); |
|
| 1122 | 1 | $this->addErrors($message); |
|
| 1123 | 1 | } else { |
|
| 1124 | 1 | $ProductClass->setDeliveryDuration($DeliveryDuration); |
|
| 1125 | } |
||
| 1126 | } else { |
||
| 1127 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['delivery_date']]); |
||
| 1128 | $this->addErrors($message); |
||
| 1129 | } |
||
| 1130 | } |
||
| 1131 | 1 | ||
| 1132 | View Code Duplication | if (isset($row[$headerByKey['product_code']]) && StringUtil::isNotBlank($row[$headerByKey['product_code']])) { |
|
| 1133 | $ProductClass->setCode(StringUtil::trimAll($row[$headerByKey['product_code']])); |
||
| 1134 | } else { |
||
| 1135 | 1 | $ProductClass->setCode(null); |
|
| 1136 | 1 | } |
|
| 1137 | 1 | ||
| 1138 | View Code Duplication | if (!isset($row[$headerByKey['stock_unlimited']]) |
|
| 1139 | || StringUtil::isBlank($row[$headerByKey['stock_unlimited']]) |
||
| 1140 | || $row[$headerByKey['stock_unlimited']] == (string) Constant::DISABLED |
||
| 1141 | ) { |
||
| 1142 | $ProductClass->setStockUnlimited(false); |
||
| 1143 | // 在庫数が設定されていなければエラー |
||
| 1144 | 1 | if (isset($row[$headerByKey['stock']]) && StringUtil::isNotBlank($row[$headerByKey['stock']])) { |
|
| 1145 | $stock = str_replace(',', '', $row[$headerByKey['stock']]); |
||
| 1146 | 1 | if (preg_match('/^\d+$/', $stock) && $stock >= 0) { |
|
| 1147 | 1 | $ProductClass->setStock($stock); |
|
| 1148 | } else { |
||
| 1149 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['stock']]); |
||
| 1150 | 1 | $this->addErrors($message); |
|
| 1151 | } |
||
| 1152 | } else { |
||
| 1153 | 1 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['stock']]); |
|
| 1154 | $this->addErrors($message); |
||
| 1155 | } |
||
| 1156 | } elseif ($row[$headerByKey['stock_unlimited']] == (string) Constant::ENABLED) { |
||
| 1157 | $ProductClass->setStockUnlimited(true); |
||
| 1158 | $ProductClass->setStock(null); |
||
| 1159 | 7 | } else { |
|
| 1160 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['stock_unlimited']]); |
||
| 1161 | 7 | $this->addErrors($message); |
|
| 1162 | 7 | } |
|
| 1163 | |||
| 1164 | View Code Duplication | if (isset($row[$headerByKey['sale_limit']]) && StringUtil::isNotBlank($row[$headerByKey['sale_limit']])) { |
|
| 1165 | $saleLimit = str_replace(',', '', $row[$headerByKey['sale_limit']]); |
||
| 1166 | if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) { |
||
| 1167 | $ProductClass->setSaleLimit($saleLimit); |
||
| 1168 | 16 | } else { |
|
| 1169 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['sale_limit']]); |
||
| 1170 | 16 | $this->addErrors($message); |
|
| 1171 | } |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | if (isset($row[$headerByKey['price01']]) && StringUtil::isNotBlank($row[$headerByKey['price01']])) { |
||
| 1175 | $price01 = str_replace(',', '', $row[$headerByKey['price01']]); |
||
| 1176 | 16 | $errors = $this->validator->validate($price01, new GreaterThanOrEqual(['value' => 0])); |
|
| 1177 | if ($errors->count() === 0) { |
||
| 1178 | 16 | $ProductClass->setPrice01($price01); |
|
| 1179 | } else { |
||
| 1180 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['price01']]); |
||
| 1181 | $this->addErrors($message); |
||
| 1182 | } |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | View Code Duplication | if (isset($row[$headerByKey['price02']]) && StringUtil::isNotBlank($row[$headerByKey['price02']])) { |
|
| 1186 | 10 | $price02 = str_replace(',', '', $row[$headerByKey['price02']]); |
|
| 1187 | $errors = $this->validator->validate($price02, new GreaterThanOrEqual(['value' => 0])); |
||
| 1188 | if ($errors->count() === 0) { |
||
| 1189 | 10 | $ProductClass->setPrice02($price02); |
|
| 1190 | } else { |
||
| 1191 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['price02']]); |
||
| 1192 | $this->addErrors($message); |
||
| 1193 | } |
||
| 1194 | 10 | } else { |
|
| 1195 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['price02']]); |
||
| 1196 | $this->addErrors($message); |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | 10 | View Code Duplication | if ($this->BaseInfo->isOptionProductDeliveryFee()) { |
| 1200 | if (isset($row[$headerByKey['delivery_fee']]) && StringUtil::isNotBlank($row[$headerByKey['delivery_fee']])) { |
||
| 1201 | $delivery_fee = str_replace(',', '', $row[$headerByKey['delivery_fee']]); |
||
| 1202 | $errors = $this->validator->validate($delivery_fee, new GreaterThanOrEqual(['value' => 0])); |
||
| 1203 | if ($errors->count() === 0) { |
||
| 1204 | 10 | $ProductClass->setDeliveryFee($delivery_fee); |
|
| 1205 | } else { |
||
| 1206 | $message = trans('admin.common.csv_invalid_greater_than_zero', |
||
| 1207 | ['%line%' => $line, '%name%' => $headerByKey['delivery_fee']]); |
||
| 1208 | $this->addErrors($message); |
||
| 1209 | 10 | } |
|
| 1210 | } |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | $Product->addProductClass($ProductClass); |
||
| 1214 | 10 | $ProductStock = new ProductStock(); |
|
| 1215 | $ProductClass->setProductStock($ProductStock); |
||
| 1216 | $ProductStock->setProductClass($ProductClass); |
||
| 1217 | |||
| 1218 | if (!$ProductClass->isStockUnlimited()) { |
||
| 1219 | 10 | $ProductStock->setStock($ProductClass->getStock()); |
|
| 1220 | } else { |
||
| 1221 | // 在庫無制限時はnullを設定 |
||
| 1222 | $ProductStock->setStock(null); |
||
| 1223 | } |
||
| 1224 | 10 | ||
| 1225 | $this->entityManager->persist($ProductClass); |
||
| 1226 | $this->entityManager->persist($ProductStock); |
||
| 1227 | |||
| 1228 | return $ProductClass; |
||
| 1229 | 10 | } |
|
| 1230 | |||
| 1231 | /** |
||
| 1232 | * 商品規格情報を更新 |
||
| 1233 | * |
||
| 1234 | 10 | * @param $row |
|
| 1235 | * @param Product $Product |
||
| 1236 | * @param ProductClass $ProductClass |
||
| 1237 | * @param CsvImportService $data |
||
| 1238 | * |
||
| 1239 | 10 | * @return ProductClass |
|
| 1240 | */ |
||
| 1241 | protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $data, $headerByKey) |
||
| 1242 | { |
||
| 1243 | $ProductClass->setProduct($Product); |
||
| 1244 | 10 | ||
| 1245 | $line = $data->key() + 1; |
||
| 1246 | View Code Duplication | if (!isset($row[$headerByKey['sale_type']]) || $row[$headerByKey['sale_type']] == '') { |
|
| 1247 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['sale_type']]); |
||
| 1248 | $this->addErrors($message); |
||
| 1249 | 10 | } else { |
|
| 1250 | if (preg_match('/^\d+$/', $row[$headerByKey['sale_type']])) { |
||
| 1251 | $SaleType = $this->saleTypeRepository->find($row[$headerByKey['sale_type']]); |
||
| 1252 | if (!$SaleType) { |
||
| 1253 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['sale_type']]); |
||
| 1254 | 10 | $this->addErrors($message); |
|
| 1255 | } else { |
||
| 1256 | $ProductClass->setSaleType($SaleType); |
||
| 1257 | } |
||
| 1258 | } else { |
||
| 1259 | 10 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['sale_type']]); |
|
| 1260 | $this->addErrors($message); |
||
| 1261 | } |
||
| 1262 | } |
||
| 1263 | |||
| 1264 | 10 | // 規格分類1、2をそれぞれセットし作成 |
|
| 1265 | View Code Duplication | if (isset($row[$headerByKey['class_category1']]) && $row[$headerByKey['class_category1']] != '') { |
|
| 1266 | if (preg_match('/^\d+$/', $row[$headerByKey['class_category1']])) { |
||
| 1267 | $ClassCategory = $this->classCategoryRepository->find($row[$headerByKey['class_category1']]); |
||
| 1268 | if (!$ClassCategory) { |
||
| 1269 | 10 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category1']]); |
|
| 1270 | $this->addErrors($message); |
||
| 1271 | } else { |
||
| 1272 | $ProductClass->setClassCategory1($ClassCategory); |
||
| 1273 | } |
||
| 1274 | 10 | } else { |
|
| 1275 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category1']]); |
||
| 1276 | $this->addErrors($message); |
||
| 1277 | } |
||
| 1278 | } |
||
| 1279 | 10 | ||
| 1280 | View Code Duplication | if (isset($row[$headerByKey['class_category2']]) && $row[$headerByKey['class_category2']] != '') { |
|
| 1281 | if (preg_match('/^\d+$/', $row[$headerByKey['class_category2']])) { |
||
| 1282 | $ClassCategory = $this->classCategoryRepository->find($row[$headerByKey['class_category2']]); |
||
| 1283 | if (!$ClassCategory) { |
||
| 1284 | 10 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
|
| 1285 | $this->addErrors($message); |
||
| 1286 | } else { |
||
| 1287 | $ProductClass->setClassCategory2($ClassCategory); |
||
| 1288 | } |
||
| 1289 | 10 | } else { |
|
| 1290 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['class_category2']]); |
||
| 1291 | $this->addErrors($message); |
||
| 1292 | } |
||
| 1293 | } |
||
| 1294 | 10 | ||
| 1295 | View Code Duplication | if (isset($row[$headerByKey['delivery_date']]) && $row[$headerByKey['delivery_date']] != '') { |
|
| 1296 | if (preg_match('/^\d+$/', $row[$headerByKey['delivery_date']])) { |
||
| 1297 | $DeliveryDuration = $this->deliveryDurationRepository->find($row[$headerByKey['delivery_date']]); |
||
| 1298 | if (!$DeliveryDuration) { |
||
| 1299 | 10 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['delivery_date']]); |
|
| 1300 | $this->addErrors($message); |
||
| 1301 | } else { |
||
| 1302 | $ProductClass->setDeliveryDuration($DeliveryDuration); |
||
| 1303 | } |
||
| 1304 | } else { |
||
| 1305 | $message = trans('admin.common.csv_invalid_not_found', ['%line%' => $line, '%name%' => $headerByKey['delivery_date']]); |
||
| 1306 | $this->addErrors($message); |
||
| 1307 | } |
||
| 1308 | } |
||
| 1309 | |||
| 1310 | 6 | View Code Duplication | if (isset($row[$headerByKey['product_code']]) && StringUtil::isNotBlank($row[$headerByKey['product_code']])) { |
| 1311 | $ProductClass->setCode(StringUtil::trimAll($row[$headerByKey['product_code']])); |
||
| 1312 | } else { |
||
| 1313 | 6 | $ProductClass->setCode(null); |
|
| 1314 | } |
||
| 1315 | |||
| 1316 | View Code Duplication | if (!isset($row[$headerByKey['stock_unlimited']]) |
|
| 1317 | || StringUtil::isBlank($row[$headerByKey['stock_unlimited']]) |
||
| 1318 | 6 | || $row[$headerByKey['stock_unlimited']] == (string) Constant::DISABLED |
|
| 1319 | ) { |
||
| 1320 | $ProductClass->setStockUnlimited(false); |
||
| 1321 | // 在庫数が設定されていなければエラー |
||
| 1322 | if ($row[$headerByKey['stock']] == '') { |
||
| 1323 | 6 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['stock']]); |
|
| 1324 | $this->addErrors($message); |
||
| 1325 | } else { |
||
| 1326 | $stock = str_replace(',', '', $row[$headerByKey['stock']]); |
||
| 1327 | if (preg_match('/^\d+$/', $stock) && $stock >= 0) { |
||
| 1328 | 6 | $ProductClass->setStock($row[$headerByKey['stock']]); |
|
| 1329 | } else { |
||
| 1330 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['stock']]); |
||
| 1331 | $this->addErrors($message); |
||
| 1332 | } |
||
| 1333 | } |
||
| 1334 | } elseif ($row[$headerByKey['stock_unlimited']] == (string) Constant::ENABLED) { |
||
| 1335 | $ProductClass->setStockUnlimited(true); |
||
| 1336 | $ProductClass->setStock(null); |
||
| 1337 | } else { |
||
| 1338 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['stock_unlimited']]); |
||
| 1339 | $this->addErrors($message); |
||
| 1340 | } |
||
| 1341 | |||
| 1342 | View Code Duplication | if (isset($row[$headerByKey['sale_limit']]) && $row[$headerByKey['sale_limit']] != '') { |
|
| 1343 | $saleLimit = str_replace(',', '', $row[$headerByKey['sale_limit']]); |
||
| 1344 | if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) { |
||
| 1345 | 4 | $ProductClass->setSaleLimit($saleLimit); |
|
| 1346 | } else { |
||
| 1347 | 4 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['sale_limit']]); |
|
| 1348 | 4 | $this->addErrors($message); |
|
| 1349 | 4 | } |
|
| 1350 | 4 | } |
|
| 1351 | 4 | ||
| 1352 | 4 | if (isset($row[$headerByKey['price01']]) && $row[$headerByKey['price01']] != '') { |
|
| 1353 | $price01 = str_replace(',', '', $row[$headerByKey['price01']]); |
||
| 1354 | 4 | $errors = $this->validator->validate($price01, new GreaterThanOrEqual(['value' => 0])); |
|
| 1355 | if ($errors->count() === 0) { |
||
| 1356 | $ProductClass->setPrice01($price01); |
||
| 1357 | } else { |
||
| 1358 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['price01']]); |
||
| 1359 | $this->addErrors($message); |
||
| 1360 | } |
||
| 1361 | } |
||
| 1362 | |||
| 1363 | View Code Duplication | if (!isset($row[$headerByKey['price02']]) || $row[$headerByKey['price02']] == '') { |
|
| 1364 | $message = trans('admin.common.csv_invalid_required', ['%line%' => $line, '%name%' => $headerByKey['price02']]); |
||
| 1365 | $this->addErrors($message); |
||
| 1366 | } else { |
||
| 1367 | $price02 = str_replace(',', '', $row[$headerByKey['price02']]); |
||
| 1368 | $errors = $this->validator->validate($price02, new GreaterThanOrEqual(['value' => 0])); |
||
| 1369 | if ($errors->count() === 0) { |
||
| 1370 | $ProductClass->setPrice02($price02); |
||
| 1371 | } else { |
||
| 1372 | $message = trans('admin.common.csv_invalid_greater_than_zero', ['%line%' => $line, '%name%' => $headerByKey['price02']]); |
||
| 1373 | $this->addErrors($message); |
||
| 1374 | } |
||
| 1375 | } |
||
| 1376 | |||
| 1377 | $ProductStock = $ProductClass->getProductStock(); |
||
| 1378 | |||
| 1379 | if (!$ProductClass->isStockUnlimited()) { |
||
| 1380 | $ProductStock->setStock($ProductClass->getStock()); |
||
| 1381 | } else { |
||
| 1382 | // 在庫無制限時はnullを設定 |
||
| 1383 | $ProductStock->setStock(null); |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | return $ProductClass; |
||
| 1387 | } |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * 登録、更新時のエラー画面表示 |
||
| 1391 | */ |
||
| 1392 | protected function addErrors($message) |
||
| 1393 | { |
||
| 1394 | $this->errors[] = $message; |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * @return array |
||
| 1399 | */ |
||
| 1400 | protected function getErrors() |
||
| 1401 | { |
||
| 1402 | return $this->errors; |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * @return boolean |
||
| 1407 | */ |
||
| 1408 | protected function hasErrors() |
||
| 1409 | { |
||
| 1410 | return count($this->getErrors()) > 0; |
||
| 1411 | } |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * 商品登録CSVヘッダー定義 |
||
| 1415 | * |
||
| 1416 | * @return array |
||
| 1417 | */ |
||
| 1418 | protected function getProductCsvHeader() |
||
| 1419 | { |
||
| 1420 | return [ |
||
| 1421 | trans('admin.product.product_csv.product_id_col') => [ |
||
| 1422 | 'id' => 'id', |
||
| 1423 | 'description' => 'admin.product.product_csv.product_id_description', |
||
| 1424 | 'required' => false, |
||
| 1425 | ], |
||
| 1426 | trans('admin.product.product_csv.display_status_col') => [ |
||
| 1427 | 'id' => 'status', |
||
| 1428 | 'description' => 'admin.product.product_csv.display_status_description', |
||
| 1429 | 'required' => true, |
||
| 1430 | ], |
||
| 1431 | trans('admin.product.product_csv.product_name_col') => [ |
||
| 1432 | 'id' => 'name', |
||
| 1433 | 'description' => 'admin.product.product_csv.product_name_description', |
||
| 1434 | 'required' => true, |
||
| 1435 | ], |
||
| 1436 | trans('admin.product.product_csv.shop_memo_col') => [ |
||
| 1437 | 'id' => 'note', |
||
| 1438 | 'description' => 'admin.product.product_csv.shop_memo_description', |
||
| 1439 | 'required' => false, |
||
| 1440 | ], |
||
| 1441 | trans('admin.product.product_csv.description_list_col') => [ |
||
| 1442 | 'id' => 'description_list', |
||
| 1443 | 'description' => 'admin.product.product_csv.description_list_description', |
||
| 1444 | 'required' => false, |
||
| 1445 | ], |
||
| 1446 | trans('admin.product.product_csv.description_detail_col') => [ |
||
| 1447 | 'id' => 'description_detail', |
||
| 1448 | 'description' => 'admin.product.product_csv.description_detail_description', |
||
| 1449 | 'required' => false, |
||
| 1450 | ], |
||
| 1451 | trans('admin.product.product_csv.keyword_col') => [ |
||
| 1452 | 'id' => 'search_word', |
||
| 1453 | 'description' => 'admin.product.product_csv.keyword_description', |
||
| 1454 | 'required' => false, |
||
| 1455 | ], |
||
| 1456 | trans('admin.product.product_csv.free_area_col') => [ |
||
| 1457 | 'id' => 'free_area', |
||
| 1458 | 'description' => 'admin.product.product_csv.free_area_description', |
||
| 1459 | 'required' => false, |
||
| 1460 | ], |
||
| 1461 | trans('admin.product.product_csv.delete_flag_col') => [ |
||
| 1462 | 'id' => 'product_del_flg', |
||
| 1463 | 'description' => 'admin.product.product_csv.delete_flag_description', |
||
| 1464 | 'required' => false, |
||
| 1465 | ], |
||
| 1466 | trans('admin.product.product_csv.product_image_col') => [ |
||
| 1467 | 'id' => 'product_image', |
||
| 1468 | 'description' => 'admin.product.product_csv.product_image_description', |
||
| 1469 | 'required' => false, |
||
| 1470 | ], |
||
| 1471 | trans('admin.product.product_csv.category_col') => [ |
||
| 1472 | 'id' => 'product_category', |
||
| 1473 | 'description' => 'admin.product.product_csv.category_description', |
||
| 1474 | 'required' => false, |
||
| 1475 | ], |
||
| 1476 | trans('admin.product.product_csv.tag_col') => [ |
||
| 1477 | 'id' => 'product_tag', |
||
| 1478 | 'description' => 'admin.product.product_csv.tag_description', |
||
| 1479 | 'required' => false, |
||
| 1480 | ], |
||
| 1481 | trans('admin.product.product_csv.sale_type_col') => [ |
||
| 1482 | 'id' => 'sale_type', |
||
| 1483 | 'description' => 'admin.product.product_csv.sale_type_description', |
||
| 1484 | 'required' => true, |
||
| 1485 | ], |
||
| 1486 | trans('admin.product.product_csv.class_category1_col') => [ |
||
| 1487 | 'id' => 'class_category1', |
||
| 1488 | 'description' => 'admin.product.product_csv.class_category1_description', |
||
| 1489 | 'required' => false, |
||
| 1490 | ], |
||
| 1491 | trans('admin.product.product_csv.class_category2_col') => [ |
||
| 1492 | 'id' => 'class_category2', |
||
| 1493 | 'description' => 'admin.product.product_csv.class_category2_description', |
||
| 1494 | 'required' => false, |
||
| 1495 | ], |
||
| 1496 | trans('admin.product.product_csv.delivery_duration_col') => [ |
||
| 1497 | 'id' => 'delivery_date', |
||
| 1498 | 'description' => 'admin.product.product_csv.delivery_duration_description', |
||
| 1499 | 'required' => false, |
||
| 1500 | ], |
||
| 1501 | trans('admin.product.product_csv.product_code_col') => [ |
||
| 1502 | 'id' => 'product_code', |
||
| 1503 | 'description' => 'admin.product.product_csv.product_code_description', |
||
| 1504 | 'required' => false, |
||
| 1505 | ], |
||
| 1506 | trans('admin.product.product_csv.stock_col') => [ |
||
| 1507 | 'id' => 'stock', |
||
| 1508 | 'description' => 'admin.product.product_csv.stock_description', |
||
| 1509 | 'required' => false, |
||
| 1510 | ], |
||
| 1511 | trans('admin.product.product_csv.stock_unlimited_col') => [ |
||
| 1512 | 'id' => 'stock_unlimited', |
||
| 1513 | 'description' => 'admin.product.product_csv.stock_unlimited_description', |
||
| 1514 | 'required' => false, |
||
| 1515 | ], |
||
| 1516 | trans('admin.product.product_csv.sale_limit_col') => [ |
||
| 1517 | 'id' => 'sale_limit', |
||
| 1518 | 'description' => 'admin.product.product_csv.sale_limit_description', |
||
| 1519 | 'required' => false, |
||
| 1520 | ], |
||
| 1521 | trans('admin.product.product_csv.normal_price_col') => [ |
||
| 1522 | 'id' => 'price01', |
||
| 1523 | 'description' => 'admin.product.product_csv.normal_price_description', |
||
| 1524 | 'required' => false, |
||
| 1525 | ], |
||
| 1526 | trans('admin.product.product_csv.sale_price_col') => [ |
||
| 1527 | 'id' => 'price02', |
||
| 1528 | 'description' => 'admin.product.product_csv.sale_price_description', |
||
| 1529 | 'required' => true, |
||
| 1530 | ], |
||
| 1531 | trans('admin.product.product_csv.delivery_fee_col') => [ |
||
| 1532 | 'id' => 'delivery_fee', |
||
| 1533 | 'description' => 'admin.product.product_csv.delivery_fee_description', |
||
| 1534 | 'required' => false, |
||
| 1535 | ], |
||
| 1536 | trans('admin.product.product_csv.tax_rate_col') => [ |
||
| 1537 | 'id' => 'tax_rate', |
||
| 1538 | 'description' => 'admin.product.product_csv.tax_rate_description', |
||
| 1539 | 'required' => false, |
||
| 1540 | ], |
||
| 1541 | ]; |
||
| 1542 | } |
||
| 1543 | |||
| 1544 | /** |
||
| 1545 | * カテゴリCSVヘッダー定義 |
||
| 1546 | */ |
||
| 1547 | protected function getCategoryCsvHeader() |
||
| 1548 | { |
||
| 1549 | return [ |
||
| 1550 | trans('admin.product.category_csv.category_id_col') => [ |
||
| 1551 | 'id' => 'id', |
||
| 1552 | 'description' => 'admin.product.category_csv.category_id_description', |
||
| 1553 | 'required' => false, |
||
| 1554 | ], |
||
| 1555 | trans('admin.product.category_csv.category_name_col') => [ |
||
| 1556 | 'id' => 'category_name', |
||
| 1557 | 'description' => 'admin.product.category_csv.category_name_description', |
||
| 1558 | 'required' => true, |
||
| 1559 | ], |
||
| 1560 | trans('admin.product.category_csv.parent_category_id_col') => [ |
||
| 1561 | 'id' => 'parent_category_id', |
||
| 1562 | 'description' => 'admin.product.category_csv.parent_category_id_description', |
||
| 1563 | 'required' => false, |
||
| 1564 | ], |
||
| 1565 | trans('admin.product.category_csv.delete_flag_col') => [ |
||
| 1566 | 'id' => 'category_del_flg', |
||
| 1567 | 'description' => 'admin.product.category_csv.delete_flag_description', |
||
| 1568 | 'required' => false, |
||
| 1569 | ], |
||
| 1570 | ]; |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * ProductCategory作成 |
||
| 1575 | * |
||
| 1576 | * @param \Eccube\Entity\Product $Product |
||
| 1577 | * @param \Eccube\Entity\Category $Category |
||
| 1578 | * @param int $sortNo |
||
| 1579 | * |
||
| 1580 | * @return ProductCategory |
||
| 1581 | */ |
||
| 1582 | View Code Duplication | private function makeProductCategory($Product, $Category, $sortNo) |
|
| 1583 | { |
||
| 1584 | $ProductCategory = new ProductCategory(); |
||
| 1585 | $ProductCategory->setProduct($Product); |
||
| 1586 | $ProductCategory->setProductId($Product->getId()); |
||
| 1587 | $ProductCategory->setCategory($Category); |
||
| 1588 | $ProductCategory->setCategoryId($Category->getId()); |
||
| 1589 | |||
| 1590 | return $ProductCategory; |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * @Route("/%eccube_admin_route%/product/csv_split", name="admin_product_csv_split") |
||
| 1595 | * @param Request $request |
||
| 1596 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 1597 | */ |
||
| 1598 | public function splitCsv(Request $request) |
||
| 1599 | { |
||
| 1600 | $this->isTokenValid(); |
||
| 1601 | |||
| 1602 | if (!$request->isXmlHttpRequest()) { |
||
| 1603 | throw new BadRequestHttpException(); |
||
| 1604 | } |
||
| 1605 | |||
| 1606 | $form = $this->formFactory->createBuilder(CsvImportType::class)->getForm(); |
||
| 1607 | $form->handleRequest($request); |
||
| 1608 | |||
| 1609 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 1610 | |||
| 1611 | $dir = $this->eccubeConfig['eccube_csv_temp_realdir']; |
||
| 1612 | if (!file_exists($dir)) { |
||
| 1613 | $fs = new Filesystem(); |
||
| 1614 | $fs->mkdir($dir); |
||
| 1615 | } |
||
| 1616 | |||
| 1617 | $data = $form['import_file']->getData(); |
||
| 1618 | $src = new \SplFileObject($data->getRealPath()); |
||
| 1619 | $src->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); |
||
| 1620 | |||
| 1621 | $fileNo = 1; |
||
| 1622 | $fileName = StringUtil::random(8); |
||
| 1623 | |||
| 1624 | $dist = new \SplFileObject($dir.'/'.$fileName.$fileNo.'.csv', 'w'); |
||
| 1625 | $header = $src->current(); |
||
| 1626 | $src->next(); |
||
| 1627 | $dist->fputcsv($header); |
||
| 1628 | |||
| 1629 | $i = 0; |
||
| 1630 | while ($row = $src->current()) { |
||
| 1631 | $dist->fputcsv($row); |
||
| 1632 | $src->next(); |
||
| 1633 | |||
| 1634 | if (!$src->eof() && ++$i % $this->eccubeConfig['eccube_csv_split_lines'] === 0) { |
||
| 1635 | $fileNo++; |
||
| 1636 | $dist = new \SplFileObject($dir.'/'.$fileName.$fileNo.'.csv', 'w'); |
||
| 1637 | $dist->fputcsv($header); |
||
| 1638 | } |
||
| 1639 | } |
||
| 1640 | |||
| 1641 | return $this->json(['success' => true, 'file_name' => $fileName, 'max_file_no' => $fileNo]); |
||
| 1642 | } |
||
| 1643 | |||
| 1644 | return $this->json(['success' => false, 'message' => $form->getErrors(true ,true)]); |
||
| 1645 | } |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * @Route("/%eccube_admin_route%/product/csv_split_import", name="admin_product_csv_split_import") |
||
| 1649 | * @param Request $request |
||
| 1650 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
| 1651 | */ |
||
| 1652 | public function importCsv(Request $request, CsrfTokenManagerInterface $tokenManager) |
||
| 1694 | |||
| 1695 | protected function convertLineNo($currentLineNo) { |
||
| 1702 | } |
||
| 1703 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.