CsvImportController   D
last analyzed

Complexity

Total Complexity 181

Size/Duplication

Total Lines 1036
Duplicated Lines 18.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 181
lcom 1
cbo 9
dl 189
loc 1036
rs 4.4314
c 2
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
F csvProduct() 51 341 70
D csvCategory() 34 131 21
B csvTemplate() 0 36 4
B render() 0 24 5
B getImportData() 0 29 3
B createProductImage() 0 27 4
B createProductCategory() 0 38 6
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 28 1
A getCategoryCsvHeader() 0 8 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\Exception\CsvImportException;
36
use Eccube\Service\CsvImportService;
37
use Eccube\Util\Str;
38
use Symfony\Component\Filesystem\Filesystem;
39
use Symfony\Component\HttpFoundation\Request;
40
use Symfony\Component\HttpFoundation\StreamedResponse;
41
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
42
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
    public function csvProduct(Application $app, Request $request)
62
    {
63
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
64
65
        $headers = $this->getProductCsvHeader();
66
67
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
68
69
            $form->handleRequest($request);
70
71
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
72
73
                $formFile = $form['import_file']->getData();
74
75
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
76
77
                    $data = $this->getImportData($app, $formFile);
78
                    if ($data === false) {
79
                        $this->addErrors('CSVのフォーマットが一致しません。');
80
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
81
                    }
82
83
                    $keys = array_keys($headers);
84
                    $columnHeaders = $data->getColumnHeaders();
85
                    if ($keys !== $columnHeaders) {
86
                        $this->addErrors('CSVのフォーマットが一致しません。');
87
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
88
                    }
89
90
                    $size = count($data);
91
                    if ($size < 1) {
92
                        $this->addErrors('CSVデータが存在しません。');
93
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
94
                    }
95
96
                    $headerSize = count($keys);
97
98
                    $this->em = $app['orm.em'];
99
                    $this->em->getConfiguration()->setSQLLogger(null);
100
101
                    $this->em->getConnection()->beginTransaction();
102
103
                    $BaseInfo = $app['eccube.repository.base_info']->get();
104
105
                    // CSVファイルの登録処理
106
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
107
108
                        if ($headerSize != count($row)) {
109
                            $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
110
                            return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
111
                        }
112
113
                        if ($row['商品ID'] == '') {
114
                            $Product = new Product();
115
                            $this->em->persist($Product);
116
                        } else {
117
                            if (is_numeric($row['商品ID'])) {
118
                                $Product = $app['eccube.repository.product']->find($row['商品ID']);
119
                                if (!$Product) {
120
                                    $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
121
                                    return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
122
                                }
123
                            } else {
124
                                $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
125
                                return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
126
                            }
127
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
128
                        }
129
130
                        if ($row['公開ステータス(ID)'] == '') {
131
                            $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
132
                        } else {
133
                            if (is_numeric($row['公開ステータス(ID)'])) {
134
                                $Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']);
135
                                if (!$Disp) {
136
                                    $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
137
                                } else {
138
                                    $Product->setStatus($Disp);
139
                                }
140
                            } else {
141
                                $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
142
                            }
143
                        }
144
145 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...
146
                            $this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
147
                            return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
148
                        } else {
149
                            $Product->setName(Str::trimAll($row['商品名']));
150
                        }
151
152 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...
153
                            $Product->setNote(Str::trimAll($row['ショップ用メモ欄']));
154
                        } else {
155
                            $Product->setNote(null);
156
                        }
157
158 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...
159
                            $Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)']));
160
                        } else {
161
                            $Product->setDescriptionList(null);
162
                        }
163
164 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...
165
                            $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)']));
166
                        } else {
167
                            $Product->setDescriptionDetail(null);
168
                        }
169
170 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...
171
                            $Product->setSearchWord(Str::trimAll($row['検索ワード']));
172
                        } else {
173
                            $Product->setSearchWord(null);
174
                        }
175
176 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...
177
                            $Product->setFreeArea(Str::trimAll($row['フリーエリア']));
178
                        } else {
179
                            $Product->setFreeArea(null);
180
                        }
181
182
                        if ($row['商品削除フラグ'] == '') {
183
                            $Product->setDelFlg(Constant::DISABLED);
184
                        } else {
185
                            if ($row['商品削除フラグ'] == (string)Constant::DISABLED || $row['商品削除フラグ'] == (string)Constant::ENABLED) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
186
                                $Product->setDelFlg($row['商品削除フラグ']);
187
                            } else {
188
                                $this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
189
                                return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
190
                            }
