CsvImportController   D
last analyzed

Complexity

Total Complexity 195

Size/Duplication

Total Lines 1131
Duplicated Lines 19.19 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 71.27%

Importance

Changes 0
Metric Value
dl 217
loc 1131
ccs 382
cts 536
cp 0.7127
rs 4.4102
c 0
b 0
f 0
wmc 195
lcom 1
cbo 9

16 Methods

Rating   Name   Duplication   Size   Complexity  
F csvProduct() 59 347 70
D csvCategory() 34 135 21
B csvTemplate() 0 36 4
B render() 0 24 5
B getImportData() 0 39 6
B createProductImage() 0 27 4
C createProductCategory() 9 49 9
C createProductTag() 0 35 7
F createProductClass() 52 136 30
F updateProductClass() 52 141 33
A addErrors() 0 5 1
A getErrors() 0 4 1
A hasErrors() 0 4 1
B getProductCsvHeader() 0 29 1
A getCategoryCsvHeader() 0 8 1
A makeProductCategory() 11 11 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Product;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Category;
30
use Eccube\Entity\Product;
31
use Eccube\Entity\ProductCategory;
32
use Eccube\Entity\ProductClass;
33
use Eccube\Entity\ProductImage;
34
use Eccube\Entity\ProductStock;
35
use Eccube\Entity\ProductTag;
36
use Eccube\Exception\CsvImportException;
37
use Eccube\Service\CsvImportService;
38
use Eccube\Util\Str;
39
use Symfony\Component\Filesystem\Filesystem;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\HttpFoundation\StreamedResponse;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
44
class CsvImportController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
45
{
46
47
    private $errors = array();
48
49
    private $fileName;
50
51
    private $em;
52
53
    private $productTwig = 'Product/csv_product.twig';
54
55
    private $categoryTwig = 'Product/csv_category.twig';
56
57
58
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
59
     * 商品登録CSVアップロード
60
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
61 3
    public function csvProduct(Application $app, Request $request)
62
    {
63 3
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
64
65 3
        $headers = $this->getProductCsvHeader();
66
67 3
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
68
69 3
            $form->handleRequest($request);
70
71 3
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
72
73 3
                $formFile = $form['import_file']->getData();
74
75 3
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
76
77 3
                    log_info('商品CSV登録開始');
78
79 3
                    $data = $this->getImportData($app, $formFile);
80 3
                    if ($data === false) {
81
                        $this->addErrors('CSVのフォーマットが一致しません。');
82
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
83
                    }
84
85 3
                    $keys = array_keys($headers);
86 3
                    $columnHeaders = $data->getColumnHeaders();
87 3
                    if ($keys !== $columnHeaders) {
88
                        $this->addErrors('CSVのフォーマットが一致しません。');
89
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
90
                    }
91
92 3
                    $size = count($data);
93 3
                    if ($size < 1) {
94
                        $this->addErrors('CSVデータが存在しません。');
95
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
96
                    }
97
98 3
                    $headerSize = count($keys);
99
100 3
                    $this->em = $app['orm.em'];
101 3
                    $this->em->getConfiguration()->setSQLLogger(null);
102
103 3
                    $this->em->getConnection()->beginTransaction();
104
105 3
                    $BaseInfo = $app['eccube.repository.base_info']->get();
106
107
                    // CSVファイルの登録処理
108 3
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
109
110 3
                        if ($headerSize != count($row)) {
111
                            $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
112
                            return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
113
                        }
114
115 3
                        if ($row['商品ID'] == '') {
116 2
                            $Product = new Product();
117 2
                            $this->em->persist($Product);
118
                        } else {
119 2
                            if (preg_match('/^\d+$/', $row['商品ID'])) {
120 2
                                $Product = $app['eccube.repository.product']->find($row['商品ID']);
121 2
                                if (!$Product) {
122
                                    $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
123 2
                                    return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
124
                                }
125 View Code Duplication
                            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
126
                                $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
127
                                return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
128
                            }
129
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
130
                        }
131
132 3
                        if ($row['公開ステータス(ID)'] == '') {
133
                            $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
134
                        } else {
135 3
                            if (preg_match('/^\d+$/', $row['公開ステータス(ID)'])) {
136 3
                                $Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']);
137 3
                                if (!$Disp) {
138
                                    $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
139
                                } else {
140 3
                                    $Product->setStatus($Disp);
141
                                }
142
                            } else {
143
                                $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
144
                            }
145
                        }
146
147 3 View Code Duplication
                        if (Str::isBlank($row['商品名'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
148
                            $this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
149
                            return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
150
                        } else {
151 3
                            $Product->setName(Str::trimAll($row['商品名']));
152
                        }
153
154 3 View Code Duplication
                        if (Str::isNotBlank($row['ショップ用メモ欄'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
155 2
                            $Product->setNote(Str::trimAll($row['ショップ用メモ欄']));
156
                        } else {
157 1
                            $Product->setNote(null);
158
                        }
159
160 3 View Code Duplication
                        if (Str::isNotBlank($row['商品説明(一覧)'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
161 3
                            $Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)']));
162
                        } else {
163 1
                            $Product->setDescriptionList(null);
164
                        }
165
166 3 View Code Duplication
                        if (Str::isNotBlank($row['商品説明(詳細)'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
167 3
                            $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)']));
168
                        } else {
169
                            $Product->setDescriptionDetail(null);
170
                        }
171
172 3 View Code Duplication
                        if (Str::isNotBlank($row['検索ワード'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
173 2
                            $Product->setSearchWord(Str::trimAll($row['検索ワード']));
174
                        } else {
175 1
                            $Product->setSearchWord(null);
176
                        }
177
178 3 View Code Duplication
                        if (Str::isNotBlank($row['フリーエリア'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
179 2
                            $Product->setFreeArea(Str::trimAll($row['フリーエリア']));
180
                        } else {
181 1
                            $Product->setFreeArea(null);
182
                        }
183
184 3
                        if ($row['商品削除フラグ'] == '') {
185
                            $Product->setDelFlg(Constant::DISABLED);
186
                        } else {
187 3
                            if ($row['商品削除フラグ'] == (string)Constant::DISABLED || $row['商品削除フラグ'] == (string)Constant::ENABLED) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
188 3
                                $Product->setDelFlg($row['商品削除フラグ']);
189 View Code Duplication
                            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
190
                                $this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
191
                                return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
192
                            }
193
                        }
194
195
                        // 商品画像登録
196 3
                        $this->createProductImage($row, $Product);
197
198 3
                        $this->em->flush($Product);
199
200
                        // 商品カテゴリ登録
201 3
                        $this->createProductCategory($row, $Product, $app, $data);
202
203
                        //タグ登録
204 3
                        $this->createProductTag($row, $Product, $app, $data);
205
206
                        // 商品規格が存在しなければ新規登録
207 3
                        $ProductClasses = $Product->getProductClasses();
208 3
                        if ($ProductClasses->count() < 1) {
209
                            // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成
210 2
                            $ProductClassOrg = $this->createProductClass($row, $Product, $app, $data);
211 2
                            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以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
218
                                    }
219
                                }
220
                            }
221
222 2
                            if ($row['規格分類1(ID)'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
223
224 1
                                if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
225
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
226
                                } else {
227
                                    // 商品規格あり
228
                                    // 企画分類あり商品を作成
229 1
                                    $ProductClass = clone $ProductClassOrg;
230 1
                                    $ProductStock = clone $ProductClassOrg->getProductStock();
231
232
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
233 1
                                    $ProductClassOrg->setDelFlg(Constant::ENABLED);
234
235
                                    // 規格分類1、2をそれぞれセットし作成
236 1
                                    $ClassCategory1 = null;
237 1
                                    if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) {
238 1
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
239 1
                                        if (!$ClassCategory1) {
240
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
241
                                        } else {
242 1
                                            $ProductClass->setClassCategory1($ClassCategory1);
243
                                        }
244
                                    } else {
245
                                        $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
246
                                    }
247
248 1
                                    if ($row['規格分類2(ID)'] != '') {
249 1
                                        if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) {
250 1
                                            $ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
251 1 View Code Duplication
                                            if (!$ClassCategory2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
252
                                                $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
253
                                            } else {
254 1
                                                if ($ClassCategory1 &&
255 1
                                                    ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
256
                                                ) {
257
                                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
258
                                                } else {
259 1
                                                    $ProductClass->setClassCategory2($ClassCategory2);
260
                                                }
261
                                            }
262
                                        } else {
263
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
264
                                        }
265
                                    }
266 1
                                    $ProductClass->setProductStock($ProductStock);
267 1
                                    $ProductStock->setProductClass($ProductClass);
268
269 1
                                    $this->em->persist($ProductClass);
270 1
                                    $this->em->persist($ProductStock);
271
                                }
272
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
273
                            } else {
274 1
                                if ($row['規格分類2(ID)'] != '') {
275 2
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
276
                                }
277
                            }
278
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
279
                        } else {
280
                            // 商品規格の更新
281
282 2
                            $flag = false;
283 2
                            $classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)'];
284 2
                            $classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)'];
285
286 2
                            foreach ($ProductClasses as $pc) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
287
288 2
                                $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId();
289 2
                                $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId();
290
291
                                // 登録されている商品規格を更新
292 2
                                if ($classCategory1 == $classCategoryId1 &&
293 2
                                    $classCategory2 == $classCategoryId2
294
                                ) {
295 1
                                    $this->updateProductClass($row, $Product, $pc, $app, $data);
296
297 1
                                    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以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
304
                                            }
305
                                        }
306
                                    }
307
308 1
                                    $flag = true;
309 2
                                    break;
310
                                }
311
                            }
312
313
                            // 商品規格を登録
314 2
                            if (!$flag) {
315 1
                                $pc = $ProductClasses[0];
316 1
                                if ($pc->getClassCategory1() == null &&
317 1
                                    $pc->getClassCategory2() == null
318
                                ) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
319
320
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
321 1
                                    $pc->setDelFlg(Constant::ENABLED);
322
                                }
323
324 1
                                if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
325
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
326
                                } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
