ProductClassController   D
last analyzed

Complexity

Total Complexity 80

Size/Duplication

Total Lines 716
Duplicated Lines 15.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 80
lcom 1
cbo 7
dl 110
loc 716
rs 4.4444
c 2
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
D index() 7 217 21
D edit() 52 239 30
B render() 0 44 4
B createProductClasses() 0 37 6
A newProductClass() 0 8 1
A getProductClassesOriginal() 0 7 1
A getProductClassesExcludeNonClass() 0 9 2
B setDefualtProductClass() 19 34 6
B insertProductClass() 32 47 6
A isValiedCategory() 0 10 3

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 ProductClassController 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 ProductClassController, 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\ClassName;
30
use Eccube\Entity\Product;
31
use Eccube\Entity\ProductClass;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Symfony\Component\Form\FormError;
35
use Symfony\Component\HttpFoundation\Request;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37
use Symfony\Component\Validator\Constraints as Assert;
38
39
40
class ProductClassController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
41
{
42
43
    /**
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 "$id" missing
Loading history...
44
     * 商品規格が登録されていなければ新規登録、登録されていれば更新画面を表示する
45
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
46
    public function index(Application $app, Request $request, $id)
47
    {
48
49
        /** @var $Product \Eccube\Entity\Product */
50
        $Product = $app['eccube.repository.product']->find($id);
51
        $hasClassCategoryFlg = false;
52
53
        if (!$Product) {
54
            throw new NotFoundHttpException();
55
        }
56
57
58
        // 商品規格情報が存在しなければ新規登録させる
59
        if (!$Product->hasProductClass()) {
60
            // 登録画面を表示
61
62
            $builder = $app['form.factory']->createBuilder();
63
64
            $builder
65
                ->add('class_name1', 'entity', array(
66
                    'class' => 'Eccube\Entity\ClassName',
67
                    'property' => 'name',
68
                    'empty_value' => '規格1を選択',
69
                    'constraints' => array(
70
                        new Assert\NotBlank(),
71
                    ),
72
                ))
73
                ->add('class_name2', 'entity', array(
74
                    'class' => 'Eccube\Entity\ClassName',
75
                    'property' => 'name',
76
                    'empty_value' => '規格2を選択',
77
                    'required' => false,
78
                ));
79
80
            $event = new EventArgs(
81
                array(
82
                    'builder' => $builder,
83
                    'Product' => $Product,
84
                ),
85
                $request
86
            );
87
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_INITIALIZE, $event);
88
89
            $form = $builder->getForm();
90
91
            $productClassForm = null;
92
93
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
94
95
                $form->handleRequest($request);
96
97
                if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
98
99
                    $data = $form->getData();
100
101
                    $ClassName1 = $data['class_name1'];
102
                    $ClassName2 = $data['class_name2'];
103
104
                    // 各規格が選択されている際に、分類を保有しているか確認
105
                    $class1Valied = $this->isValiedCategory($ClassName1);
106
                    $class2Valied = $this->isValiedCategory($ClassName2);
107
108
                    // 規格が選択されていないか、選択された状態で分類が保有されていれば、画面表示
109
                    if($class1Valied && $class2Valied){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
110
                        $hasClassCategoryFlg = true;
111
                    }
112
113
                    if (!is_null($ClassName2) && $ClassName1->getId() == $ClassName2->getId()) {
114
                        // 規格1と規格2が同じ値はエラー
115
116
                        $form['class_name2']->addError(new FormError('規格1と規格2は、同じ値を使用できません。'));
117
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
118
                    } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
119
120
                        // 規格分類が設定されていない商品規格を取得
121
                        $orgProductClasses = $Product->getProductClasses();
122
                        $sourceProduct = $orgProductClasses[0];
123
124
                        // 規格分類が組み合わされた商品規格を取得
125
                        $ProductClasses = $this->createProductClasses($app, $Product, $ClassName1, $ClassName2);
126
127
                        // 組み合わされた商品規格にデフォルト値をセット
128
                        foreach ($ProductClasses as $productClass) {
129
                            $this->setDefualtProductClass($app, $productClass, $sourceProduct);
130
                        }
131
132
133
                        $builder = $app['form.factory']->createBuilder();
134
135
                        $builder
136
                            ->add('product_classes', 'collection', array(
137
                                'type' => 'admin_product_class',
138
                                'allow_add' => true,
139
                                'allow_delete' => true,
140
                                'data' => $ProductClasses,
141
                             ));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 29.
Loading history...
142
143
                        $event = new EventArgs(
144
                            array(
145
                                'builder' => $builder,
146
                                'Product' => $Product,
147
                                'ProductClasses' => $ProductClasses,
148
                            ),
149
                            $request
150
                        );
151
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, $event);
152
153
                        $productClassForm = $builder->getForm()->createView();