191
                        }
192
193
                        // 商品画像登録
194
                        $this->createProductImage($row, $Product);
195
196
                        $this->em->flush($Product);
197
198
                        // 商品カテゴリ登録
199
                        $this->createProductCategory($row, $Product, $app, $data);
200
201
202
                        // 商品規格が存在しなければ新規登録
203
                        $ProductClasses = $Product->getProductClasses();
204
                        if ($ProductClasses->count() < 1) {
205
                            // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成
206
                            $ProductClassOrg = $this->createProductClass($row, $Product, $app, $data);
207
                            if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
208
                                if ($row['送料'] != '') {
209
                                    $deliveryFee = str_replace(',', '', $row['送料']);
210
                                    if (is_numeric($deliveryFee) && $deliveryFee >= 0) {
211
                                        $ProductClassOrg->setDeliveryFee($deliveryFee);
212
                                    } else {
213
                                        $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
214
                                    }
215
                                }
216
                            }
217
218
                            if ($row['規格分類1(ID)'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
219
220
                                if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
221
                                    $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...
222
                                } else {
223
                                    // 商品規格あり
224
                                    // 企画分類あり商品を作成
225
                                    $ProductClass = clone $ProductClassOrg;
226
                                    $ProductStock = clone $ProductClassOrg->getProductStock();
227
228
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
229
                                    $ProductClassOrg->setDelFlg(Constant::ENABLED);
230
231
                                    // 規格分類1、2をそれぞれセットし作成
232
                                    $ClassCategory1 = null;
233
                                    if (is_numeric($row['規格分類1(ID)'])) {
234
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
235
                                        if (!$ClassCategory1) {
236
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
237
                                        } else {
238
                                            $ProductClass->setClassCategory1($ClassCategory1);
239
                                        }
240
                                    } else {
241
                                        $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...
242
                                    }
243
244
                                    if ($row['規格分類2(ID)'] != '') {
245
                                        if (is_numeric($row['規格分類2(ID)'])) {
246
                                            $ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
247 View Code Duplication
                                            if (!$ClassCategory2) {
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...
248
                                                $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...
249
                                            } else {
250
                                                if ($ClassCategory1 &&
251
                                                    ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
252
                                                ) {
253
                                                    $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...
254
                                                } else {
255
                                                    $ProductClass->setClassCategory2($ClassCategory2);
256
                                                }
257
                                            }
258
                                        } else {
259
                                            $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...
260
                                        }
261
                                    }
262
                                    $ProductClass->setProductStock($ProductStock);
263
                                    $ProductStock->setProductClass($ProductClass);
264
265
                                    $this->em->persist($ProductClass);
266
                                    $this->em->persist($ProductStock);
267
                                }
268
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
269
                            } else {
270
                                if ($row['規格分類2(ID)'] != '') {
271
                                    $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...
272
                                }
273
                            }
274
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
275
                        } else {
276
                            // 商品規格の更新
277
278
                            $flag = false;
279
                            $classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)'];
280
                            $classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)'];
281
282
                            foreach ($ProductClasses as $pc) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
283
284
                                $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId();
285
                                $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId();
286
287
                                // 登録されている商品規格を更新
288
                                if ($classCategory1 == $classCategoryId1 &&
289
                                    $classCategory2 == $classCategoryId2
290
                                ) {
291
                                    $this->updateProductClass($row, $Product, $pc, $app, $data);
292
293
                                    if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
294
                                        if ($row['送料'] != '') {
295
                                            $deliveryFee = str_replace(',', '', $row['送料']);
296
                                            if (is_numeric($deliveryFee) && $deliveryFee >= 0) {
297
                                                $pc->setDeliveryFee($deliveryFee);
298
                                            } else {
299
                                                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
300
                                            }
301
                                        }
302
                                    }
303
304
                                    $flag = true;
305
                                    break;
306
                                }
307
                            }
308
309
                            // 商品規格を登録
310
                            if (!$flag) {
311
                                $pc = $ProductClasses[0];
312
                                if ($pc->getClassCategory1() == null &&
313
                                    $pc->getClassCategory2() == null
314
                                ) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
315
316
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
317
                                    $pc->setDelFlg(Constant::ENABLED);
318
                                }
319
320
                                if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
321
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
322
                                } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