327
328
                                    // 必ず規格分類1がセットされている
329
                                    // 規格分類1、2をそれぞれセットし作成
330 1
                                    $ClassCategory1 = null;
331 1
                                    if (preg_match('/^\d+$/', $classCategoryId1)) {
332 1
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1);
333 1
                                        if (!$ClassCategory1) {
334 1
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
335
                                        }
336
                                    } else {
337
                                        $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
338
                                    }
339
340 1
                                    $ClassCategory2 = null;
341 1
                                    if ($row['規格分類2(ID)'] != '') {
342 1
                                        if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) {
343
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)は設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
344
                                        } else {
345 1
                                            if (preg_match('/^\d+$/', $classCategoryId2)) {
346 1
                                                $ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2);
347 1 View Code Duplication
                                                if (!$ClassCategory2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
348
                                                    $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
349
                                                } else {
350 1
                                                    if ($ClassCategory1 &&
351 1
                                                        ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
352
                                                    ) {
353 1
                                                        $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
354
                                                    }
355
                                                }
356
                                            } else {
357 1
                                                $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
358
                                            }
359
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
360
                                        }
361
                                    } else {
362
                                        if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) {
363
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)に値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
364
                                        }
365
                                    }
366 1
                                    $ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2);
367
368 1
                                    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以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
375
                                            }