154
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
155
                    }
156
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
157
                }
158
            }
159
160
            return $app->render('Product/product_class.twig', array(
161
                'form' => $form->createView(),
162
                'classForm' => $productClassForm,
163
                'Product' => $Product,
164
                'not_product_class' => true,
165
                'error' => null,
166
                'has_class_category_flg' => $hasClassCategoryFlg,
167
            ));
168
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
169
        } else {
170
            // 既に商品規格が登録されている場合、商品規格画面を表示する
171
172
            // 既に登録されている商品規格を取得
173
            $ProductClasses = $this->getProductClassesExcludeNonClass($Product);
174
175
            // 設定されている規格分類1、2を取得(商品規格の規格分類には必ず同じ値がセットされている)
176
            $ProductClass = $ProductClasses[0];
177
            $ClassName1 = $ProductClass->getClassCategory1()->getClassName();
178
            $ClassName2 = null;
179
            if (!is_null($ProductClass->getClassCategory2())) {
180
                $ClassName2 = $ProductClass->getClassCategory2()->getClassName();
181
            }
182
183
            // 規格分類が組み合わされた空の商品規格を取得
184
            $createProductClasses = $this->createProductClasses($app, $Product, $ClassName1, $ClassName2);
185
186
187
            $mergeProductClasses = array();
188
189
            // 商品税率が設定されている場合、商品税率を項目に設定
190
            $BaseInfo = $app['eccube.repository.base_info']->get();
191 View Code Duplication
            if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
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...
192
                foreach ($ProductClasses as $class) {
193
                    if ($class->getTaxRule() && !$class->getTaxRule()->getDelFlg()) {
194
                        $class->setTaxRate($class->getTaxRule()->getTaxRate());
195
                    }
196
                }
197
            }
198
199
200
            // 登録済み商品規格と空の商品規格をマージ
201
            $flag = false;
202
            foreach ($createProductClasses as $createProductClass) {
203
                // 既に登録済みの商品規格にチェックボックスを設定
204
                foreach ($ProductClasses as $productClass) {
205
                    if ($productClass->getClassCategory1() == $createProductClass->getClassCategory1() &&
206
                            $productClass->getClassCategory2() == $createProductClass->getClassCategory2()) {
207
                                // チェックボックスを追加
208
                                $productClass->setAdd(true);
209
                                $flag = true;
210
                                break;
211
                    }
212
                }
213
214
                if (!$flag) {
215
                    $mergeProductClasses[] = $createProductClass;
216
                }
217
218
                $flag = false;
219
            }
220
221
            // 登録済み商品規格と空の商品規格をマージ
222
            foreach ($mergeProductClasses as $mergeProductClass) {
223
                // 空の商品規格にデフォルト値を設定
224
                $this->setDefualtProductClass($app, $mergeProductClass, $ProductClass);
225
                $ProductClasses->add($mergeProductClass);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $ProductClasses (of type array<integer,object<Eccube\Entity\ProductClass>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
226
            }
227
228
            $builder = $app['form.factory']->createBuilder();
229
230
            $builder
231
                ->add('product_classes', 'collection', array(
232
                    'type' => 'admin_product_class',
233
                    'allow_add' => true,
234
                    'allow_delete' => true,
235
                    'data' => $ProductClasses,
236
                ));
237
238
            $event = new EventArgs(
239
                array(
240
                    'builder' => $builder,
241
                    'Product' => $Product,
242
                    'ProductClasses' => $ProductClasses,
243
                ),
244
                $request
245
            );
246
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, $event);
247
248
            $productClassForm = $builder->getForm()->createView();
249
250
            return $app->render('Product/product_class.twig', array(
251
                'classForm' => $productClassForm,
252
                'Product' => $Product,
253
                'class_name1' => $ClassName1,
254
                'class_name2' => $ClassName2,
255
                'not_product_class' => false,
256
                'error' => null,
257
                'has_class_category_flg' => true,
258
            ));
259
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
260
        }
261
262
    }
263
264
265
    /**
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 "$id" missing
Loading history...
266
     * 商品規格の登録、更新、削除を行う
267
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
268
    public function edit(Application $app, Request $request, $id)
269
    {
270
271
        /** @var $Product \Eccube\Entity\Product */