323
324
                                    // 必ず規格分類1がセットされている
325
                                    // 規格分類1、2をそれぞれセットし作成
326
                                    $ClassCategory1 = null;
327
                                    if (is_numeric($classCategoryId1)) {
328
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1);
329
                                        if (!$ClassCategory1) {
330
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
331
                                        }
332
                                    } else {
333
                                        $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...
334
                                    }
335
336
                                    $ClassCategory2 = null;
337
                                    if ($row['規格分類2(ID)'] != '') {
338
                                        if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) {
339
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)は設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
340
                                        } else {
341
                                            if (is_numeric($classCategoryId2)) {
342
                                                $ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2);
343 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...
344
                                                    $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...
345
                                                } else {
346
                                                    if ($ClassCategory1 &&
347
                                                        ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
348
                                                    ) {
349
                                                        $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...
350
                                                    }
351
                                                }
352
                                            } else {
353
                                                $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...
354
                                            }
355
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
356
                                        }
357
                                    } else {
358
                                        if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) {
359
                                            $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...
360
                                        }
361
                                    }
362
                                    $ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2);
363
364
                                    if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
365
                                        if ($row['送料'] != '') {
366
                                            $deliveryFee = str_replace(',', '', $row['送料']);
367
                                            if (is_numeric($deliveryFee) && $deliveryFee >= 0) {
368
                                                $ProductClass->setDeliveryFee($deliveryFee);
369
                                            } else {
370
                                                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
371
                                            }
372
                                        }
373
                                    }
374
375
                                    $Product->addProductClass($ProductClass);
376
                                }
377
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
378
                            }
379
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
380
                        }
381
382
383
                        if ($this->hasErrors()) {
384
                            return $this->render($app, $form, $headers, $this->productTwig);
385
                        }
386
387
                        $this->em->persist($Product);
388
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
389
                    }
390
391
                    $this->em->flush();
392
                    $this->em->getConnection()->commit();
393
394
                    $app->addSuccess('admin.product.csv_import.save.complete', 'admin');
395
                }
396
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
397
            }
398
        }
399
400
        return $this->render($app, $form, $headers, $this->productTwig);
401
    }
402
403
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
404
     * カテゴリ登録CSVアップロード
405
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
406
    public function csvCategory(Application $app, Request $request)
407
    {
408
409
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
410
411
        $headers = $this->getCategoryCsvHeader();
412
413
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
414
415
            $form->handleRequest($request);
416
417
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
418
419
                $formFile = $form['import_file']->getData();
420
421
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
422
423
                    $data = $this->getImportData($app, $formFile);
424 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...
425
                        $this->addErrors('CSVのフォーマットが一致しません。');
426
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
427
                    }
428
429
                    $keys = array_keys($headers);
430
                    $columnHeaders = $data->getColumnHeaders();
431
                    if ($keys !== $columnHeaders) {
432
                        $this->addErrors('CSVのフォーマットが一致しません。');
433
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
434
                    }
435
436
                    $size = count($data);
437
                    if ($size < 1) {
438
                        $this->addErrors('CSVデータが存在しません。');
439
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
440
                    }
441
442
                    $headerSize = count($keys);
443
444
                    $this->em = $app['orm.em'];
445
                    $this->em->getConfiguration()->setSQLLogger(null);
446
447
                    $this->em->getConnection()->beginTransaction();
448
449
                    // CSVファイルの登録処理
450
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
451
452
                        if ($headerSize != count($row)) {
453
                            $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
454
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
455
                        }
456
457
                        if ($row['カテゴリID'] == '') {
458
                            $Category = new Category();
459
                        } else {
460 View Code Duplication
                            if (!is_numeric($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...
461
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。');
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
                            $Category = $app['eccube.repository.category']->find($row['カテゴリID']);
465 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...
466
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
467
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
468
                            }
469 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...
470
                                $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...
471
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
472
                            }
473
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
474
                        }
475
476 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...
477
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリ名が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
478
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
479
                        } else {
480
                            $Category->setName(Str::trimAll($row['カテゴリ名']));
481
                        }