376
                                        }
377
                                    }
378
379 1
                                    $Product->addProductClass($ProductClass);
380
                                }
381
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
382
                            }
383
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
384
                        }
385
386
387 3
                        if ($this->hasErrors()) {
388
                            return $this->render($app, $form, $headers, $this->productTwig);
389
                        }
390
391 3
                        $this->em->persist($Product);
392
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
393
                    }
394
395 3
                    $this->em->flush();
396 3
                    $this->em->getConnection()->commit();
397
398 3
                    log_info('商品CSV登録完了');
399
400 3
                    $app->addSuccess('admin.product.csv_import.save.complete', 'admin');
401
                }
402
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
403
            }
404
        }
405
406 3
        return $this->render($app, $form, $headers, $this->productTwig);
407
    }
408
409
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
410
     * カテゴリ登録CSVアップロード
411
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
412 2
    public function csvCategory(Application $app, Request $request)
413
    {
414
415 2
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
416
417 2
        $headers = $this->getCategoryCsvHeader();
418
419 2
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
420
421 2
            $form->handleRequest($request);
422
423 2
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
424
425 2
                $formFile = $form['import_file']->getData();
426
427 2
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
428
429 2
                    log_info('カテゴリCSV登録開始');
430
431 2
                    $data = $this->getImportData($app, $formFile);
432 2 View Code Duplication
                    if ($data === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
433
                        $this->addErrors('CSVのフォーマットが一致しません。');
434
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
435
                    }
436
437 2
                    $keys = array_keys($headers);
438 2
                    $columnHeaders = $data->getColumnHeaders();
439 2
                    if ($keys !== $columnHeaders) {
440
                        $this->addErrors('CSVのフォーマットが一致しません。');
441
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
442
                    }
443
444 2
                    $size = count($data);
445 2
                    if ($size < 1) {
446
                        $this->addErrors('CSVデータが存在しません。');
447
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
448
                    }
449
450 2
                    $headerSize = count($keys);
451
452 2
                    $this->em = $app['orm.em'];
453 2
                    $this->em->getConfiguration()->setSQLLogger(null);
454
455 2
                    $this->em->getConnection()->beginTransaction();
456
457
                    // CSVファイルの登録処理
458 2
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
459
460 2
                        if ($headerSize != count($row)) {
461
                            $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
462
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
463
                        }
464
465 2
                        if ($row['カテゴリID'] == '') {
466 1
                            $Category = new Category();
467
                        } else {
468 1 View Code Duplication
                            if (!preg_match('/^\d+$/', $row['カテゴリID'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
469
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
470
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
471
                            }
472 1
                            $Category = $app['eccube.repository.category']->find($row['カテゴリID']);
473 1 View Code Duplication
                            if (!$Category) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
474
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
475
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
476
                            }
477 1 View Code Duplication
                            if ($row['カテゴリID'] == $row['親カテゴリID']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
478
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDと親カテゴリIDが同じです。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
479
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
480
                            }
481
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
482
                        }
483
484 2 View Code Duplication
                        if (Str::isBlank($row['カテゴリ名'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
485
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリ名が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
486
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
487
                        } else {
488 2
                            $Category->setName(Str::trimAll($row['カテゴリ名']));
489
                        }
490
491 2
                        if ($row['親カテゴリID'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
492
493 1 View Code Duplication
                            if (!preg_match('/^\d+$/', $row['親カテゴリID'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
494
                                $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
495
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
496
                            }
497
498 1
                            $ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']);
499 1 View Code Duplication
                            if (!$ParentCategory) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
500
                                $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
501 1
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
502
                            }
503
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
504
                        } else {
505 2
                            $ParentCategory = null;
506
                        }
507
508 2
                        $Category->setParent($ParentCategory);
509 2
                        if ($ParentCategory) {
510 1
                            $Category->setLevel($ParentCategory->getLevel() + 1);
511
                        } else {
512 2
                            $Category->setLevel(1);
513
                        }
514
515 2 View Code Duplication
                        if ($app['config']['category_nest_level'] < $Category->getLevel()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
516
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリが最大レベルを超えているため設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
517
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
518
                        }
519
520 2
                        $status = $app['eccube.repository.category']->save($Category);
521
522 2
                        if (!$status) {
523
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリが設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
524
                        }
525
526 2
                        if ($this->hasErrors()) {
527
                            return $this->render($app, $form, $headers, $this->categoryTwig);
528
                        }
529
530 2
                        $this->em->persist($Category);
531
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
532
                    }
533
534 2
                    $this->em->flush();
535 2
                    $this->em->getConnection()->commit();
536
537 2
                    log_info('カテゴリCSV登録完了');
538
539 2
                    $app->addSuccess('admin.category.csv_import.save.complete', 'admin');
540
                }
541
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
542
            }
543
        }
544
545 2
        return $this->render($app, $form, $headers, $this->categoryTwig);
546
    }
547
548
549
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$type" missing
Loading history...
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
550
     * アップロード用CSV雛形ファイルダウンロード
551
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
552
    public function csvTemplate(Application $app, Request $request, $type)
553
    {
554
        set_time_limit(0);
555
556
        $response = new StreamedResponse();
557
558
        if ($type == 'product') {
559
            $headers = $this->getProductCsvHeader();
560
            $filename = 'product.csv';
561
        } else if ($type == 'category') {
562
            $headers = $this->getCategoryCsvHeader();
563
            $filename = 'category.csv';
564
        } else {
565
            throw new NotFoundHttpException();
566
        }
567
568
        $response->setCallback(function () use ($app, $request, $headers) {
569
570
            // ヘッダ行の出力
571
            $row = array();
572
            foreach ($headers as $key => $value) {
573
                $row[] = mb_convert_encoding($key, $app['config']['csv_export_encoding'], 'UTF-8');
574
            }
575
576
            $fp = fopen('php://output', 'w');
577
            fputcsv($fp, $row, $app['config']['csv_export_separator']);
578
            fclose($fp);
579
580
        });
581
582
        $response->headers->set('Content-Type', 'application/octet-stream');
583
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
584
        $response->send();
585
586
        return $response;
587
    }
588
589
590
    /**
591
     * 登録、更新時のエラー画面表示
592
     *
593
     */
594 5
    protected function render($app, $form, $headers, $twig)
595
    {
596
597 5
        if ($this->hasErrors()) {
598
            if ($this->em) {
599
                $this->em->getConnection()->rollback();
600
            }
601
        }
602
603 5
        if (!empty($this->fileName)) {
604
            try {
605 5
                $fs = new Filesystem();
606 5
                $fs->remove($app['config']['csv_temp_realdir'] . '/' . $this->fileName);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
607
            } catch (\Exception $e) {
608
                // エラーが発生しても無視する
609
            }
610
        }
611
612 5
        return $app->render($twig, array(
613 5
            'form' => $form->createView(),
614 5
            'headers' => $headers,
615 5
            'errors' => $this->errors,
616
        ));
617
    }
618
619
620
    /**
621
     * アップロードされたCSVファイルの行ごとの処理
622
     *
623
     * @param $formFile
624
     * @return CsvImportService
625
     */
626 5
    protected function getImportData($app, $formFile)
627
    {
628
        // アップロードされたCSVファイルを一時ディレクトリに保存
629 5
        $this->fileName = 'upload_' . Str::random() . '.' . $formFile->getClientOriginalExtension();
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
630 5
        $formFile->move($app['config']['csv_temp_realdir'], $this->fileName);
631
632 5
        $file = file_get_contents($app['config']['csv_temp_realdir'] . '/' . $this->fileName);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
633
634 5
        if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) {
635
            // Windows 環境の PHP7 の場合はファイルエンコーディングを CP932 に合わせる
636
            // see https://github.com/EC-CUBE/ec-cube/issues/1780
637
            setlocale(LC_ALL, ''); // 既定のロケールに設定
638
            if (mb_detect_encoding($file) === 'UTF-8') { // UTF-8 を検出したら SJIS-win に変換
639
                $file = mb_convert_encoding($file, 'SJIS-win', 'UTF-8');
640
            }
641
        } else {
642
            // アップロードされたファイルがUTF-8以外は文字コード変換を行う
643 5
            $encode = Str::characterEncoding(substr($file, 0, 6));
644 5
            if ($encode != 'UTF-8') {
645 2
                $file = mb_convert_encoding($file, 'UTF-8', $encode);
646
            }
647
        }
648 5
        $file = Str::convertLineFeed($file);
649
650 5
        $tmp = tmpfile();
651 5
        fwrite($tmp, $file);
652 5
        rewind($tmp);
653 5
        $meta = stream_get_meta_data($tmp);
654 5
        $file = new \SplFileObject($meta['uri']);
655
656 5
        set_time_limit(0);
657
658
        // アップロードされたCSVファイルを行ごとに取得
659 5
        $data = new CsvImportService($file, $app['config']['csv_import_delimiter'], $app['config']['csv_import_enclosure']);
660
661 5
        $ret = $data->setHeaderRowNumber(0);
662
663 5
        return ($ret !== false) ? $data : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $ret !== false ? $data : false; of type Eccube\Service\CsvImportService|false adds false to the return on line 663 which is incompatible with the return type documented by Eccube\Controller\Admin\...ntroller::getImportData of type Eccube\Service\CsvImportService. It seems like you forgot to handle an error condition.
Loading history...
664
    }