272
        $Product = $app['eccube.repository.product']->find($id);
273
274
        if (!$Product) {
275
            throw new NotFoundHttpException();
276
        }
277
278
        $builder = $app['form.factory']->createBuilder();
279
        $builder
280
                ->add('product_classes', 'collection', array(
281
                    'type' => 'admin_product_class',
282
                    'allow_add' => true,
283
                    'allow_delete' => true,
284
            ));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
It seems like the identation of this line is off (expected at least 16 spaces, but found 12).
Loading history...
285
286
        $event = new EventArgs(
287
            array(
288
                'builder' => $builder,
289
                'Product' => $Product,
290
            ),
291
            $request
292
        );
293
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_INITIALIZE, $event);
294
295
        $form = $builder->getForm();
296
297
        $ProductClasses = $this->getProductClassesExcludeNonClass($Product);
298
299
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
300
301
            $form->handleRequest($request);
302
303
            switch ($request->get('mode')) {
304
                case 'edit':
305
                    // 新規登録
306
307 View Code Duplication
                    if (count($ProductClasses) > 0) {
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...
308
                        // 既に登録されていれば最初の画面に戻す
309
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
310
                    }
311
312
                    $addProductClasses = array();
313
314
                    $tmpProductClass = null;
315 View Code Duplication
                    foreach ($form->get('product_classes') as $formData) {
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...
316
                        // 追加対象の行をvalidate
317
                        $ProductClass = $formData->getData();
318
319
                        if ($ProductClass->getAdd()) {
320
                            if ($formData->isValid()) {
321
                                $addProductClasses[] = $ProductClass;
322
                            } else {
323
                                // 対象行のエラー
324
                                return $this->render($app, $Product, $ProductClass, true, $form);
325
                            }
326
                        }
327
                        $tmpProductClass = $ProductClass;
328
                    }
329
330 View Code Duplication
                    if (count($addProductClasses) == 0) {
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...
331
                        // 対象がなければエラー
332
                        $error = array('message' => '商品規格が選択されていません。');
333
                        return $this->render($app, $Product, $tmpProductClass, true, $form, $error);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
334
                    }
335
336
                    // 選択された商品規格を登録
337
                    $this->insertProductClass($app, $Product, $addProductClasses);
338
339
                    // デフォルトの商品規格を更新
340
                    $defaultProductClass = $app['eccube.repository.product_class']
341
                            ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null));
342
343
                    $defaultProductClass->setDelFlg(Constant::ENABLED);
344
345
                    $event = new EventArgs(
346
                        array(
347
                            'form' => $form,
348
                            'Product' => $Product,
349
                            'defaultProductClass' => $defaultProductClass,
350
                        ),
351
                        $request
352
                    );
353
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_COMPLETE, $event);
354
355
                    $app['orm.em']->flush();
356
357
                    $app->addSuccess('admin.product.product_class.save.complete', 'admin');
358
359
                    break;
360
                case 'update':
361
                    // 更新
362
363 View Code Duplication
                    if (count($ProductClasses) == 0) {
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...
364
                        // 商品規格が0件であれば最初の画面に戻す
365
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
366
                    }
367
368
                    $checkProductClasses = array();
369
                    $removeProductClasses = array();
370
371
                    $tempProductClass = null;
372 View Code Duplication
                    foreach ($form->get('product_classes') as $formData) {
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...
373
                        // 追加対象の行をvalidate
374
                        $ProductClass = $formData->getData();
375
376
                        if ($ProductClass->getAdd()) {
377
                            if ($formData->isValid()) {
378
                                $checkProductClasses[] = $ProductClass;
379
                            } else {
380
                                return $this->render($app, $Product, $ProductClass, false, $form);
381
                            }
382
                        } else {
383
                            // 削除対象の行
384
                            $removeProductClasses[] = $ProductClass;
385
                        }
386
                        $tempProductClass = $ProductClass;
387
                    }
388
389 View Code Duplication
                    if (count($checkProductClasses) == 0) {
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...
390
                        // 対象がなければエラー
391
                        $error = array('message' => '商品規格が選択されていません。');
392
                        return $this->render($app, $Product, $tempProductClass, false, $form, $error);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
393
                    }
394
395
396
                    // 登録対象と更新対象の行か判断する
397
                    $addProductClasses = array();
398
                    $updateProductClasses = array();
