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

ChangeController::index()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 62
Code Lines 37

Duplication

Lines 11
Ratio 17.74 %

Code Coverage

Tests 32
CRAP Score 5.0006

Importance

Changes 0
Metric Value
cc 5
eloc 37
nc 4
nop 2
dl 11
loc 62
ccs 32
cts 33
cp 0.9697
crap 5.0006
rs 8.6652
c 0
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
25
namespace Eccube\Controller\Mypage;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Component;
29
use Eccube\Annotation\Inject;
30
use Eccube\Application;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Front\EntryType;
35
use Eccube\Repository\CustomerRepository;
36
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
38
use Symfony\Component\EventDispatcher\EventDispatcher;
39
use Symfony\Component\Form\FormFactory;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
42
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
43
44
/**
45
 * @Component
46
 * @Route(service=ChangeController::class)
47
 */
48
class ChangeController extends AbstractController
49
{
50
    /**
51
     * @Inject("security.token_storage")
52
     * @var TokenStorage
53
     */
54
    protected $tokenStorage;
55
56
    /**
57
     * @Inject(CustomerRepository::class)
58
     * @var CustomerRepository
59
     */
60
    protected $customerRepository;
61
62
    /**
63
     * @Inject("eccube.event.dispatcher")
64
     * @var EventDispatcher
65
     */
66
    protected $eventDispatcher;
67
68
    /**
69
     * @Inject("form.factory")
70
     * @var FormFactory
71
     */
72
    protected $formFactory;
73
74
    /**
75
     * @Inject("config")
76
     * @var array
77
     */
78
    protected $appConfig;
79
80
    /**
81
     * @Inject("orm.em")
82
     * @var EntityManager
83
     */
84
    protected $entityManager;
85
86
    /**
87
     * @Inject("security.encoder_factory")
88
     * @var EncoderFactoryInterface
89
     */
90
    protected $encoderFactory;
91
92
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
93
     * 会員情報編集画面.
94
     *
95
     * @Route("/mypage/change", name="mypage_change")
96
     * @Template("Mypage/change.twig")
97
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
98 4
    public function index(Application $app, Request $request)
99
    {
100 4
        $Customer = $app->user();
101 4
        $LoginCustomer = clone $Customer;
102 4
        $this->entityManager->detach($LoginCustomer);
103
104 4
        $previous_password = $Customer->getPassword();
105 4
        $Customer->setPassword($this->appConfig['default_password']);
106
107
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
108 4
        $builder = $this->formFactory->createBuilder(EntryType::class, $Customer);
109
110 4
        $event = new EventArgs(
111
            array(
112 4
                'builder' => $builder,
113 4
                'Customer' => $Customer,
114
            ),
115 4
            $request
116
        );
117 4
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_INITIALIZE, $event);
118
119
        /* @var $form \Symfony\Component\Form\FormInterface */
120 4
        $form = $builder->getForm();
121 4
        $form->handleRequest($request);
122
123 4
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
124
125 2
            log_info('会員編集開始');
126
127 2 View Code Duplication
            if ($Customer->getPassword() === $this->appConfig['default_password']) {
128 1
                $Customer->setPassword($previous_password);
129
            } else {
130 1
                $encoder = $this->encoderFactory->getEncoder($Customer);
131 1
                if ($Customer->getSalt() === null) {
132
                    $Customer->setSalt($encoder->createSalt());
133
                }
134 1
                $Customer->setPassword(
135 1
                    $encoder->encodePassword($Customer->getPassword(), $Customer->getSalt())
136
                );
137
            }
138 2
            $this->entityManager->flush();
139
140 2
            log_info('会員編集完了');
141
142 2
            $event = new EventArgs(
143
                array(
144 2
                    'form' => $form,
145 2
                    'Customer' => $Customer,
146
                ),
147 2
                $request
148
            );
149 2
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE, $event);
150
151 2
            return $app->redirect($app->url('mypage_change_complete'));
152
        }
153
154 2
        $this->tokenStorage->getToken()->setUser($LoginCustomer);
155
156
        return [
157 2
            'form' => $form->createView(),
158
        ];
159
    }
160
161
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
162
     * 会員情報編集完了画面.
163
     *
164
     * @Route("/mypage/change_complete", name="mypage_change_complete")
165
     * @Template("Mypage/change_complete.twig")
166
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
167 1
    public function complete(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...
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...
168
    {
169 1
        return [];
170
    }
171
}
172