Completed
Pull Request — master (#1504)
by Kentaro
39:20
created

ForgotController::index()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 58
ccs 7
cts 9
cp 0.7778
rs 9.0077
cc 4
eloc 32
nc 3
nop 2
crap 4.1755

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