399
                    foreach ($checkProductClasses as $cp) {
400
                        $flag = false;
401
                        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
402
                        // 既に登録済みの商品規格か確認
403
                        foreach ($ProductClasses as $productClass) {
404
                            if ($productClass->getProduct()->getId() == $id &&
405
                                    $productClass->getClassCategory1() == $cp->getClassCategory1() &&
406
                                    $productClass->getClassCategory2() == $cp->getClassCategory2()) {
407
                                $updateProductClasses[] = $cp;
408
409
                                // 商品情報
410
                                $cp->setProduct($Product);
411
                                // 商品在庫
412
                                $productStock = $productClass->getProductStock();
413
                                if (!$cp->getStockUnlimited()) {
414
                                    $productStock->setStock($cp->getStock());
415
                                } else {
416
                                    $productStock->setStock(null);
417
                                }
418
                                $this->setDefualtProductClass($app, $productClass, $cp);
419
                                $flag = true;
420
                                break;
421
                            }
422
                        }
423
                        if (!$flag) {
424
                            $addProductClasses[] = $cp;
425
                        }
426
                    }
427
428
                    foreach ($removeProductClasses as $rc) {
429
                        // 登録されている商品規格に削除フラグをセット
430
                        foreach ($ProductClasses as $productClass) {
431
                            if ($productClass->getProduct()->getId() == $id &&
432
                                    $productClass->getClassCategory1() == $rc->getClassCategory1() &&
433
                                    $productClass->getClassCategory2() == $rc->getClassCategory2()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
434
435
                                $productClass->setDelFlg(Constant::ENABLED);
436
                                break;
437
                            }
438
                        }
439
                    }
440
441
                    // 選択された商品規格を登録
442
                    $this->insertProductClass($app, $Product, $addProductClasses);
443
444
                    $event = new EventArgs(
445
                        array(
446
                            'form' => $form,
447
                            'Product' => $Product,
448
                            'updateProductClasses' => $updateProductClasses,
449
                        ),
450
                        $request
451
                    );
452
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE, $event);
453
454
                    $app['orm.em']->flush();
455
456
                    $app->addSuccess('admin.product.product_class.update.complete', 'admin');
457
458
                    break;
459
                case 'delete':
460
                    // 削除
461
462 View Code Duplication
                    if (count($ProductClasses) == 0) {
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...
463
                        // 既に商品が削除されていれば元の画面に戻す
464
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
465
                    }
466
467
                    foreach ($ProductClasses as $ProductClass) {
468
                        // 登録されている商品規格に削除フラグをセット
469
                        $ProductClass->setDelFlg(Constant::ENABLED);
470
                    }
471
472
                    /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
473
                    $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
474
                    $softDeleteFilter->setExcludes(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
475
                        'Eccube\Entity\ProductClass'
476
                    ));
477
478
                    // デフォルトの商品規格を更新
479
                    $defaultProductClass = $app['eccube.repository.product_class']
480
                            ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null, 'del_flg' => Constant::ENABLED));
481
482
                    $defaultProductClass->setDelFlg(Constant::DISABLED);
483
484
                    $event = new EventArgs(
485
                        array(
486
                            'form' => $form,
487
                            'Product' => $Product,
488
                            'defaultProductClass' => $defaultProductClass,
489
                        ),
490
                        $request
491
                    );
492
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_DELETE, $event);
493
494
                    $app['orm.em']->flush();
495
496
                    $app->addSuccess('admin.product.product_class.delete.complete', 'admin');
497
498
                    break;
499
                default:
500
                    break;
501
            }
502
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
503
        }
504
505
        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
506
    }
507
508
509
510
    /**
511
     * 登録、更新時のエラー画面表示
512
     *
513
     */
514
    protected function render($app, $Product, $ProductClass, $not_product_class, $classForm, $error = null)
515
    {
516
517
        $ClassName1 = null;
518
        $ClassName2 = null;
519
        // 規格を取得
520
        if (isset($ProductClass)) {
521
            $ClassCategory1 = $ProductClass->getClassCategory1();
522
            if ($ClassCategory1) {
523
                $ClassName1 = $ClassCategory1->getClassName();
524
            }
525
            $ClassCategory2 = $ProductClass->getClassCategory2();
526
            if ($ClassCategory2) {
527
                $ClassName2 = $ClassCategory2->getClassName();
528
            }
529
        }
530
531
        $form = $app->form()
532
            ->add('class_name1', 'entity', array(
533
                'class' => 'Eccube\Entity\ClassName',
534
                'property' => 'name',
535
                'empty_value' => '規格1を選択',
536
                'data' => $ClassName1,
537
            ))
538
            ->add('class_name2', 'entity', array(
539
                'class' => 'Eccube\Entity\ClassName',
540
                'property' => 'name',
541
                'empty_value' => '規格2を選択',
542
                'data' => $ClassName2,
543
            ))
544
            ->getForm();
545
546
547
        return $app->render('Product/product_class.twig', array(
548
            'form' => $form->createView(),
549
            'classForm' => $classForm->createView(),
550
            'Product' => $Product,
551
            'class_name1' => $ClassName1,
552
            'class_name2' => $ClassName2,
553
            'not_product_class' => $not_product_class,
554
            'error' => $error,
555
            'has_class_category_flg' => true,
556
        ));
557
    }
