Failed Conditions
Push — experimental/sf ( 37b3aa...cc45c1 )
by chihiro
921:05 queued 894:01
created

CsvController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 144
Duplicated Lines 13.89 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 21.54%

Importance

Changes 0
Metric Value
dl 20
loc 144
ccs 14
cts 65
cp 0.2154
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B index() 20 111 6

How to fix   Duplicated Code   

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:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Setting\Shop;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\Master\CsvType;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Repository\CsvRepository;
21
use Eccube\Repository\Master\CsvTypeRepository;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Validator\Constraints as Assert;
27
28
/**
29
 * Class CsvController
30
 */
31
class CsvController extends AbstractController
32
{
33
    /**
34
     * @var CsvRepository
35
     */
36
    protected $csvRepository;
37
38
    /**
39
     * @var CsvTypeRepository
40
     */
41
    protected $csvTypeRepository;
42
43
    /**
44
     * CsvController constructor.
45
     *
46
     * @param CsvRepository $csvRepository
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
47
     * @param CsvTypeRepository $csvTypeRepository
48
     */
49 2
    public function __construct(CsvRepository $csvRepository, CsvTypeRepository $csvTypeRepository)
50
    {
51 2
        $this->csvRepository = $csvRepository;
52 2
        $this->csvTypeRepository = $csvTypeRepository;
53
    }
54
55
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$CsvType" missing
Loading history...
56
     * @Route("/%eccube_admin_route%/setting/shop/csv/{id}",
57
     *     requirements={"id" = "\d+"},
58
     *     defaults={"id" = CsvType::CSV_TYPE_ORDER},
59
     *     name="admin_setting_shop_csv"
60
     * )
61
     * @Template("@admin/Setting/Shop/csv.twig")
62
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
63 1
    public function index(Request $request, CsvType $CsvType)
64
    {
65 1
        $builder = $this->createFormBuilder();
66
67 1
        $builder->add(
68 1
            'csv_type',
69 1
            \Eccube\Form\Type\Master\CsvType::class,
70
            [
71 1
                'label' => 'CSV出力項目',
72
                'required' => true,
73
                'constraints' => [
74 1
                    new Assert\NotBlank(),
75
                ],
76 1
                'data' => $CsvType,
77
            ]
78
        );
79
80 1
        $CsvNotOutput = $this->csvRepository->findBy(
81 1
            ['CsvType' => $CsvType, 'enabled' => false],
82 1
            ['sort_no' => 'ASC']
83
        );
84
85
        $builder->add(
86
            'csv_not_output',
87
            EntityType::class,
88
            [
89
                'class' => 'Eccube\Entity\Csv',
90
                'choice_label' => 'disp_name',
91
                'required' => false,
92
                'expanded' => false,
93
                'multiple' => true,
94
                'choices' => $CsvNotOutput,
95
            ]
96
        );
97
98
        $CsvOutput = $this->csvRepository->findBy(
99
            ['CsvType' => $CsvType, 'enabled' => true],
100
            ['sort_no' => 'ASC']
101
        );
102
103
        $builder->add(
104
            'csv_output',
105
            EntityType::class,
106
            [
107
                'class' => 'Eccube\Entity\Csv',
108
                'choice_label' => 'disp_name',
109
                'required' => false,
110
                'expanded' => false,
111
                'multiple' => true,
112
                'choices' => $CsvOutput,
113
            ]
114
        );
115
116
        $event = new EventArgs(
117
            [
118
                'builder' => $builder,
119
                'CsvOutput' => $CsvOutput,
120
                'CsvType' => $CsvType,
121
            ],
122
            $request
123
        );
124
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_CSV_INDEX_INITIALIZE, $event);
125
126
        $form = $builder->getForm();
127
128
        if ('POST' === $request->getMethod()) {
129
            $data = $request->get('form');
130 View Code Duplication
            if (isset($data['csv_not_output'])) {
131
                $Csvs = $data['csv_not_output'];
132
                $sortNo = 1;
133
                foreach ($Csvs as $csv) {
134
                    $c = $this->csvRepository->find($csv);
135
                    $c->setSortNo($sortNo);
136
                    $c->setEnabled(false);
137
                    $sortNo++;
138
                }
139
            }
140
141 View Code Duplication
            if (isset($data['csv_output'])) {
142
                $Csvs = $data['csv_output'];
143
                $sortNo = 1;
144
                foreach ($Csvs as $csv) {
145
                    $c = $this->csvRepository->find($csv);
146
                    $c->setSortNo($sortNo);
147
                    $c->setEnabled(true);
148
                    $sortNo++;
149
                }
150
            }
151
152
            $this->entityManager->flush();
153
154
            $event = new EventArgs(
155
                [
156
                    'form' => $form,
157
                    'CsvOutput' => $CsvOutput,
158
                    'CsvType' => $CsvType,
159
                ],
160
                $request
161
            );
162
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_CSV_INDEX_COMPLETE, $event);
163
164
            $this->addSuccess('admin.shop.csv.save.complete', 'admin');
165
166
            return $this->redirectToRoute('admin_setting_shop_csv', ['id' => $CsvType->getId()]);
167
        }
168
169
        return [
170
            'form' => $form->createView(),
171
            'id' => $CsvType->getId(),
172
        ];
173
    }
174
}
175