Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

MemberController::edit()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 65
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5.2742

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 40
nc 4
nop 3
dl 0
loc 65
ccs 28
cts 36
cp 0.7778
crap 5.2742
rs 8.6195
c 1
b 0
f 0

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 Doctrine\ORM\EntityManager;
27
use Eccube\Annotation\Component;
28
use Eccube\Annotation\Inject;
29
use Eccube\Application;
30
use Eccube\Controller\AbstractController;
31
use Eccube\Entity\Member;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Admin\MemberType;
35
use Eccube\Repository\MemberRepository;
36
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Component\EventDispatcher\EventDispatcher;
40
use Symfony\Component\Form\FormFactory;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
43
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
44
45
/**
46
 * @Component
47
 * @Route(service=MemberController::class)
48
 */
49
class MemberController extends AbstractController
50
{
51
    /**
52
     * @Inject("security.token_storage")
53
     * @var TokenStorage
54
     */
55
    protected $tokenStorage;
56
57
    /**
58
     * @Inject("orm.em")
59
     * @var EntityManager
60
     */
61
    protected $entityManager;
62
63
    /**
64
     * @Inject("config")
65
     * @var array
66
     */
67
    protected $appConfig;
68
69
    /**
70
     * @Inject("eccube.event.dispatcher")
71
     * @var EventDispatcher
72
     */
73
    protected $eventDispatcher;
74
75
    /**
76
     * @Inject("form.factory")
77
     * @var FormFactory
78
     */
79
    protected $formFactory;
80
81
    /**
82
     * @Inject(MemberRepository::class)
83
     * @var MemberRepository
84
     */
85
    protected $memberRepository;
86
87
    /**
88
     * @Inject("security.encoder_factory")
89
     * @var EncoderFactoryInterface
90
     */
91
    protected $encoderFactory;
92
93
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
94
     * @Route("/{_admin}/setting/system/member", name="admin_setting_system_member")
95
     * @Template("Setting/System/member.twig")
96
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
97 1 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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...
98
    {
99 1
        $Members = $this->memberRepository->findBy([], ['rank' => 'DESC']);
100
101 1
        $builder = $this->formFactory->createBuilder();
102
103 1
        $event = new EventArgs(
104
            array(
105 1
                'builder' => $builder,
106 1
                'Members' => $Members,
107
            ),
108 1
            $request
109
        );
110 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_INDEX_INITIALIZE, $event);
111
112 1
        $form = $builder->getForm();
113
114
        return [
115 1
            'form' => $form->createView(),
116 1
            'Members' => $Members,
117
        ];
118
    }
119
120
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
121
     * @Route("/{_admin}/setting/system/member/new", name="admin_setting_system_member_new")
122
     * @Template("Setting/System/member_edit.twig")
123
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
124 3
    public function create(Application $app, Request $request)
125
    {
126 3
        $LoginMember = clone $app->user();
127 3
        $this->entityManager->detach($LoginMember);
128
129 3
        $Member = new Member();
130 3
        $builder = $this->formFactory
131 3
            ->createBuilder(MemberType::class, $Member);
132
133 3
        $event = new EventArgs([
134 3
            'builder' => $builder,
135 3
            'Member' => $Member,
136
        ],
137 3
            $request
138
        );
139 3
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_EDIT_INITIALIZE, $event);
140
141 3
        $form = $builder->getForm();
142 3
        $form->handleRequest($request);
143
144 3
        if ($form->isSubmitted() && $form->isValid()) {
145 1
            $encoder = $this->encoderFactory->getEncoder($Member);
146 1
            $salt = $encoder->createSalt();
147 1
            $rawPassword = $Member->getPassword();
148 1
            $encodedPassword = $encoder->encodePassword($rawPassword, $salt);
149
            $Member
150 1
                ->setSalt($salt)
151 1
                ->setPassword($encodedPassword);
152
153 1
            $this->memberRepository->save($Member);
154
155 1
            $event = new EventArgs(
156
                array(
157 1
                    'form' => $form,
158 1
                    'Member' => $Member,
159
                ),
160 1
                $request
161
            );
162 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_EDIT_COMPLETE, $event);
163
164 1
            $app->addSuccess('admin.member.save.complete', 'admin');
165
166 1
            return $app->redirect($app->url('admin_setting_system_member'));
167
        }
168
169 2
        $this->tokenStorage->getToken()->setUser($LoginMember);
170
171
        return [
172 2
            'form' => $form->createView(),
173 2
            'Member' => $Member,
174
        ];
175
    }
176
177
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Member" missing
Loading history...
178
     * @Route("/{_admin}/setting/system/member/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_system_member_edit")
179
     * @Template("Setting/System/member_edit.twig")
180
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
181 3
    public function edit(Application $app, Request $request, Member $Member)