665
666
667
    /**
668
     * 商品画像の削除、登録
669
     */
670 3
    protected function createProductImage($row, Product $Product)
671
    {
672 3
        if ($row['商品画像'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
673
674
            // 画像の削除
675 3
            $ProductImages = $Product->getProductImage();
676 3
            foreach ($ProductImages as $ProductImage) {
677 2
                $Product->removeProductImage($ProductImage);
678 3
                $this->em->remove($ProductImage);
679
            }
680
681
            // 画像の登録
682 3
            $images = explode(',', $row['商品画像']);
683 3
            $rank = 1;
684 3
            foreach ($images as $image) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
685
686 3
                $ProductImage = new ProductImage();
687 3
                $ProductImage->setFileName(Str::trimAll($image));
688 3
                $ProductImage->setProduct($Product);
689 3
                $ProductImage->setRank($rank);
690
691 3
                $Product->addProductImage($ProductImage);
692 3
                $rank++;
693 3
                $this->em->persist($ProductImage);
694
            }
695
        }
696
    }
697
698
699
    /**
700
     * 商品カテゴリの削除、登録
701
     */
702 3
    protected function createProductCategory($row, Product $Product, $app, $data)
703
    {
704
        // カテゴリの削除
705 3
        $ProductCategories = $Product->getProductCategories();
706 3
        foreach ($ProductCategories as $ProductCategory) {
707 2
            $Product->removeProductCategory($ProductCategory);
708 2
            $this->em->remove($ProductCategory);
709 3
            $this->em->flush($ProductCategory);
710
        }
711
712 3
        if ($row['商品カテゴリ(ID)'] == '') {
713
            // 入力されていなければ削除のみ
714
            return;
715
        }
716
717
        // カテゴリの登録
718 3
        $categories = explode(',', $row['商品カテゴリ(ID)']);
719 3
        $rank = 1;
720 3
        $categoriesIdList = array();
721 3
        foreach ($categories as $category) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
722
723 3
            if (preg_match('/^\d+$/', $category)) {
724 3
                $Category = $app['eccube.repository.category']->find($category);
725 3
                if (!$Category) {
726
                    $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。');
727
                } else {
728 3 View Code Duplication
                    foreach($Category->getPath() as $ParentCategory){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
729 3
                        if (!isset($categoriesIdList[$ParentCategory->getId()])){
730 3
                            $ProductCategory = $this->makeProductCategory($Product, $ParentCategory, $rank);
731 3
                            $app['orm.em']->persist($ProductCategory);
732 3
                            $rank++;
733 3
                            $Product->addProductCategory($ProductCategory);
734 3
                            $categoriesIdList[$ParentCategory->getId()] = true;
735
                        }
736
                    }
737 3
                    if (!isset($categoriesIdList[$Category->getId()])){
738
                        $ProductCategory = $this->makeProductCategory($Product, $Category, $rank);
739
                        $rank++;
740
                        $this->em->persist($ProductCategory);
741
                        $Product->addProductCategory($ProductCategory);
742 3
                        $categoriesIdList[$Category->getId()] = true;
743
                    }
744
                }
745
            } else {
746 3
                $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。');
747
            }
748
        }
