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

MemberController::edit()   C

Complexity

Conditions 9
Paths 17

Size

Total Lines 74
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 9.0051

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 74
ccs 48
cts 50
cp 0.96
rs 5.9292
cc 9
eloc 47
nc 17
nop 3
crap 9.0051

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
namespace Eccube\Controller\Admin\Setting\System;
25
26
use Eccube\Application;
27
use Eccube\Controller\AbstractController;
28
use Eccube\Event\EccubeEvents;
29
use Eccube\Event\EventArgs;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
class MemberController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
34
{
35 23
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
36
    {
37 23
    }
38
39 2 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41 2
        $Members = $app['eccube.repository.member']->findBy(array(), array('rank' => 'DESC'));
42
43 2
        $builder = $app['form.factory']->createBuilder();
44
45 2
        $event = new EventArgs(
46
            array(
47 2
                'builder' => $builder,
48 2
                'Members' => $Members,
49 2
            ),
50
            $request
51 2
        );
52 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_INDEX_INITIALIZE, $event);
53
54 2
        $form = $builder->getForm();
55
56 2
        return $app->render('Setting/System/member.twig', array(
57 2
            'form' => $form->createView(),
58 2
            'Members' => $Members,
59 2
        ));
60
    }
61
62 21
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
63
    {
64 9
        $previous_password = null;
65 21
        if ($id) {
66 5
            $Member = $app['eccube.repository.member']->find($id);
67 5
            if (!$Member) {
68 1
                throw new NotFoundHttpException();
69
            }
70 4
            $previous_password = $Member->getPassword();
71 4
            $Member->setPassword($app['config']['default_password']);
72 4
        } else {
73 4
            $Member = new \Eccube\Entity\Member();
74
        }
75
76 8
        $builder = $app['form.factory']
77 8
            ->createBuilder('admin_member', $Member);
78
79 8
        $event = new EventArgs(
80
            array(
81 8
                'builder' => $builder,
82 8
                'Member' => $Member,
83 8
            ),
84
            $request
85 8
        );
86 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_EDIT_INITIALIZE, $event);
87
88 8
        $form = $builder->getForm();
89
90 8
        if ('POST' === $request->getMethod()) {
91 4
            $form->handleRequest($request);
92 4
            if ($form->isValid()) {
93 2
                if (!is_null($previous_password)
94 2
                    && $Member->getpassword() === $app['config']['default_password']) {
95
                    // 編集時にPWを変更していなければ
96
                    // 変更前のパスワード(暗号化済み)をセット
97 1
                    $Member->setPassword($previous_password);
98 1
                } else {
99 1
                    $salt = $Member->getSalt();
100 1
                    if (!isset($salt)) {
101 1
                        $salt = $app['eccube.repository.member']->createSalt(5);
102 1
                        $Member->setSalt($salt);
103 1
                    }
104
105
                    // 入力されたPWを暗号化してセット
106 1
                    $password = $app['eccube.repository.member']->encryptPassword($Member);
107 1
                    $Member->setPassword($password);
108
                }
109 2
                $status = $app['eccube.repository.member']->save($Member);
110
111 2
                if ($status) {
112 2
                    $event = new EventArgs(
113
                        array(
114 2
                            'form' => $form,
115 2
                            'Member' => $Member,
116 2
                        ),
117
                        $request
118 2
                    );
119 2
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_EDIT_COMPLETE, $event);
120
121 2
                    $app->addSuccess('admin.member.save.complete', 'admin');
122
123 2
                    return $app->redirect($app->url('admin_setting_system_member'));
124
                } else {
125
                    $app->addError('admin.member.save.error', 'admin');
126
                }
127
            }
128 2
        }
129
130 6
        return $app->render('Setting/System/member_edit.twig', array(
131 6
            'form' => $form->createView(),
132 6
            'Member' => $Member,
133 6
        ));
134
135
    }
136
137 4 View Code Duplication
    public function up(Application $app, Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
138
    {
139 4
        $this->isTokenValid($app);
140
141 4
        $TargetMember = $app['eccube.repository.member']->find($id);
142
143 4
        if (!$TargetMember) {
144 1
            throw new NotFoundHttpException();
145
        }
146
147 3
        $status = false;
148 3
        if ('PUT' === $request->getMethod()) {
149 3
            $status = $app['eccube.repository.member']->up($TargetMember);
150 3
        }
151
152 3
        if ($status) {
153 1
            $app->addSuccess('admin.member.up.complete', 'admin');
154 1
        } else {
155 2
            $app->addError('admin.member.up.error', 'admin');
156
        }
157
158 3
        return $app->redirect($app->url('admin_setting_system_member'));
159
    }
160
161 5 View Code Duplication
    public function down(Application $app, Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
162
    {
163 5
        $this->isTokenValid($app);
164
165 5
        $TargetMember = $app['eccube.repository.member']->find($id);
166
167 5
        if (!$TargetMember) {
168 1
            throw new NotFoundHttpException();
169
        }
170
171 4
        $status = false;
172 4
        if ('PUT' === $request->getMethod()) {
173 4
            $status = $app['eccube.repository.member']->down($TargetMember);
174 4
        }
175
176 4
        if ($status) {
177 2
            $app->addSuccess('admin.member.down.complete', 'admin');
178 2
        } else {
179 2
            $app->addError('admin.member.down.error', 'admin');
180
        }
181
182 4
        return $app->redirect($app->url('admin_setting_system_member'));
183
    }
184
185 3
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
186
    {
187 3
        $this->isTokenValid($app);
188
189 3
        $TargetMember = $app['eccube.repository.member']->find($id);
190 3
        if (!$TargetMember) {
191 1
            $app->deleteMessage();
192 1
            return $app->redirect($app->url('admin_setting_system_member'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
193
        }
194
195 2
        $event = new EventArgs(
196
            array(
197 2
                'TargetMember' => $TargetMember,
198 2
            ),
199
            $request
200 2
        );
201 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_DELETE_INITIALIZE, $event);
202
203 2
        $status = $app['eccube.repository.member']->delete($TargetMember);
204
205 2
        if ($status) {
206 2
            $app->addSuccess('admin.member.delete.complete', 'admin');
207 2
            $event = new EventArgs(
208 2
                array(),
209
                $request
210 2
            );
211 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_DELETE_COMPLETE, $event);
212 2
        } else {
213
            $app->addError('admin.member.delete.error', 'admin');
214
        }
215
216 2
        return $app->redirect($app->url('admin_setting_system_member'));
217
    }
218
}