Failed Conditions
Push — master ( 36ccc8...f8bd2f )
by Kentaro
34:47
created

MemberController::edit()   C

Complexity

Conditions 9
Paths 17

Size

Total Lines 74
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 74
ccs 10
cts 10
cp 1
rs 5.9292
c 3
b 0
f 0
cc 9
eloc 47
nc 17
nop 3
crap 9

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