749
750
    }
751
752
753
    /**
754
     * タグの登録
755
     *
756
     * @param array $row
757
     * @param Product $Product
758
     * @param Application $app
759
     * @param CsvImportService $data
760
     */
761 3
    protected function createProductTag($row, Product $Product, $app, $data)
762
    {
763
        // タグの削除
764 3
        $ProductTags = $Product->getProductTag();
765 3
        foreach ($ProductTags as $ProductTags) {
766 1
            $Product->removeProductTag($ProductTags);
767 3
            $this->em->remove($ProductTags);
768
        }
769
770 3
        if ($row['タグ(ID)'] == '') {
771
            return;
772
        }
773
774
        // タグの登録
775 3
        $tags = explode(',', $row['タグ(ID)']);
776 3
        foreach ($tags as $tag_id) {
777 3
            $Tag = null;
778 3
            if (preg_match('/^\d+$/', $tag_id)) {
779 3
                $Tag = $app['eccube.repository.master.tag']->find($tag_id);
780 3
                if ($Tag) {
781 3
                    $ProductTags = new ProductTag();
782
                    $ProductTags
783 3
                        ->setProduct($Product)
784 3
                        ->setTag($Tag);
785
786 3
                    $Product->addProductTag($ProductTags);
787
788 3
                    $this->em->persist($ProductTags);
789
                }
790
            }
791 3
            if (!$Tag) {
792 3
                $this->addErrors(($data->key() + 1) . '行目のタグ(ID)「' . $tag_id . '」が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
793
            }
794
        }
795
    }
796
797
798
    /**
799
     * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成
800
     */
801 3
    protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null)