482
483
                        if ($row['親カテゴリID'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
484
485 View Code Duplication
                            if (!is_numeric($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...
486
                                $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
487
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
488
                            }
489
490
                            $ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']);
491 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...
492
                                $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
493
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
494
                            }
495
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
496
                        } else {
497
                            $ParentCategory = null;
498
                        }
499
500
                        $Category->setParent($ParentCategory);
501
                        if ($ParentCategory) {
502
                            $Category->setLevel($ParentCategory->getLevel() + 1);
503
                        } else {
504
                            $Category->setLevel(1);
505
                        }
506
507 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...
508
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリが最大レベルを超えているため設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
509
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
510
                        }
511
512
                        $status = $app['eccube.repository.category']->save($Category);
513
514
                        if (!$status) {
515
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリが設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
516
                        }
517
518
                        if ($this->hasErrors()) {
519
                            return $this->render($app, $form, $headers, $this->categoryTwig);
520
                        }
521
522
                        $this->em->persist($Category);
523
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
524
                    }
525
526
                    $this->em->flush();
527
                    $this->em->getConnection()->commit();
528
529
                    $app->addSuccess('admin.category.csv_import.save.complete', 'admin');
530
                }
531
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
532
            }
533
        }
534
535
        return $this->render($app, $form, $headers, $this->categoryTwig);
536
    }
537
538
539
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$type" missing
Loading history...
540
     * アップロード用CSV雛形ファイルダウンロード
541
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
542
    public function csvTemplate(Application $app, Request $request, $type)
543
    {
544
        set_time_limit(0);
545
546
        $response = new StreamedResponse();
547
548
        if ($type == 'product') {
549
            $headers = $this->getProductCsvHeader();
550
            $filename = 'product.csv';
551
        } else if ($type == 'category') {
552
            $headers = $this->getCategoryCsvHeader();
553
            $filename = 'category.csv';
554
        } else {
555
            throw new NotFoundHttpException();
556
        }
557
558
        $response->setCallback(function () use ($app, $request, $headers) {
559
560
            // ヘッダ行の出力
561
            $row = array();
562
            foreach ($headers as $key => $value) {
563
                $row[] = mb_convert_encoding($key, $app['config']['csv_export_encoding'], 'UTF-8');
564
            }
565
566
            $fp = fopen('php://output', 'w');
567
            fputcsv($fp, $row, $app['config']['csv_export_separator']);
568
            fclose($fp);
569
570
        });
571
572
        $response->headers->set('Content-Type', 'application/octet-stream');
573
        $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...
574
        $response->send();
575
576
        return $response;
577
    }
578
579
580
    /**
581
     * 登録、更新時のエラー画面表示
582
     *
583
     */
584
    protected function render($app, $form, $headers, $twig)
585
    {
586
587
        if ($this->hasErrors()) {
588
            if ($this->em) {
589
                $this->em->getConnection()->rollback();
590
            }
591
        }
592
593
        if (!empty($this->fileName)) {
594
            try {
595
                $fs = new Filesystem();
596
                $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...
597
            } catch (\Exception $e) {
598
                // エラーが発生しても無視する
599
            }
600
        }
601
602
        return $app->render($twig, array(
603
            'form' => $form->createView(),
604
            'headers' => $headers,
605
            'errors' => $this->errors,
606
        ));
607
    }
608
609
610
    /**
611
     * アップロードされたCSVファイルの行ごとの処理
612
     *
613
     * @param $formFile
614
     * @return CsvImportService
615
     */
616
    protected function getImportData($app, $formFile)
617
    {
618
        // アップロードされたCSVファイルを一時ディレクトリに保存
619
        $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...
620
        $formFile->move($app['config']['csv_temp_realdir'], $this->fileName);
621
622
        $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...
623
        // アップロードされたファイルがUTF-8以外は文字コード変換を行う
624
        $encode = Str::characterEncoding(substr($file, 0, 6));
625
        if ($encode != 'UTF-8') {
626
            $file = mb_convert_encoding($file, 'UTF-8', $encode);
627
        }
628
        $file = Str::convertLineFeed($file);
629
630
        $tmp = tmpfile();
631
        fwrite($tmp, $file);
632
        rewind($tmp);
633
        $meta = stream_get_meta_data($tmp);
634
        $file = new \SplFileObject($meta['uri']);
635
636
        set_time_limit(0);
637
638
        // アップロードされたCSVファイルを行ごとに取得
639
        $data = new CsvImportService($file, $app['config']['csv_import_delimiter'], $app['config']['csv_import_enclosure']);
640
641
        $ret = $data->setHeaderRowNumber(0);
642
643
        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 643 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...
644
    }
645
646
647
    /**
648
     * 商品画像の削除、登録
649
     */
