Conditions | 13 |
Paths | 73 |
Total Lines | 97 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
74 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
75 | { |
||
76 | $user = $request->getAttribute('user'); |
||
77 | |||
78 | $params = (array) $request->getParsedBody(); |
||
79 | |||
80 | $user_id = (int) $params['user_id']; |
||
81 | $username = $params['username'] ?? ''; |
||
82 | $real_name = $params['real_name'] ?? ''; |
||
83 | $email = $params['email'] ?? ''; |
||
84 | $password = $params['password'] ?? ''; |
||
85 | $theme = $params['theme'] ?? ''; |
||
86 | $language = $params['language'] ?? ''; |
||
87 | $timezone = $params['timezone'] ?? ''; |
||
88 | $contact_method = $params['contact-method'] ?? ''; |
||
89 | $comment = $params['comment'] ?? ''; |
||
90 | $auto_accept = (bool) ($params[UserInterface::PREF_AUTO_ACCEPT_EDITS] ?? ''); |
||
91 | $canadmin = (bool) ($params[UserInterface::PREF_IS_ADMINISTRATOR] ?? ''); |
||
92 | $visible_online = (bool) ($params['visible-online'] ?? ''); |
||
93 | $verified = (bool) ($params[UserInterface::PREF_IS_EMAIL_VERIFIED] ?? ''); |
||
94 | $approved = (bool) ($params['approved'] ?? ''); |
||
95 | |||
96 | $edit_user = $this->user_service->find($user_id); |
||
97 | |||
98 | if ($edit_user === null) { |
||
99 | throw new HttpNotFoundException(I18N::translate('%1$s does not exist', 'user_id:' . $user_id)); |
||
100 | } |
||
101 | |||
102 | // We have just approved a user. Tell them |
||
103 | if ($approved && $edit_user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) !== '1') { |
||
104 | I18N::init($edit_user->getPreference(UserInterface::PREF_LANGUAGE)); |
||
105 | |||
106 | $base_url = $request->getAttribute('base_url'); |
||
107 | |||
108 | $this->email_service->send( |
||
109 | new SiteUser(), |
||
110 | $edit_user, |
||
111 | Auth::user(), |
||
112 | /* I18N: %s is a server name/URL */ |
||
113 | I18N::translate('New user at %s', $base_url), |
||
114 | view('emails/approve-user-text', ['user' => $edit_user, 'base_url' => $base_url]), |
||
115 | view('emails/approve-user-html', ['user' => $edit_user, 'base_url' => $base_url]) |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $edit_user->setRealName($real_name); |
||
120 | $edit_user->setPreference(UserInterface::PREF_THEME, $theme); |
||
121 | $edit_user->setPreference(UserInterface::PREF_LANGUAGE, $language); |
||
122 | $edit_user->setPreference(UserInterface::PREF_TIME_ZONE, $timezone); |
||
123 | $edit_user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method); |
||
124 | $edit_user->setPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT, $comment); |
||
125 | $edit_user->setPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS, (string) $auto_accept); |
||
126 | $edit_user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online); |
||
127 | $edit_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, (string) $verified); |
||
128 | $edit_user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, (string) $approved); |
||
129 | |||
130 | if ($password !== '') { |
||
131 | $edit_user->setPassword($password); |
||
132 | } |
||
133 | |||
134 | // We cannot change our own admin status. Another admin will need to do it. |
||
135 | if ($edit_user->id() !== $user->id()) { |
||
136 | $edit_user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, $canadmin ? '1' : ''); |
||
137 | } |
||
138 | |||
139 | foreach ($this->tree_service->all() as $tree) { |
||
140 | $path_length = (int) $params['RELATIONSHIP_PATH_LENGTH' . $tree->id()]; |
||
141 | $gedcom_id = $params['gedcomid' . $tree->id()] ?? ''; |
||
142 | $can_edit = $params['canedit' . $tree->id()] ?? ''; |
||
143 | |||
144 | // Do not allow a path length to be set if the individual ID is not |
||
145 | if ($gedcom_id === '') { |
||
146 | $path_length = 0; |
||
147 | } |
||
148 | |||
149 | $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_ACCOUNT_XREF, $gedcom_id); |
||
150 | $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_ROLE, $can_edit); |
||
151 | $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_PATH_LENGTH, (string) $path_length); |
||
152 | } |
||
153 | |||
154 | if ($edit_user->email() !== $email && $this->user_service->findByEmail($email) instanceof User) { |
||
155 | FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.') . $email, 'danger'); |
||
156 | |||
157 | return redirect(route('admin-users-edit', ['user_id' => $edit_user->id()])); |
||
158 | } |
||
159 | |||
160 | if ($edit_user->userName() !== $username && $this->user_service->findByUserName($username) instanceof User) { |
||
161 | FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'), 'danger'); |
||
162 | |||
163 | return redirect(route(UserEditPage::class, ['user_id' => $edit_user->id()])); |
||
164 | } |
||
165 | |||
166 | $edit_user |
||
167 | ->setEmail($email) |
||
168 | ->setUserName($username); |
||
169 | |||
170 | return redirect(route(UserListPage::class)); |
||
171 | } |
||
173 |