802
    {
803
        // 規格分類1、規格分類2がnullとなる商品を作成
804
805 3
        $ProductClass = new ProductClass();
806 3
        $ProductClass->setProduct($Product);
807
808
809 3 View Code Duplication
        if ($row['商品種別(ID)'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
810
            $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
811
        } else {
812 3
            if (preg_match('/^\d+$/', $row['商品種別(ID)'])) {
813 3
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
814 3
                if (!$ProductType) {
815
                    $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
816
                } else {
817 3
                    $ProductClass->setProductType($ProductType);
818
                }
819
            } else {
820
                $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
821
            }
822
        }
823
824 3
        $ProductClass->setClassCategory1($ClassCategory1);
825 3
        $ProductClass->setClassCategory2($ClassCategory2);
826
827 3
        if ($row['発送日目安(ID)'] != '') {
828 2
            if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) {
829 2
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
830 2
                if (!$DeliveryDate) {
831
                    $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
832
                } else {
833 2
                    $ProductClass->setDeliveryDate($DeliveryDate);
834
                }
835
            } else {
836
                $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
837
            }
838
        }
839
840 3 View Code Duplication
        if (Str::isNotBlank($row['商品コード'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
841 2
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
842
        } else {
843 1
            $ProductClass->setCode(null);
844
        }
845
846 3 View Code Duplication
        if ($row['在庫数無制限フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
847
            $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
848
        } else {
849 3
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
850 3
                $ProductClass->setStockUnlimited(Constant::DISABLED);
851
                // 在庫数が設定されていなければエラー
852 3
                if ($row['在庫数'] == '') {
853
                    $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
854
                } else {
855 3
                    $stock = str_replace(',', '', $row['在庫数']);
856 3
                    if (preg_match('/^\d+$/', $stock) && $stock >= 0) {
857 3
                        $ProductClass->setStock($stock);
858
                    } else {
859 3
                        $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
860
                    }
861
                }
862
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
863
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
864
                $ProductClass->setStockUnlimited(Constant::ENABLED);
865
                $ProductClass->setStock(null);
866
            } else {
867
                $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
868
            }
869
        }
870
871 3
        if ($row['販売制限数'] != '') {
872
            $saleLimit = str_replace(',', '', $row['販売制限数']);
873
            if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) {
874
                $ProductClass->setSaleLimit($saleLimit);
875
            } else {
876
                $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
877
            }
878
        }
879
880 3
        if ($row['通常価格'] != '') {
881 2
            $price01 = str_replace(',', '', $row['通常価格']);
882 2
            if (preg_match('/^\d+$/', $price01) && $price01 >= 0) {
883 2
                $ProductClass->setPrice01($price01);
884
            } else {
885
                $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
886
            }
887
        }
888
889 3
        if ($row['販売価格'] == '') {
890
            $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
891
        } else {
892 3
            $price02 = str_replace(',', '', $row['販売価格']);
893 3
            if (preg_match('/^\d+$/', $price02) && $price02 >= 0) {
894 3
                $ProductClass->setPrice02($price02);
895
            } else {
896
                $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
897
            }
898
        }
899
900 3
        if ($row['送料'] != '') {
901 2
            $delivery_fee = str_replace(',', '', $row['送料']);
902 2
            if (preg_match('/^\d+$/', $delivery_fee) && $delivery_fee >= 0) {
903 2
                $ProductClass->setDeliveryFee($delivery_fee);
904
            } else {
905
                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
906
            }
907
        }
908
909 3 View Code Duplication
        if ($row['商品規格削除フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
910
            $ProductClass->setDelFlg(Constant::DISABLED);
911
        } else {
912 3
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
913 3
                $ProductClass->setDelFlg($row['商品規格削除フラグ']);
914
            } else {
915
                $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
916
            }
917
        }
918
919 3
        $Product->addProductClass($ProductClass);
920 3
        $ProductStock = new ProductStock();
921 3
        $ProductClass->setProductStock($ProductStock);
922 3
        $ProductStock->setProductClass($ProductClass);
923
924 3
        if (!$ProductClass->getStockUnlimited()) {
925 3
            $ProductStock->setStock($ProductClass->getStock());
926
        } else {
927
            // 在庫無制限時はnullを設定
928
            $ProductStock->setStock(null);
929
        }
930
931 3
        $this->em->persist($ProductClass);
932 3
        $this->em->persist($ProductStock);
933
934 3
        return $ProductClass;
935
936
    }
937
938
939
    /**
940
     * 商品規格情報を更新
941
     */
942 1
    protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data)
