Failed Conditions
Pull Request — master (#1340)
by Tsuyoshi
16:42
created

ForgotController::index()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6.7441

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
ccs 4
cts 9
cp 0.4444
rs 8.5806
cc 4
eloc 21
nc 3
nop 2
crap 6.7441
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 Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\Validator\Constraints as Assert;
29
use Symfony\Component\HttpKernel\Exception as HttpException;
30
31
class ForgotController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
32
{
33
34 2
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
35
    {
36
37
        $form = $app['form.factory']
38
            ->createNamedBuilder('', 'forgot')
39
            ->getForm();
40
        $form->handleRequest($request);
41
42
        if ($form->isSubmitted() && $form->isValid()) {
43
            $Customer = $app['eccube.repository.customer']
44
                ->getActiveCustomerByEmail($form->get('login_email')->getData());
45
46
            if (!is_null($Customer)) {
47
                // リセットキーの発行・有効期限の設定
48
                $Customer
49
                    ->setResetKey($app['eccube.repository.customer']->getUniqueResetKey($app))
50
                    ->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...
51
52
                // リセットキーを更新
53
                $app['orm.em']->persist($Customer);
54
                $app['orm.em']->flush();
55
56
                // 完了URLの生成
57
                $reset_url = $app->url('forgot_reset', array('reset_key' => $Customer->getResetKey()));
58
59
                // メール送信
60
                $app['eccube.service.mail']->sendPasswordResetNotificationMail($Customer, $reset_url);
61
62
                // ログ出力
63
                $app['monolog']->addInfo(
64
                    '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...
65
                );
66
            }
67
68
            return $app->redirect($app->url('forgot_complete'));
69
        }
70
71 1
        return $app->render('Forgot/index.twig', array(
72 1
            'form' => $form->createView(),
73
        ));
74 2
    }
75
76 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...
introduced by
Missing function doc comment
Loading history...
77
    {
78
        return $app->render('Forgot/complete.twig');
79 1
    }
80
81 3
    public function reset(Application $app, Request $request, $reset_key)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
82
    {
83
        $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...
84
            new Assert\NotBlank(),
85
            new Assert\Regex(array(
86
                'pattern' => '/^[a-zA-Z0-9]+$/',
87
            )))
88
        );
89
90
        if ('GET' === $request->getMethod()
91
                && count($errors) === 0) {
92
            try {
93
                $Customer = $app['eccube.repository.customer']
94
                    ->getActiveCustomerByResetKey($reset_key);
95
            } catch (\Exception $e) {
96
                throw new HttpException\NotFoundHttpException('有効期限が切れているか、無効なURLです。');
97 2
            }
98
99
            // パスワードの発行・更新
100
            $pass = $app['eccube.repository.customer']->getResetPassword();
101
            $Customer->setPassword($pass);
102
103
            // 発行したパスワードの暗号化
104
            $encPass = $app['eccube.repository.customer']->encryptPassword($app, $Customer);
105
            $Customer->setPassword($encPass);
106
107
            $Customer->setResetKey(null);
108
109
            // パスワードを更新
110
            $app['orm.em']->persist($Customer);
111
            $app['orm.em']->flush();
112
113
            // メール送信
114
            $app['eccube.service.mail']->sendPasswordResetCompleteMail($Customer, $pass);
115
116
            // ログ出力
117
            $app['monolog']->addInfo(
118
                '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...
119
            );
120
        } else {
121
            throw new HttpException\AccessDeniedHttpException('不正なアクセスです。');
122 1
        }
123
124
        return $app->render('Forgot/reset.twig');
125 3
    }
126
127
}
128