650
    protected function createProductImage($row, Product $Product)
651
    {
652
        if ($row['商品画像'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
653
654
            // 画像の削除
655
            $ProductImages = $Product->getProductImage();
656
            foreach ($ProductImages as $ProductImage) {
657
                $Product->removeProductImage($ProductImage);
658
                $this->em->remove($ProductImage);
659
            }
660
661
            // 画像の登録
662
            $images = explode(',', $row['商品画像']);
663
            $rank = 1;
664
            foreach ($images as $image) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
665
666
                $ProductImage = new ProductImage();
667
                $ProductImage->setFileName(Str::trimAll($image));
668
                $ProductImage->setProduct($Product);
669
                $ProductImage->setRank($rank);
670
671
                $Product->addProductImage($ProductImage);
672
                $rank++;
673
                $this->em->persist($ProductImage);
674
            }
675
        }
676
    }
677
678
679
    /**
680
     * 商品カテゴリの削除、登録
681
     */
682
    protected function createProductCategory($row, Product $Product, $app, $data)
683
    {
684
        if ($row['商品カテゴリ(ID)'] != '') {
685
            // カテゴリの削除
686
            $ProductCategories = $Product->getProductCategories();
687
            foreach ($ProductCategories as $ProductCategory) {
688
                $Product->removeProductCategory($ProductCategory);
689
                $this->em->remove($ProductCategory);
690
                $this->em->flush($ProductCategory);
691
            }
692
693
            // カテゴリの登録
694
            $categories = explode(',', $row['商品カテゴリ(ID)']);
695
            $rank = 1;
696
            foreach ($categories as $category) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
697
698
                if (is_numeric($category)) {
699
                    $Category = $app['eccube.repository.category']->find($category);
700
                    if (!$Category) {
701
                        $this->addErrors(($data->key() + 1) . '行目の商品カテゴリ(ID)「' . $category . '」が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
702
                    } else {
703
                        $ProductCategory = new ProductCategory();
704
                        $ProductCategory->setProductId($Product->getId());
705
                        $ProductCategory->setCategoryId($Category->getId());
706
                        $ProductCategory->setProduct($Product);
707
                        $ProductCategory->setCategory($Category);
708
                        $ProductCategory->setRank($rank);
709
                        $Product->addProductCategory($ProductCategory);
710
                        $rank++;
711
                        $this->em->persist($ProductCategory);
712
                    }
713
                } else {
714
                    $this->addErrors(($data->key() + 1) . '行目の商品カテゴリ(ID)「' . $category . '」が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
715
                }
716
            }
717
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
718
        }
719
    }
720
721
722
    /**
723
     * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成
724
     */
725
    protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null)
726
    {
727
        // 規格分類1、規格分類2がnullとなる商品を作成
728
729
        $ProductClass = new ProductClass();
730
        $ProductClass->setProduct($Product);
731
732
733 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...
734
            $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
735
        } else {
736
            if (is_numeric($row['商品種別(ID)'])) {
737
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
738
                if (!$ProductType) {
739
                    $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
740
                } else {
741
                    $ProductClass->setProductType($ProductType);
742
                }
743
            } else {
744
                $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
745
            }
746
        }
747
748
        $ProductClass->setClassCategory1($ClassCategory1);
749
        $ProductClass->setClassCategory2($ClassCategory2);
750
751
        if ($row['発送日目安(ID)'] != '') {
752
            if (is_numeric($row['発送日目安(ID)'])) {
753
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
754
                if (!$DeliveryDate) {
755
                    $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
756
                } else {
757
                    $ProductClass->setDeliveryDate($DeliveryDate);
758
                }
759
            } else {
760
                $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
761
            }
762
        }
763
764 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...
765
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
766
        } else {
767
            $ProductClass->setCode(null);
768
        }
769
770 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...
771
            $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
772
        } else {
773
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
774
                $ProductClass->setStockUnlimited(Constant::DISABLED);
775
                // 在庫数が設定されていなければエラー
776
                if ($row['在庫数'] == '') {
777
                    $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
778
                } else {
779
                    $stock = str_replace(',', '', $row['在庫数']);
780
                    if (is_numeric($stock) && $stock >= 0) {
781
                        $ProductClass->setStock($stock);
782
                    } else {
783
                        $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
784
                    }
785
                }
786
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
787
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
788
                $ProductClass->setStockUnlimited(Constant::ENABLED);
789
                $ProductClass->setStock(null);
790
            } else {
791
                $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
792
            }