943
    {
944
945 1
        $ProductClass->setProduct($Product);
946
947 1 View Code Duplication
        if ($row['商品種別(ID)'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
948
            $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
949
        } else {
950 1
            if (preg_match('/^\d+$/', $row['商品種別(ID)'])) {
951 1
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
952 1
                if (!$ProductType) {
953
                    $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
954
                } else {
955 1
                    $ProductClass->setProductType($ProductType);
956
                }
957
            } else {
958
                $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
959
            }
960
        }
961
962
        // 規格分類1、2をそれぞれセットし作成
963 1
        if ($row['規格分類1(ID)'] != '') {
964 1
            if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) {
965 1
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
966 1
                if (!$ClassCategory) {
967
                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
968
                } else {
969 1
                    $ProductClass->setClassCategory1($ClassCategory);
970
                }
971
            } else {
972
                $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
973
            }
974
        }
975
976 1
        if ($row['規格分類2(ID)'] != '') {
977 1
            if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) {
978 1
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
979 1
                if (!$ClassCategory) {
980
                    $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
981
                } else {
982 1
                    $ProductClass->setClassCategory2($ClassCategory);
983
                }
984
            } else {
985
                $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
986
            }
987
        }
988
989 1
        if ($row['発送日目安(ID)'] != '') {
990
            if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) {
991
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
992
                if (!$DeliveryDate) {
993
                    $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
994
                } else {
995
                    $ProductClass->setDeliveryDate($DeliveryDate);
996
                }
997
            } else {
998
                $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
999
            }
1000
        }
1001
1002 1 View Code Duplication
        if (Str::isNotBlank($row['商品コード'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1003 1
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
1004
        } else {
1005
            $ProductClass->setCode(null);
1006
        }
1007
1008 1 View Code Duplication
        if ($row['在庫数無制限フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1009
            $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1010
        } else {
1011 1
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
1012 1
                $ProductClass->setStockUnlimited(Constant::DISABLED);
1013
                // 在庫数が設定されていなければエラー
1014 1
                if ($row['在庫数'] == '') {
1015
                    $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1016
                } else {
1017 1
                    $stock = str_replace(',', '', $row['在庫数']);
1018 1
                    if (preg_match('/^\d+$/', $stock) && $stock >= 0) {
1019 1
                        $ProductClass->setStock($row['在庫数']);
1020
                    } else {
1021 1
                        $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1022
                    }
1023
                }
1024
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1025 1
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
1026 1
                $ProductClass->setStockUnlimited(Constant::ENABLED);
1027 1
                $ProductClass->setStock(null);
1028
            } else {
1029
                $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1030
            }
1031
        }
1032
1033 1
        if ($row['販売制限数'] != '') {
1034 1
            $saleLimit = str_replace(',', '', $row['販売制限数']);
1035 1
            if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) {
1036 1
                $ProductClass->setSaleLimit($saleLimit);
1037
            } else {
1038
                $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1039
            }
1040
        }
1041
1042 1
        if ($row['通常価格'] != '') {
1043 1
            $price01 = str_replace(',', '', $row['通常価格']);
1044 1
            if (preg_match('/^\d+$/', $price01) && $price01 >= 0) {
1045 1
                $ProductClass->setPrice01($price01);
1046
            } else {
1047
                $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1048
            }
1049
        }
1050
1051 1
        if ($row['販売価格'] == '') {
1052
            $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1053
        } else {
1054 1
            $price02 = str_replace(',', '', $row['販売価格']);
1055 1
            if (preg_match('/^\d+$/', $price02) && $price02 >= 0) {
1056 1
                $ProductClass->setPrice02($price02);
1057
            } else {
1058
                $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1059
            }
1060
        }
1061
1062 1 View Code Duplication
        if ($row['商品規格削除フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1063
            $ProductClass->setDelFlg(Constant::DISABLED);
1064
        } else {
1065 1
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
1066 1
                $ProductClass->setDelFlg($row['商品規格削除フラグ']);
1067
            } else {
1068
                $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1069
            }
1070
        }
1071
1072 1
        $ProductStock = $ProductClass->getProductStock();
1073
1074 1
        if (!$ProductClass->getStockUnlimited()) {
1075 1
            $ProductStock->setStock($ProductClass->getStock());
1076
        } else {
1077
            // 在庫無制限時はnullを設定
1078 1
            $ProductStock->setStock(null);
1079
        }
1080
1081 1
        return $ProductClass;
1082
    }
