Total Complexity | 40 |
Total Lines | 233 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class User extends \O2System\Security\Authentication\User |
||
30 | { |
||
31 | /** |
||
32 | * User::$app |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $app = 'app'; |
||
37 | |||
38 | /** |
||
39 | * User::__construct |
||
40 | * |
||
41 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
42 | */ |
||
43 | public function __construct() |
||
53 | } |
||
54 | } |
||
55 | |||
56 | // ------------------------------------------------------------------------ |
||
57 | |||
58 | /** |
||
59 | * User::setApp |
||
60 | * |
||
61 | * @param string $app |
||
62 | * |
||
63 | * @return static |
||
64 | */ |
||
65 | public function setApp($app) |
||
66 | { |
||
67 | if($app = modules()->getApp($app)) { |
||
68 | $this->app = $app; |
||
69 | } |
||
70 | |||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | // ------------------------------------------------------------------------ |
||
75 | |||
76 | /** |
||
77 | * User::authenticate |
||
78 | * |
||
79 | * @param string $username |
||
80 | * @param string $password |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function authenticate($username, $password) |
||
85 | { |
||
86 | if ($user = $this->find($username)) { |
||
87 | if ($user->account) { |
||
88 | if ($this->passwordVerify($password, $user->account->password)) { |
||
89 | if ($this->passwordRehash($password)) { |
||
90 | $user->account->update([ |
||
91 | 'id' => $user->id, |
||
92 | 'password' => $this->passwordHash($password), |
||
93 | ]); |
||
94 | } |
||
95 | |||
96 | $account = $user->account->getArrayCopy(); |
||
97 | } |
||
98 | } elseif ($this->passwordVerify($password, $user->password)) { |
||
99 | $account = $user->getArrayCopy(); |
||
100 | } |
||
101 | |||
102 | if (isset($account)) { |
||
103 | foreach ($account as $key => $value) { |
||
104 | if (strpos($key, 'record') !== false) { |
||
105 | unset($account[ $key ]); |
||
106 | } elseif (in_array($key, |
||
107 | ['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) { |
||
108 | unset($account[ $key ]); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $this->login($account); |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | return false; |
||
119 | } |
||
120 | |||
121 | // ------------------------------------------------------------------------ |
||
122 | |||
123 | /** |
||
124 | * User::find |
||
125 | * |
||
126 | * @param string $username |
||
127 | * |
||
128 | * @return bool|mixed|\O2System\Database\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result |
||
129 | */ |
||
130 | public function find($username) |
||
148 | } |
||
149 | |||
150 | // ------------------------------------------------------------------------ |
||
151 | |||
152 | /** |
||
153 | * User::loggedIn |
||
154 | * |
||
155 | * @return bool |
||
156 | * @throws \Psr\Cache\InvalidArgumentException |
||
157 | */ |
||
158 | public function loggedIn() |
||
159 | { |
||
160 | if (parent::loggedIn()) { |
||
161 | $account = new Account($_SESSION[ 'account' ]); |
||
162 | |||
163 | if ($user = models('users')->findWhere(['username' => $account->username], 1)) { |
||
164 | // Store Account Profile |
||
165 | if ($profile = $user->profile) { |
||
166 | $account->store('profile', $profile); |
||
167 | } |
||
168 | |||
169 | // Store Account Role |
||
170 | if ($role = $user->role) { |
||
171 | $account->store('role', new Role([ |
||
172 | 'label' => $role->label, |
||
173 | 'description' => $role->description, |
||
174 | 'code' => $role->code, |
||
175 | 'authorities' => $role->authorities, |
||
176 | ])); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | // Store Globals Account |
||
181 | globals()->store('account', $account); |
||
182 | |||
183 | // Store Presenter Account |
||
184 | if (services()->has('view')) { |
||
185 | presenter()->store('account', $account); |
||
186 | } |
||
187 | |||
188 | return true; |
||
189 | } |
||
190 | |||
191 | return false; |
||
192 | } |
||
193 | |||
194 | // ------------------------------------------------------------------------ |
||
195 | |||
196 | /** |
||
197 | * User::forceLogin |
||
198 | * |
||
199 | * @param string $username |
||
200 | * @param string $column |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function forceLogin($username, $column = 'username') |
||
205 | { |
||
206 | if (is_numeric($username)) { |
||
207 | $column = 'id'; |
||
208 | } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
||
209 | $column = 'email'; |
||
210 | } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
||
211 | $column = 'msisdn'; |
||
212 | } elseif (strpos($username, 'token-') !== false) { |
||
213 | $username = str_replace('token-', '', $username); |
||
214 | $column = 'token'; |
||
215 | } elseif (strpos($username, 'sso-') !== false) { |
||
216 | $username = str_replace('sso-', '', $username); |
||
217 | $column = 'sso'; |
||
218 | } |
||
219 | |||
220 | if ($account = models('users')->findWhere([$column => $username], 1)) { |
||
221 | $account = $account->getArrayCopy(); |
||
222 | |||
223 | foreach ($account as $key => $value) { |
||
224 | if (strpos($key, 'record') !== false) { |
||
225 | unset($account[ $key ]); |
||
226 | } elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) { |
||
227 | unset($account[ $key ]); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | if ($column === 'token') { |
||
232 | models('users')->update([ |
||
233 | 'id' => $account[ 'id' ], |
||
234 | 'token' => null, |
||
235 | ]); |
||
236 | } |
||
237 | |||
238 | $this->login($account); |
||
239 | |||
240 | return true; |
||
241 | } |
||
242 | |||
243 | return false; |
||
244 | } |
||
245 | |||
246 | // ------------------------------------------------------------------------ |
||
247 | |||
248 | /** |
||
249 | * User::getIframeCode |
||
250 | * |
||
251 | * @return string |
||
252 | * @throws \Psr\Cache\InvalidArgumentException |
||
253 | */ |
||
254 | public function getIframeCode() |
||
262 | } |
||
263 | } |
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.