Passed
Push — master ( 57bfa9...a49d0e )
by Greg
05:39
created

AccountUpdate::handle()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 35
nc 36
nop 1
dl 0
loc 56
rs 8.4266
c 1
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
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\FlashMessages;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Services\UserService;
25
use Fisharebest\Webtrees\Session;
26
use Fisharebest\Webtrees\Tree;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function redirect;
32
use function route;
33
34
/**
35
 * Edit user account details.
36
 */
37
class AccountUpdate implements RequestHandlerInterface
38
{
39
    /** @var UserService */
40
    private $user_service;
41
42
    /**
43
     * AccountController constructor.
44
     *
45
     * @param UserService $user_service
46
     */
47
    public function __construct(UserService $user_service)
48
    {
49
        $this->user_service = $user_service;
50
    }
51
52
    /**
53
     * @param ServerRequestInterface $request
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59
        $tree   = $request->getAttribute('tree');
60
        $user   = $request->getAttribute('user');
61
        $params = $request->getParsedBody();
62
63
        $contact_method = $params['contact_method'];
64
        $email          = $params['email'];
65
        $language       = $params['language'];
66
        $real_name      = $params['real_name'];
67
        $password       = $params['password'];
68
        $time_zone      = $params['timezone'];
69
        $user_name      = $params['user_name'];
70
        $visible_online = $params['visible_online'] ?? '';
71
72
        // Change the password
73
        if ($password !== '') {
74
            $user->setPassword($password);
75
        }
76
77
        // Change the username
78
        if ($user_name !== $user->userName()) {
79
            if ($this->user_service->findByUserName($user_name) === null) {
80
                $user->setUserName($user_name);
81
            } else {
82
                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
83
            }
84
        }
85
86
        // Change the email
87
        if ($email !== $user->email()) {
88
            if ($this->user_service->findByEmail($email) === null) {
89
                $user->setEmail($email);
90
            } else {
91
                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
92
            }
93
        }
94
95
        $user
96
            ->setRealName($real_name)
97
            ->setPreference('contactmethod', $contact_method)
98
            ->setPreference('language', $language)
99
            ->setPreference('TIMEZONE', $time_zone)
100
            ->setPreference('visibleonline', $visible_online);
101
102
        if ($tree instanceof Tree) {
103
            $rootid = $params['root_id'];
104
            $tree->setUserPreference($user, 'rootid', $rootid);
105
        }
106
107
        // Switch to the new language now
108
        Session::put('language', $language);
109
110
        FlashMessages::addMessage(I18N::translate('The details for ā€œ%sā€ have been updated.', e($user->username())), 'success');
111
112
        return redirect(route(HomePage::class, ['tree' => $tree->name()]));
113
    }
114
}
115