Passed
Push — master ( 502cab...a00bcc )
by Greg
05:16
created

PasswordResetForm   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 19 2
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2019 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\RequestHandlers;
19
20
use Fig\Http\Message\StatusCodeInterface;
21
use Fisharebest\Webtrees\Auth;
22
use Fisharebest\Webtrees\FlashMessages;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Log;
26
use Fisharebest\Webtrees\Services\UserService;
27
use Fisharebest\Webtrees\User;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
use function redirect;
34
use function response;
35
36
/**
37
 * Set a new password.
38
 */
39
class PasswordResetForm implements RequestHandlerInterface, StatusCodeInterface
40
{
41
    use ViewResponseTrait;
42
43
    /** @var UserService */
44
    private $user_service;
45
46
    /**
47
     * PasswordResetForm constructor.
48
     *
49
     * @param UserService $user_service
50
     */
51
    public function __construct(UserService $user_service)
52
    {
53
54
        $this->user_service = $user_service;
55
    }
56
57
    /**
58
     * @param ServerRequestInterface $request
59
     *
60
     * @return ResponseInterface
61
     */
62
    public function handle(ServerRequestInterface $request): ResponseInterface
63
    {
64
        $title = I18N::translate('Set a new password');
65
66
        $token = $request->getQueryParams()['token'] ?? '';
67
68
        $user = $this->user_service->findByToken($token);
69
70
        if ($user instanceof User) {
71
            return $this->viewResponse('password-reset-page', ['title' => $title, 'user' => $user, 'token' => $token]);
72
        }
73
74
        $message1 = I18N::translate('The password reset link has expired.');
75
        $message2 = I18N::translate('Please try again.');
76
        $message  = $message1 . '<br>' . $message2;
77
78
        FlashMessages::addMessage($message, 'danger');
79
80
        return redirect(route('password-request'));
81
    }
82
}
83