Completed
Push — sf/improvement-coverage ( b3937e...01a837 )
by Kiyotaka
51:20 queued 45:08
created

CsvController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 144
Duplicated Lines 13.89 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

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

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
47
     * @param CsvTypeRepository $csvTypeRepository
48
     */
49 3
    public function __construct(CsvRepository $csvRepository, CsvTypeRepository $csvTypeRepository)
50
    {
51 3
        $this->csvRepository = $csvRepository;
52 3
        $this->csvTypeRepository = $csvTypeRepository;
53
    }
54
55
    /**
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
     */
63 2
    public function index(Request $request, CsvType $CsvType)
64
    {
65 2
        $builder = $this->createFormBuilder();
66
67 2
        $builder->add(
68 2
            'csv_type',
69 2
            \Eccube\Form\Type\Master\CsvType::class,
70
            [
71 2
                'label' => 'CSV出力項目',
72
                'required' => true,
73
                'constraints' => [
74 2
                    new Assert\NotBlank(),
75
                ],
76 2
                'data' => $CsvType,
77
            ]
78
        );
79
80 2
        $CsvNotOutput = $this->csvRepository->findBy(
81 2
            ['CsvType' => $CsvType, 'enabled' => false],
82 2
            ['sort_no' => 'ASC']
83
        );
84
85 2
        $builder->add(
86 2
            'csv_not_output',
87 2
            EntityType::class,
88
            [
89 2
                'class' => 'Eccube\Entity\Csv',
90 2
                'choice_label' => 'disp_name',
91
                'required' => false,
92
                'expanded' => false,
93
                'multiple' => true,
94 2
                'choices' => $CsvNotOutput,
95
            ]
96
        );
97
98 2
        $CsvOutput = $this->csvRepository->findBy(
99 2
            ['CsvType' => $CsvType, 'enabled' => true],
100 2
            ['sort_no' => 'ASC']
101
        );
102
103 2
        $builder->add(
104 2
            'csv_output',
105 2
            EntityType::class,
106
            [
107 2
                'class' => 'Eccube\Entity\Csv',
108 2
                'choice_label' => 'disp_name',
109
                'required' => false,
110
                'expanded' => false,
111
                'multiple' => true,
112 2
                'choices' => $CsvOutput,
113
            ]
114
        );
115
116 2
        $event = new EventArgs(
117
            [
118 2
                'builder' => $builder,
119 2
                'CsvOutput' => $CsvOutput,
120 2
                'CsvType' => $CsvType,
121
            ],
122 2
            $request
123
        );
124 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_CSV_INDEX_INITIALIZE, $event);
125
126 2
        $form = $builder->getForm();
127
128 2
        if ('POST' === $request->getMethod()) {
129 1
            $data = $request->get('form');
130 1 View Code Duplication
            if (isset($data['csv_not_output'])) {
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...
131 1
                $Csvs = $data['csv_not_output'];
132 1
                $sortNo = 1;
133 1
                foreach ($Csvs as $csv) {
134 1
                    $c = $this->csvRepository->find($csv);
135 1
                    $c->setSortNo($sortNo);
136 1
                    $c->setEnabled(false);
137 1
                    $sortNo++;
138
                }
139
            }
140
141 1 View Code Duplication
            if (isset($data['csv_output'])) {
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...
142 1
                $Csvs = $data['csv_output'];
143 1
                $sortNo = 1;
144 1
                foreach ($Csvs as $csv) {
145 1
                    $c = $this->csvRepository->find($csv);
146 1
                    $c->setSortNo($sortNo);
147 1
                    $c->setEnabled(true);
148 1
                    $sortNo++;
149
                }
150
            }
151
152 1
            $this->entityManager->flush();
153
154 1
            $event = new EventArgs(
155
                [
156 1
                    'form' => $form,
157 1
                    'CsvOutput' => $CsvOutput,
158 1
                    'CsvType' => $CsvType,
159
                ],
160 1
                $request
161
            );
162 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_CSV_INDEX_COMPLETE, $event);
163
164 1
            $this->addSuccess('admin.shop.csv.save.complete', 'admin');
165
166 1
            return $this->redirectToRoute('admin_setting_shop_csv', ['id' => $CsvType->getId()]);
167
        }
168
169
        return [
170 1
            'form' => $form->createView(),
171 1
            'id' => $CsvType->getId(),
172
        ];
173
    }
174
}
175