EditUser::doSubmitAction()   C
last analyzed

Complexity

Conditions 13
Paths 32

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 30
nc 32
nop 0
dl 0
loc 59
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Edit user page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use InvalidArgumentException;
12
use EBloodBank as EBB;
13
use EBloodBank\Notices;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * Edit user page controller class
18
 *
19
 * @since 1.0
20
 */
21
class EditUser extends Controller
22
{
23
    /**
24
     * @var   int
25
     * @since 1.6
26
     */
27
    protected $userId = 0;
28
29
    /**
30
     * @var \EBloodBank\Models\User|null
31
     * @since 1.0
32
     */
33
    protected $user;
34
35
    /**
36
     * @since 1.0
37
     */
38
    public function __construct(ContainerInterface $container, $userId)
39
    {
40
        parent::__construct($container);
41
        if (EBB\isValidID($userId)) {
0 ignored issues
show
Bug introduced by
The function isValidID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        if (/** @scrutinizer ignore-call */ EBB\isValidID($userId)) {
Loading history...
42
            $this->userId = (int) $userId;
43
        }
44
    }
45
46
    /**
47
     * @return void
48
     * @since 1.0
49
     */
50
    public function __invoke()
51
    {
52
        if (! $this->hasAuthenticatedUser()) {
53
            $this->viewFactory->displayView('error-403');
54
            return;
55
        }
56
57
        if ($this->userId) {
58
            $this->user = $this->getUserRepository()->find($this->userId);
59
        }
60
61
        if (! $this->user) {
62
            $this->viewFactory->displayView('error-404');
63
            return;
64
        }
65
66
        $user = $this->user;
67
68
        if (! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $user)) {
69
            $this->viewFactory->displayView('error-403');
70
            return;
71
        }
72
73
        $this->doActions();
74
        $this->addNotices();
75
        $this->viewFactory->displayView(
76
            'edit-user',
77
            [
78
                'user' => $user,
79
            ]
80
        );
81
    }
82
83
    /**
84
     * @return void
85
     * @since 1.0
86
     */
87
    protected function doActions()
88
    {
89
        switch (filter_input(INPUT_POST, 'action')) {
90
            case 'submit_user':
91
                $this->doSubmitAction();
92
                break;
93
        }
94
    }
95
96
    /**
97
     * @return void
98
     * @since 1.0
99
     */
100
    protected function addNotices()
101
    {
102
        if (filter_has_var(INPUT_GET, 'flag-edited')) {
103
            Notices::addNotice('edited', __('User edited.'), 'success');
104
        }
105
    }
106
107
    /**
108
     * @return void
109
     * @since 1.0
110
     */
111
    protected function doSubmitAction()
112
    {
113
        try {
114
            $sessionToken = $this->getSession()->getCsrfToken();
115
            $actionToken = filter_input(INPUT_POST, 'token');
116
117
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
118
                return;
119
            }
120
121
            $user = $this->user;
122
123
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $user)) {
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $resource of EBloodBank\AclInterface::canEditEntity() does only seem to accept EBloodBank\Models\Entity, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $user)) {
Loading history...
124
                return;
125
            }
126
127
            // Set the user name.
128
            $user->set('name', filter_input(INPUT_POST, 'user_name'), true);
0 ignored issues
show
Bug introduced by
The method set() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            $user->/** @scrutinizer ignore-call */ 
129
                   set('name', filter_input(INPUT_POST, 'user_name'), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130
            // Set the user email.
131
            $user->set('email', filter_input(INPUT_POST, 'user_email'), true);
132
133
            $duplicateUser = $this->getUserRepository()->findOneBy(['email' => $user->get('email'), 'status' => 'any']);
134
135
            if (! empty($duplicateUser) && $duplicateUser->get('id') != $user->get('id')) {
136
                throw new InvalidArgumentException(__('Please enter a unique user e-mail.'));
137
            }
138
139
            $userPass1 = filter_input(INPUT_POST, 'user_pass_1', FILTER_UNSAFE_RAW);
140
            $userPass2 = filter_input(INPUT_POST, 'user_pass_2', FILTER_UNSAFE_RAW);
141
142
            if (! empty($userPass1) xor ! empty($userPass2)) {
143
                throw new InvalidArgumentException(__('Please enter the password twice.'));
144
            }
145
146
            if (! empty($userPass1) && ! empty($userPass2)) {
147
                if ($userPass1 !== $userPass2) {
148
                    throw new InvalidArgumentException(__('Please enter the same password.'));
149
                }
150
151
                // Set the user password.
152
                $user->set('pass', password_hash($userPass1, PASSWORD_BCRYPT), false);
153
            }
154
155
            // Set the user role.
156
            if ($user->get('id') != $this->getAuthenticatedUser()->get('id')) {
157
                $user->set('role', filter_input(INPUT_POST, 'user_role'), true);
158
            }
159
160
            $this->getEntityManager()->flush($user);
161
162
            EBB\redirect(
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
            /** @scrutinizer ignore-call */ 
163
            EBB\redirect(
Loading history...
163
                EBB\addQueryArgs(
0 ignored issues
show
Bug introduced by
The function addQueryArgs was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
                /** @scrutinizer ignore-call */ 
164
                EBB\addQueryArgs(
Loading history...
164
                    EBB\getEditUserURL($user->get('id')),
0 ignored issues
show
Bug introduced by
The function getEditUserURL was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
                    /** @scrutinizer ignore-call */ 
165
                    EBB\getEditUserURL($user->get('id')),
Loading history...
165
                    ['flag-edited' => true]
166
                )
167
            );
168
        } catch (InvalidArgumentException $ex) {
169
            Notices::addNotice('invalid_user_argument', $ex->getMessage());
170
        }
171
    }
172
}
173