Failed Conditions
Pull Request — experimental/3.1 (#2449)
by Kiyotaka
52:40
created

ForgotController::reset()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5.001

Importance

Changes 0
Metric Value
cc 5
eloc 32
nc 4
nop 3
dl 0
loc 56
ccs 28
cts 29
cp 0.9655
crap 5.001
rs 8.7592
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
namespace Eccube\Controller;
25
26
use Eccube\Application;
27
use Eccube\Event\EccubeEvents;
28
use Eccube\Event\EventArgs;
29
use Eccube\Form\Type\Front\ForgotType;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception as HttpException;
32
use Symfony\Component\Validator\Constraints as Assert;
33
34
class ForgotController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    /**
37
     * パスワードリマインダ.
38
     *
39
     * @param Application $app
40
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
41
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
42
     */
43 2
    public function index(Application $app, Request $request)
44
    {
45
46 2
        $builder = $app['form.factory']
47 2
            ->createNamedBuilder('', ForgotType::class);
48
49 2
        $event = new EventArgs(
50
            array(
51 2
                'builder' => $builder,
52
            ),
53 2
            $request
54
        );
55 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_FORGOT_INDEX_INITIALIZE, $event);
56
57 2
        $form = $builder->getForm();
58 2
        $form->handleRequest($request);
59
60 2
        if ($form->isSubmitted() && $form->isValid()) {
61 1
            $Customer = $app['eccube.repository.customer']
62 1
                ->getActiveCustomerByEmail($form->get('login_email')->getData());
63
64 1
            if (!is_null($Customer)) {
65
                // リセットキーの発行・有効期限の設定
66
                $Customer
67 1
                    ->setResetKey($app['eccube.repository.customer']->getUniqueResetKey($app))
68 1
                    ->setResetExpire(new \DateTime('+' . $app['config']['customer_reset_expire'] .' min'));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
69
70
                // リセットキーを更新
71 1
                $app['orm.em']->persist($Customer);
72 1
                $app['orm.em']->flush();
73
74 1
                $event = new EventArgs(
75
                    array(
76 1
                        'form' => $form,
77 1
                        'Customer' => $Customer,
78
                    ),
79 1
                    $request
80
                );
81 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_FORGOT_INDEX_COMPLETE, $event);
82
83
                // 完了URLの生成
84 1
                $reset_url = $app->url('forgot_reset', array('reset_key' => $Customer->getResetKey()));
85
86
                // メール送信
87 1
                $app['eccube.service.mail']->sendPasswordResetNotificationMail($Customer, $reset_url);
88
89
                // ログ出力
90 1
                $app['monolog']->addInfo(
91 1
                    'send reset password mail to:'  . "{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}"
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
Coding Style introduced by
Variable "Customer" is not in valid camel caps format
Loading history...
92
                );
93
            } else {
94
                log_warning('Un active customer try send reset password email: ', array('Enter email' => $form->get('login_email')->getData()));
95
            }
96
97 1
            return $app->redirect($app->url('forgot_complete'));
98
        }
99
100 1
        return $app->render('Forgot/index.twig', array(
101 1
            'form' => $form->createView(),
102
        ));
103
    }
104
105
    /**
106
     * パスワードリマインダ完了画面.
107
     *
108
     * @param Application $app
109
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
110
     * @return \Symfony\Component\HttpFoundation\Response
111
     */
112 1
    public function complete(Application $app, Request $request)
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...
113
    {
114 1
        return $app->render('Forgot/complete.twig');
115
    }
116
117
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$reset_key" missing
Loading history...
118
     * パスワード再発行実行画面.
119
     *
120
     * @param Application $app
121
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
122
     * @param $reset_key
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
123
     * @return \Symfony\Component\HttpFoundation\Response
124
     */
125 3
    public function reset(Application $app, Request $request, $reset_key)
126
    {
127 3
        $errors = $app['validator']->validate($reset_key, array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
128 3
            new Assert\NotBlank(),
129 3
            new Assert\Regex(array(
130 3
                'pattern' => '/^[a-zA-Z0-9]+$/',
131
            )))
132
        );
133
134 3
        if ('GET' === $request->getMethod()
135 3
                && count($errors) === 0) {
136
            try {
137 2
                $Customer = $app['eccube.repository.customer']
138 2
                    ->getActiveCustomerByResetKey($reset_key);
139 1
            } catch (\Exception $e) {
140 1
                throw new HttpException\NotFoundHttpException('有効期限が切れているか、無効なURLです。');
141
            }
142
143
            // パスワードの発行・更新
144 1
            $pass = $app['eccube.repository.customer']->getResetPassword();
145 1
            $Customer->setPassword($pass);
146
147
            // 発行したパスワードの暗号化
148 1
            if ($Customer->getSalt() === null) {
149
                $Customer->setSalt($app['eccube.repository.customer']->createSalt(5));
150
            }
151 1
            $encPass = $app['eccube.repository.customer']->encryptPassword($app, $Customer);
152 1
            $Customer->setPassword($encPass);
153
154 1
            $Customer->setResetKey(null);
155
156
            // パスワードを更新
157 1
            $app['orm.em']->persist($Customer);
158 1
            $app['orm.em']->flush();
159
160 1
            $event = new EventArgs(
161
                array(
162 1
                    'Customer' => $Customer,
163
                ),
164 1
                $request
165
            );
166 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_FORGOT_RESET_COMPLETE, $event);
167
168
            // メール送信
169 1
            $app['eccube.service.mail']->sendPasswordResetCompleteMail($Customer, $pass);
170
171
            // ログ出力
172 1
            $app['monolog']->addInfo(
173 1
                'reset password complete:' . "{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}"
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
Coding Style introduced by
Variable "Customer" is not in valid camel caps format
Loading history...
174
            );
175
        } else {
176 1
            throw new HttpException\AccessDeniedHttpException('不正なアクセスです。');
177
        }
178
179 1
        return $app->render('Forgot/reset.twig');
180
    }
181
182
}
183