182
    {
183 3
        $LoginMember = clone $app->user();
184 3
        $this->entityManager->detach($LoginMember);
185
186 3
        $previousPassword = $Member->getPassword();
187 3
        $Member->setPassword($this->appConfig['default_password']);
188
189 3
        $builder = $this->formFactory
190 3
            ->createBuilder(MemberType::class, $Member);
191
192 3
        $event = new EventArgs(
193
            array(
194 3
                'builder' => $builder,
195 3
                'Member' => $Member,
196
            ),
197 3
            $request
198
        );
199 3
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_EDIT_INITIALIZE, $event);
200
201 3
        $form = $builder->getForm();
202 3
        $form->handleRequest($request);
203
204 3
        if ($form->isSubmitted() && $form->isValid()) {
205 1
            if ($Member->getpassword() === $this->appConfig['default_password']) {
206
                // 編集時にパスワードを変更していなければ
207
                // 変更前のパスワード(暗号化済み)をセット
208 1
                $Member->setPassword($previousPassword);
209
            } else {
210
                $salt = $Member->getSalt();
211
                // 2系からのデータ移行でsaltがセットされていない場合はsaltを生成.
212
                if (empty($salt)) {
213
                    $salt = bin2hex(openssl_random_pseudo_bytes(5));
214
                    $Member->setSalt($salt);
215
                }
216
217
                $rawPassword = $Member->getPassword();
218
                $encoder = $this->encoderFactory->getEncoder($Member);
219
                $encodedPassword = $encoder->encodePassword($rawPassword, $salt);
220
                $Member->setPassword($encodedPassword);
221
            }
222
223 1
            $this->memberRepository->save($Member);
224
225 1
            $event = new EventArgs(
226
                array(
227 1
                    'form' => $form,
228 1
                    'Member' => $Member,
229
                ),
230 1
                $request
231
            );
232 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_EDIT_COMPLETE, $event);
233
234 1
            $app->addSuccess('admin.member.save.complete', 'admin');
235
236 1
            return $app->redirect($app->url('admin_setting_system_member'));
237
        }
238
239 2
        $this->tokenStorage->getToken()->setUser($LoginMember);
240
241
        return [
242 2
            'form' => $form->createView(),
243 2
            'Member' => $Member,
244
        ];
245
    }
246
247
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Member" missing
Loading history...
248
     * @Method("PUT")
249
     * @Route("/{_admin}/setting/system/member/{id}/up", requirements={"id" = "\d+"}, name="admin_setting_system_member_up")
250
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
251 2 View Code Duplication
    public function up(Application $app, Request $request, Member $Member)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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...
252
    {
253 2
        $this->isTokenValid($app);
254
255
        try {
256 2
            $this->memberRepository->up($Member);
257
258 1
            $app->addSuccess('admin.member.up.complete', 'admin');
259
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
260 1
        } catch (\Exception $e) {
261 1
            log_error('メンバー表示順更新エラー', [$Member->getId(), $e]);
262
263 1
            $app->addError('admin.member.up.error', 'admin');
264
        }
265
266 2
        return $app->redirect($app->url('admin_setting_system_member'));
267
    }
268
269
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Member" missing
Loading history...
270
     * @Method("PUT")
271
     * @Route("/{_admin}/setting/system/member/{id}/down", requirements={"id" = "\d+"}, name="admin_setting_system_member_down")
272
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
273 3 View Code Duplication
    public function down(Application $app, Request $request, Member $Member)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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...
274
    {
275 3
        $this->isTokenValid($app);
276
277
        try {
278 3
            $this->memberRepository->down($Member);
279
280 2
            $app->addSuccess('admin.member.down.complete', 'admin');
281 1
        } catch (\Exception $e) {
282 1
            log_error('メンバー表示順更新エラー', [$Member->getId(), $e]);
283
284 1
            $app->addError('admin.member.down.error', 'admin');
285
        }
286
287 3
        return $app->redirect($app->url('admin_setting_system_member'));
288
    }
289
290
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Member" missing
Loading history...
291
     * @Method("DELETE")
292
     * @Route("/{_admin}/setting/system/member/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_system_member_delete")
293
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
294 1 View Code Duplication
    public function delete(Application $app, Request $request, Member $Member)
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...
295
    {
296 1
        $this->isTokenValid($app);
297
298 1
        log_info('メンバー削除開始', [$Member->getId()]);
299
300
        try {
301 1
            $this->memberRepository->delete($Member);
302
303 1
            $event = new EventArgs(
304
                [
305 1
                    'Member' => $Member,
306
                ],
307 1
                $request
308
            );
309 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MEMBER_DELETE_COMPLETE, $event);
310
311 1
            $app->addSuccess('admin.member.delete.complete', 'admin');
312
313 1
            log_info('メンバー削除完了', [$Member->getId()]);
314
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
315
        } catch (\Exception $e) {
316
            log_info('メンバー削除エラー', [$Member->getId(), $e]);
317
318
            $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => 'メンバー']);
319
            $app->addError($message, 'admin');
320
        }
321
322 1
        return $app->redirect($app->url('admin_setting_system_member'));
323
    }
324
}