Completed
Pull Request — master (#1779)
by Kentaro
214:45 queued 207:18
created

MasterdataController::index()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 8.0005

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 71
ccs 47
cts 48
cp 0.9792
rs 6.4391
cc 8
eloc 45
nc 12
nop 3
crap 8.0005

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Setting\System;
26
27
use Doctrine\Common\Persistence\Mapping\MappingException;
28
use Eccube\Application;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Event\EccubeEvents;
31
use Eccube\Event\EventArgs;
32
use Symfony\Component\HttpFoundation\Request;
33
34
class MasterdataController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36 2
    public function index(Application $app, Request $request, $entity = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38 2
        $data = array();
39
40 2
        $builder = $app['form.factory']->createBuilder('admin_system_masterdata');
41
42 2
        $event = new EventArgs(
43
            array(
44 2
                'builder' => $builder,
45 2
            ),
46
            $request
47 2
        );
48 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_INDEX_INITIALIZE, $event);
49
50 2
        $form = $builder->getForm();
51
52 2
        if ('POST' === $request->getMethod()) {
53 1
            $form->handleRequest($request);
54 1
            if ($form->isValid()) {
55 1
                $event = new EventArgs(
56
                    array(
57 1
                        'form' => $form,
58 1
                    ),
59
                    $request
60 1
                );
61 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_INDEX_COMPLETE, $event);
62
63 1
                if ($event->hasResponse()) {
64
                    return $event->getResponse();
65
                }
66
67 1
                return $app->redirect($app->url('admin_setting_system_masterdata_view', array('entity' => $form['masterdata']->getData())));
68
            }
69 2
        } elseif (!is_null($entity)) {
70 1
            $form->submit(array('masterdata' => $entity));
71 1
            if ($form['masterdata']->isValid()) {
72 1
                $entityName = str_replace('-', '\\', $entity);
73
                try {
74 1
                    $masterdata = $app['orm.em']->getRepository($entityName)->findBy(array(), array('rank' => 'ASC'));
75 1
                    $data['data'] = array();
76 1
                    $data['masterdata_name'] = $entity;
77 1
                    foreach ($masterdata as $value) {
78 1
                        $data['data'][$value['id']]['id'] = $value['id'];
79 1
                        $data['data'][$value['id']]['name'] = $value['name'];
80 1
                    }
81 1
                    $data['data'][] = array(
82 1
                        'id' => '',
83 1
                        'name' => '',
84
                    );
85 1
                } catch (MappingException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
86
                }
87 1
            }
88 1
        }
89
90 2
        $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit', $data);
91
92 2
        $event = new EventArgs(
93
            array(
94 2
                'builder' => $builder2,
95 2
            ),
96
            $request
97 2
        );
98 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_INDEX_FORM2_INITIALIZE, $event);
99
100 2
        $form2 = $builder2->getForm();
101
102 2
        return $app->render('Setting/System/masterdata.twig', array(
103 2
            'form' => $form->createView(),
104 2
            'form2' => $form2->createView(),
105 2
        ));
106
    }
107
108 3
    public function edit(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
109
    {
110 3
        $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit');
111
112 3
        $event = new EventArgs(
113
            array(
114 3
                'builder' => $builder2,
115 3
            ),
116
            $request
117 3
        );
118 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_INITIALIZE, $event);
119
120 3
        $form2 = $builder2->getForm();
121
122 3
        if ('POST' === $request->getMethod()) {
123 2
            $form2->handleRequest($request);
124
125 2
            if ($form2->isValid()) {
126 2
                $data = $form2->getData();
127
128 2
                $entityName = str_replace('-', '\\', $data['masterdata_name']);
129 2
                $entity = new $entityName();
130 2
                $rank = 0;
131
                $ids = array_map(function ($v) {return $v['id'];}, $data['data']);
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
132 2
                foreach ($data['data'] as $key => $value) {
133 2
                    if ($value['id'] !== null && $value['name'] !== null) {
134 2
                        $entity->setId($value['id']);
135 2
                        $entity->setName($value['name']);
136 2
                        $entity->setRank($rank++);
137 2
                        $app['orm.em']->merge($entity);
138 2
                    } elseif (!in_array($key, $ids)) {
139
                        // remove
140
                        $delKey = $app['orm.em']->getRepository($entityName)->find($key);
141
                        if ($delKey) {
142
                            $app['orm.em']->remove($delKey);
143
                        }
144
                    }
145 2
                }
146
147
                try {
148 2
                    $app['orm.em']->flush();
149
150 2
                    $event = new EventArgs(
151
                        array(
152 2
                            'form' => $form2,
153 2
                        ),
154
                        $request
155 2
                    );
156 2
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_COMPLETE, $event);
157
158 2
                    $app->addSuccess('admin.register.complete', 'admin');
159 2
                } catch (\Exception $e) {
160
                    // 外部キー制約などで削除できない場合に例外エラーになる
161
                    $app->addError('admin.register.failed', 'admin');
162
                }
163
164 2
                return $app->redirect($app->url('admin_setting_system_masterdata_view', array('entity' => $data['masterdata_name'])));
165
            }
166
        }
167
168 1
        $builder = $app['form.factory']->createBuilder('admin_system_masterdata');
169
170 1
        $event = new EventArgs(
171
            array(
172 1
                'builder' => $builder,
173 1
            ),
174
            $request
175 1
        );
176 1
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_FORM_INITIALIZE, $event);
177
178 1
        $form = $builder->getForm();
179 1
        $parameter = array_merge($request->request->all(), array('masterdata' => $form2['masterdata_name']->getData()));
180 1
        $form->submit($parameter);
181
182 1
        return $app->render('Setting/System/masterdata.twig', array(
183 1
            'form' => $form->createView(),
184 1
            'form2' => $form2->createView(),
185 1
        ));
186
    }
187
}
188