793
        }
794
795
        if ($row['販売制限数'] != '') {
796
            $saleLimit = str_replace(',', '', $row['販売制限数']);
797
            if (is_numeric($saleLimit) && $saleLimit >= 0) {
798
                $ProductClass->setSaleLimit($saleLimit);
799
            } else {
800
                $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
801
            }
802
        }
803
804
        if ($row['通常価格'] != '') {
805
            $price01 = str_replace(',', '', $row['通常価格']);
806
            if (is_numeric($price01) && $price01 >= 0) {
807
                $ProductClass->setPrice01($price01);
808
            } else {
809
                $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
810
            }
811
        }
812
813
        if ($row['販売価格'] == '') {
814
            $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
815
        } else {
816
            $price02 = str_replace(',', '', $row['販売価格']);
817
            if (is_numeric($price02) && $price02 >= 0) {
818
                $ProductClass->setPrice02($price02);
819
            } else {
820
                $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
821
            }
822
        }
823
824
        if ($row['送料'] != '') {
825
            $delivery_fee = str_replace(',', '', $row['送料']);
826
            if (is_numeric($delivery_fee) && $delivery_fee >= 0) {
827
                $ProductClass->setDeliveryFee($delivery_fee);
828
            } else {
829
                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
830
            }
831
        }
832
833 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...
834
            $ProductClass->setDelFlg(Constant::DISABLED);
835
        } else {
836
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
837
                $ProductClass->setDelFlg($row['商品規格削除フラグ']);
838
            } else {
839
                $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
840
            }
841
        }
842
843
        $Product->addProductClass($ProductClass);
844
        $ProductStock = new ProductStock();
845
        $ProductClass->setProductStock($ProductStock);
846
        $ProductStock->setProductClass($ProductClass);
847
848
        if (!$ProductClass->getStockUnlimited()) {
849
            $ProductStock->setStock($ProductClass->getStock());
850
        } else {
851
            // 在庫無制限時はnullを設定
852
            $ProductStock->setStock(null);
853
        }
854
855
        $this->em->persist($ProductClass);
856
        $this->em->persist($ProductStock);
857
858
        return $ProductClass;
859
860
    }
861
862
863
    /**
864
     * 商品規格情報を更新
865
     */
866
    protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data)
867
    {
868
869
        $ProductClass->setProduct($Product);
870
871 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...
872
            $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
873
        } else {
874
            if (is_numeric($row['商品種別(ID)'])) {
875
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
876
                if (!$ProductType) {
877
                    $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
878
                } else {
879
                    $ProductClass->setProductType($ProductType);
880
                }
881
            } else {
882
                $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
883
            }
884
        }
885
886
        // 規格分類1、2をそれぞれセットし作成
887
        if ($row['規格分類1(ID)'] != '') {
888
            if (is_numeric($row['規格分類1(ID)'])) {
889
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
890
                if (!$ClassCategory) {
891
                    $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...
892
                } else {
893
                    $ProductClass->setClassCategory1($ClassCategory);
894
                }
895
            } else {
896
                $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...
897
            }
898
        }
899
900
        if ($row['規格分類2(ID)'] != '') {
901
            if (is_numeric($row['規格分類2(ID)'])) {
902
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
903
                if (!$ClassCategory) {
904
                    $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...
905
                } else {
906
                    $ProductClass->setClassCategory2($ClassCategory);
907
                }
908
            } else {
909
                $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...
910
            }
911
        }
912
913
        if ($row['発送日目安(ID)'] != '') {
914
            if (is_numeric($row['発送日目安(ID)'])) {
915
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
916
                if (!$DeliveryDate) {
917
                    $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
918
                } else {
919
                    $ProductClass->setDeliveryDate($DeliveryDate);
920
                }
921
            } else {
922
                $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
923
            }
924
        }
925
926 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...
927
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
928
        } else {
929
            $ProductClass->setCode(null);
930
        }
931
932 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...
933
            $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
934
        } else {
935
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
936
                $ProductClass->setStockUnlimited(Constant::DISABLED);
937
                // 在庫数が設定されていなければエラー
938
                if ($row['在庫数'] == '') {
939
                    $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
940
                } else {
941
                    $stock = str_replace(',', '', $row['在庫数']);
942
                    if (is_numeric($stock) && $stock >= 0) {
943
                        $ProductClass->setStock($row['在庫数']);
944
                    } else {
945
                        $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
946
                    }
947
                }
948
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
949
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
950
                $ProductClass->setStockUnlimited(Constant::ENABLED);
951
                $ProductClass->setStock(null);
952
            } else {
953
                $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
954
            }