1083
1084
    /**
1085
     * 登録、更新時のエラー画面表示
1086
     *
1087
     */
1088
    protected function addErrors($message)
1089
    {
1090
        $e = new CsvImportException($message);
1091
        $this->errors[] = $e;
1092
    }
1093
1094
    /**
1095
     * @return array
1096
     */
1097 5
    protected function getErrors()
1098
    {
1099 5
        return $this->errors;
1100
    }
1101
1102
    /**
1103
     *
1104
     * @return boolean
1105
     */
1106 5
    protected function hasErrors()
1107
    {
1108 5
        return count($this->getErrors()) > 0;
1109
    }
1110
1111
    /**
1112
     * 商品登録CSVヘッダー定義
1113
     */
1114 3
    private function getProductCsvHeader()
1115
    {
1116
        return array(
1117 3
            '商品ID' => 'id',
1118
            '公開ステータス(ID)' => 'status',
1119
            '商品名' => 'name',
1120
            'ショップ用メモ欄' => 'note',
1121
            '商品説明(一覧)' => 'description_list',
1122
            '商品説明(詳細)' => 'description_detail',
1123
            '検索ワード' => 'search_word',
1124
            'フリーエリア' => 'free_area',
1125
            '商品削除フラグ' => 'product_del_flg',
1126
            '商品画像' => 'product_image',
1127
            '商品カテゴリ(ID)' => 'product_category',
1128
            'タグ(ID)' => 'product_tag',
1129
            '商品種別(ID)' => 'product_type',
1130
            '規格分類1(ID)' => 'class_category1',
1131
            '規格分類2(ID)' => 'class_category2',
1132
            '発送日目安(ID)' => 'deliveryFee',
1133
            '商品コード' => 'product_code',
1134
            '在庫数' => 'stock',
1135
            '在庫数無制限フラグ' => 'stock_unlimited',
1136
            '販売制限数' => 'sale_limit',
1137
            '通常価格' => 'price01',
1138
            '販売価格' => 'price02',
1139
            '送料' => 'delivery_fee',
1140
            '商品規格削除フラグ' => 'product_class_del_flg',
1141
        );
1142
    }
1143
1144
1145
    /**
1146
     * カテゴリCSVヘッダー定義
1147
     */
1148 2
    private function getCategoryCsvHeader()
1149
    {
1150
        return array(
1151 2
            'カテゴリID' => 'id',
1152
            'カテゴリ名' => 'category_name',
1153
            '親カテゴリID' => 'parent_category_id',
1154
        );
1155
    }
1156
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
1157
        /**
1158
     * ProductCategory作成
1159
     * @param \Eccube\Entity\Product $Product
1160
     * @param \Eccube\Entity\Category $Category
1161
     * @return ProductCategory
1162
     */
1163 3 View Code Duplication
    private function makeProductCategory($Product, $Category, $rank)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1164
    {
1165 3
        $ProductCategory = new ProductCategory();
1166 3
        $ProductCategory->setProduct($Product);
1167 3
        $ProductCategory->setProductId($Product->getId());
1168 3
        $ProductCategory->setCategory($Category);
1169 3
        $ProductCategory->setCategoryId($Category->getId());
1170 3
        $ProductCategory->setRank($rank);
1171
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
1172 3
        return $ProductCategory;
1173
    }
1174
}
1175