Completed
Push — master ( d8324b...851875 )
by Kentaro
33:50
created

MasterdataController::index()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 6.4391
c 0
b 0
f 0
cc 8
eloc 45
nc 12
nop 3

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
    public function index(Application $app, Request $request, $entity = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        $data = array();
39
40
        $builder = $app['form.factory']->createBuilder('admin_system_masterdata');
41
42
        $event = new EventArgs(
43
            array(
44
                'builder' => $builder,
45
            ),
46
            $request
47
        );
48
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_INDEX_INITIALIZE, $event);
49
50
        $form = $builder->getForm();
51
52
        if ('POST' === $request->getMethod()) {
53
            $form->handleRequest($request);
54
            if ($form->isValid()) {
55
                $event = new EventArgs(
56
                    array(
57
                        'form' => $form,
58
                    ),
59
                    $request
60
                );
61
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_INDEX_COMPLETE, $event);
62
63
                if ($event->hasResponse()) {
64
                    return $event->getResponse();
65
                }
66
67
                return $app->redirect($app->url('admin_setting_system_masterdata_view', array('entity' => $form['masterdata']->getData())));
68
            }
69
        } elseif (!is_null($entity)) {
70
            $form->submit(array('masterdata' => $entity));
71
            if ($form['masterdata']->isValid()) {
72
                $entityName = str_replace('-', '\\', $entity);
73
                try {
74
                    $masterdata = $app['orm.em']->getRepository($entityName)->findBy(array(), array('rank' => 'ASC'));
75
                    $data['data'] = array();
76
                    $data['masterdata_name'] = $entity;
77
                    foreach ($masterdata as $value) {
78
                        $data['data'][$value['id']]['id'] = $value['id'];
79
                        $data['data'][$value['id']]['name'] = $value['name'];
80
                    }
81
                    $data['data'][] = array(
82
                        'id' => '',
83
                        'name' => '',
84
                    );
85
                } 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
            }
88
        }
89
90
        $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit', $data);
91
92
        $event = new EventArgs(
93
            array(
94
                'builder' => $builder2,
95
            ),
96
            $request
97
        );
98
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_INDEX_FORM2_INITIALIZE, $event);
99
100
        $form2 = $builder2->getForm();
101
102
        return $app->render('Setting/System/masterdata.twig', array(
103
            'form' => $form->createView(),
104
            'form2' => $form2->createView(),
105
        ));
106
    }
107
108
    public function edit(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
109
    {
110
        $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit');
111
112
        $event = new EventArgs(
113
            array(
114
                'builder' => $builder2,
115
            ),
116
            $request
117
        );
118
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_INITIALIZE, $event);
119
120
        $form2 = $builder2->getForm();
121
122
        if ('POST' === $request->getMethod()) {
123
            $form2->handleRequest($request);
124
125
            if ($form2->isValid()) {
126
                $data = $form2->getData();
127
128
                $entityName = str_replace('-', '\\', $data['masterdata_name']);
129
                $entity = new $entityName();
130
                $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
                foreach ($data['data'] as $key => $value) {
133
                    if ($value['id'] !== null && $value['name'] !== null) {
134
                        $entity->setId($value['id']);
135
                        $entity->setName($value['name']);
136
                        $entity->setRank($rank++);
137
                        $app['orm.em']->merge($entity);
138
                    } 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
                }
146
147
                try {
148
                    $app['orm.em']->flush();
149
150
                    $event = new EventArgs(
151
                        array(
152
                            'form' => $form2,
153
                        ),
154
                        $request
155
                    );
156
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_COMPLETE, $event);
157
158
                    $app->addSuccess('admin.register.complete', 'admin');
159
                } catch (\Exception $e) {
160
                    // 外部キー制約などで削除できない場合に例外エラーになる
161
                    $app->addError('admin.register.failed', 'admin');
162
                }
163
164
                return $app->redirect($app->url('admin_setting_system_masterdata_view', array('entity' => $data['masterdata_name'])));
165
            }
166
        }
167
168
        $builder = $app['form.factory']->createBuilder('admin_system_masterdata');
169
170
        $event = new EventArgs(
171
            array(
172
                'builder' => $builder,
173
            ),
174
            $request
175
        );
176
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_FORM_INITIALIZE, $event);
177
178
        $form = $builder->getForm();
179
        $parameter = array_merge($request->request->all(), array('masterdata' => $form2['masterdata_name']->getData()));
180
        $form->submit($parameter);
181
182
        return $app->render('Setting/System/masterdata.twig', array(
183
            'form' => $form->createView(),
184
            'form2' => $form2->createView(),
185
        ));
186
    }
187
}
188