558
559
560
    /**
561
     * 規格1と規格2を組み合わせた商品規格を作成
562
     */
563
    private function createProductClasses($app, Product $Product, ClassName $ClassName1 = null, ClassName $ClassName2 = null)
564
    {
565
566
        $ClassCategories1 = array();
567
        if ($ClassName1) {
568
            $ClassCategories1 = $app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName1));
569
        }
570
571
        $ClassCategories2 = array();
572
        if ($ClassName2) {
573
            $ClassCategories2 = $app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName2));
574
        }
575
576
        $ProductClasses = array();
577
        foreach ($ClassCategories1 as $ClassCategory1) {
578
            if ($ClassCategories2) {
579
                foreach ($ClassCategories2 as $ClassCategory2) {
580
                    $ProductClass = $this->newProductClass($app);
581
                    $ProductClass->setProduct($Product);
582
                    $ProductClass->setClassCategory1($ClassCategory1);
583
                    $ProductClass->setClassCategory2($ClassCategory2);
584
                    $ProductClass->setTaxRate(null);
585
                    $ProductClass->setDelFlg(Constant::DISABLED);
586
                    $ProductClasses[] = $ProductClass;
587
                }
588
            } else {
589
                $ProductClass = $this->newProductClass($app);
590
                $ProductClass->setProduct($Product);
591
                $ProductClass->setClassCategory1($ClassCategory1);
592
                $ProductClass->setTaxRate(null);
593
                $ProductClass->setDelFlg(Constant::DISABLED);
594
                $ProductClasses[] = $ProductClass;
595
            }
596
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
597
        }
598
        return $ProductClasses;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
599
    }
600
601
    /**
602
     * 新しい商品規格を作成
603
     */
604
    private function newProductClass(Application $app)
605
    {
606
        $ProductType = $app['eccube.repository.master.product_type']->find($app['config']['product_type_normal']);
607
608
        $ProductClass = new ProductClass();
609
        $ProductClass->setProductType($ProductType);
610
        return $ProductClass;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
611
    }
612
613
    /**
614
     * 商品規格のコピーを取得.
615
     *
616
     * @see http://symfony.com/doc/current/cookbook/form/form_collections.html
617
     * @param Product $Product
618
     * @return \Eccube\Entity\ProductClass[]
619
     */
620
    private function getProductClassesOriginal(Product $Product)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
621
    {
622
        $ProductClasses = $Product->getProductClasses();
623
        return $ProductClasses->filter(function($ProductClass) {
0 ignored issues
show
Unused Code introduced by
The parameter $ProductClass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
624
            return true;
625
        });
626
    }
627
628
    /**
629
     * 規格なし商品を除いて商品規格を取得.
630
     *
631
     * @param Product $Product
632
     * @return \Eccube\Entity\ProductClass[]
633
     */
634
    private function getProductClassesExcludeNonClass(Product $Product)
635
    {
636
        $ProductClasses = $Product->getProductClasses();
637
        return $ProductClasses->filter(function($ProductClass) {
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
638
            $ClassCategory1 = $ProductClass->getClassCategory1();
639
            $ClassCategory2 = $ProductClass->getClassCategory2();
640
            return ($ClassCategory1 || $ClassCategory2);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
641
        });
642
    }
643
644
    /**
645
     * デフォルトとなる商品規格を設定
646
     *
647
     * @param $productClassDest コピー先となる商品規格
648
     * @param $productClassOrig コピー元となる商品規格
649
     */