955
        }
956
957
        if ($row['販売制限数'] != '') {
958
            $saleLimit = str_replace(',', '', $row['販売制限数']);
959
            if (is_numeric($saleLimit) && $saleLimit >= 0) {
960
                $ProductClass->setSaleLimit($saleLimit);
961
            } else {
962
                $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
963
            }
964
        }
965
966
        if ($row['通常価格'] != '') {
967
            $price01 = str_replace(',', '', $row['通常価格']);
968
            if (is_numeric($price01) && $price01 >= 0) {
969
                $ProductClass->setPrice01($price01);
970
            } else {
971
                $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
972
            }
973
        }
974
975
        if ($row['販売価格'] == '') {
976
            $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
977
        } else {
978
            $price02 = str_replace(',', '', $row['販売価格']);
979
            if (is_numeric($price02) && $price02 >= 0) {
980
                $ProductClass->setPrice02($price02);
981
            } else {
982
                $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
983
            }
984
        }
985
986 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...
987
            $ProductClass->setDelFlg(Constant::DISABLED);
988
        } else {
989
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
990
                $ProductClass->setDelFlg($row['商品規格削除フラグ']);
991
            } else {
992
                $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
993
            }
994
        }
995
996
        $ProductStock = $ProductClass->getProductStock();
997
998
        if (!$ProductClass->getStockUnlimited()) {
999
            $ProductStock->setStock($ProductClass->getStock());
1000
        } else {
1001
            // 在庫無制限時はnullを設定
1002
            $ProductStock->setStock(null);
1003
        }
1004
1005
        return $ProductClass;
1006
    }
1007
1008
    /**
1009
     * 登録、更新時のエラー画面表示
1010
     *
1011
     */
1012
    protected function addErrors($message)
1013
    {
1014
        $e = new CsvImportException($message);
1015
        $this->errors[] = $e;
1016
    }
1017
1018
    /**
1019
     * @return array
1020
     */
1021
    protected function getErrors()
1022
    {
1023
        return $this->errors;
1024
    }
1025
1026
    /**
1027
     *
1028
     * @return boolean
1029
     */
1030
    protected function hasErrors()
1031
    {
1032
        return count($this->getErrors()) > 0;
1033
    }
1034
1035
    /**
1036
     * 商品登録CSVヘッダー定義
1037
     */
1038
    private function getProductCsvHeader()
1039
    {
1040
        return array(
1041
            '商品ID' => 'id',
1042
            '公開ステータス(ID)' => 'status',
1043
            '商品名' => 'name',
1044
            'ショップ用メモ欄' => 'note',
1045
            '商品説明(一覧)' => 'description_list',
1046
            '商品説明(詳細)' => 'description_detail',
1047
            '検索ワード' => 'search_word',
1048
            'フリーエリア' => 'free_area',
1049
            '商品削除フラグ' => 'product_del_flg',
1050
            '商品画像' => 'product_image',
1051
            '商品カテゴリ(ID)' => 'product_category',
1052
            '商品種別(ID)' => 'product_type',
1053
            '規格分類1(ID)' => 'class_category1',
1054
            '規格分類2(ID)' => 'class_category2',
1055
            '発送日目安(ID)' => 'deliveryFee',
1056
            '商品コード' => 'product_code',
1057
            '在庫数' => 'stock',
1058
            '在庫数無制限フラグ' => 'stock_unlimited',
1059
            '販売制限数' => 'sale_limit',
1060
            '通常価格' => 'price01',
1061
            '販売価格' => 'price02',
1062
            '送料' => 'delivery_fee',
1063
            '商品規格削除フラグ' => 'product_class_del_flg',
1064
        );
1065
    }
1066
1067
1068
    /**
1069
     * カテゴリCSVヘッダー定義
1070
     */
1071
    private function getCategoryCsvHeader()
1072
    {
1073
        return array(
1074
            'カテゴリID' => 'id',
1075
            'カテゴリ名' => 'category_name',
1076
            '親カテゴリID' => 'parent_category_id',
1077
        );
1078
    }
1079
}
1080