650
    private function setDefualtProductClass($app, $productClassDest, $productClassOrig) {
651
        $productClassDest->setDeliveryDate($productClassOrig->getDeliveryDate());
652
        $productClassDest->setProduct($productClassOrig->getProduct());
653
        $productClassDest->setProductType($productClassOrig->getProductType());
654
        $productClassDest->setCode($productClassOrig->getCode());
655
        $productClassDest->setStock($productClassOrig->getStock());
656
        $productClassDest->setStockUnlimited($productClassOrig->getStockUnlimited());
657
        $productClassDest->setSaleLimit($productClassOrig->getSaleLimit());
658
        $productClassDest->setPrice01($productClassOrig->getPrice01());
659
        $productClassDest->setPrice02($productClassOrig->getPrice02());
660
        $productClassDest->setDeliveryFee($productClassOrig->getDeliveryFee());
661
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
662
        // 個別消費税
663
        $BaseInfo = $app['eccube.repository.base_info']->get();
664 View Code Duplication
        if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
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...
665
            if($productClassOrig->getTaxRate()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
666
                $productClassDest->setTaxRate($productClassOrig->getTaxRate());
667
                if ($productClassDest->getTaxRule() && !$productClassDest->getTaxRule()->getDelFlg()) {
668
                    $productClassDest->getTaxRule()->setTaxRate($productClassOrig->getTaxRate());
669
                } else {
670
                    $taxrule = $app['eccube.repository.tax_rule']->newTaxRule();
671
                    $taxrule->setTaxRate($productClassOrig->getTaxRate());
672
                    $taxrule->setApplyDate(new \DateTime());
673
                    $taxrule->setProduct($productClassDest->getProduct());
674
                    $taxrule->setProductClass($productClassDest);
675
                    $productClassDest->setTaxRule($taxrule);
676
                }
677
            } else {
678
                if ($productClassDest->getTaxRule()) {
679
                    $productClassDest->getTaxRule()->setDelFlg(Constant::ENABLED);
680
                }
681
            }
682
        }
683
    }
684
685
686
    /**
687
     * 商品規格を登録
688
     *
689
     * @param $ProductClasses 登録される商品規格
690
     */
691
    private function insertProductClass($app, $Product, $ProductClasses) {
692
693
        $BaseInfo = $app['eccube.repository.base_info']->get();
694
695
        // 選択された商品を登録
696 View Code Duplication
        foreach ($ProductClasses as $ProductClass) {
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...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
697
698
            $ProductClass->setDelFlg(Constant::DISABLED);
699
            $ProductClass->setProduct($Product);
700
            $app['orm.em']->persist($ProductClass);
701
702
            // 在庫情報を作成
703
            $ProductStock = new \Eccube\Entity\ProductStock();
704
            $ProductClass->setProductStock($ProductStock);
705
            $ProductStock->setProductClass($ProductClass);
706
            if (!$ProductClass->getStockUnlimited()) {
707
                $ProductStock->setStock($ProductClass->getStock());
708
            } else {
709
                // 在庫無制限時はnullを設定
710
                $ProductStock->setStock(null);
711
            }
712
            $app['orm.em']->persist($ProductStock);
713
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
714
        }
715
716
        // 商品税率が設定されている場合、商品税率をセット
717
        if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
718
            // 初期設定の税設定.
719
            $TaxRule = $app['eccube.repository.tax_rule']->find(\Eccube\Entity\TaxRule::DEFAULT_TAX_RULE_ID);
720
            // 初期税率設定の計算方法を設定する
721
            $CalcRule = $TaxRule->getCalcRule();
722 View Code Duplication
            foreach ($ProductClasses as $ProductClass) {
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...
723
                if ($ProductClass->getTaxRate()) {
724
                    $TaxRule = new \Eccube\Entity\TaxRule();
725
                    $TaxRule->setProduct($Product);
726
                    $TaxRule->setProductClass($ProductClass);
727
                    $TaxRule->setCalcRule($CalcRule);
728
                    $TaxRule->setTaxRate($ProductClass->getTaxRate());
729
                    $TaxRule->setTaxAdjust(0);
730
                    $TaxRule->setApplyDate(new \DateTime());
731
                    $TaxRule->setDelFlg(Constant::DISABLED);
732
                    $app['orm.em']->persist($TaxRule);
733
                }
734
            }
735
        }
736
737
    }
738
739
    /**
740
     * 規格の分類判定
741
     *
742
     * @param $class_name
743
     * @return boolean
744
     */
745
    private function isValiedCategory($class_name)
746
    {
747
        if (empty($class_name)) {
748
            return true;
749
        }
750
        if (count($class_name->getClassCategories()) < 1) {
751
            return false;
752
        }
753
        return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
754
    }
755
}
756