Total Complexity | 48 |
Total Lines | 391 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 1 |
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 |
||
23 | class user |
||
24 | { |
||
25 | /** |
||
26 | * [$DB Data base object] |
||
27 | * @var object |
||
28 | */ |
||
29 | private $DB; |
||
30 | |||
31 | /** |
||
32 | * [$name Account username] |
||
33 | * @var string |
||
34 | */ |
||
35 | private $name; |
||
36 | |||
37 | /** |
||
38 | * [$name Account email address] |
||
39 | * @var string |
||
40 | */ |
||
41 | private $mail; |
||
42 | |||
43 | /** |
||
44 | * [$ACCOUNT_ID] |
||
45 | * @var string |
||
46 | */ |
||
47 | private $ACCOUNT_ID; |
||
48 | |||
49 | /** |
||
50 | * [$timestamp Time now] |
||
51 | * @var integer |
||
52 | */ |
||
53 | protected $timestamp; |
||
54 | |||
55 | /** |
||
56 | * [$bucketToken] |
||
57 | * @var string |
||
58 | */ |
||
59 | private $bucketToken; |
||
60 | |||
61 | /** |
||
62 | * [$options Resposible API options] |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $options; |
||
66 | |||
67 | /** |
||
68 | * [$credentials User credentials] |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $credentials; |
||
72 | |||
73 | /** |
||
74 | * [create Create a new access account] |
||
75 | * @return array |
||
76 | */ |
||
77 | public function create() |
||
78 | { |
||
79 | return (new userCreate($this->credentials)) |
||
80 | ->setOptions($this->getOptions()) |
||
81 | ->createAccount() |
||
82 | ; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * [update Update an access account] |
||
87 | * @return array |
||
88 | */ |
||
89 | public function update($properties) |
||
90 | { |
||
91 | return $this->updateAccount($properties); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * [load Load a stored account] |
||
96 | * @return array |
||
97 | */ |
||
98 | public function load($property, array $options) |
||
103 | ; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * [updateAccountAccess Update the requests account access] |
||
108 | * @return void |
||
109 | */ |
||
110 | public function updateAccountAccess($ACCOUNT_ID = null) |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * [updateAccess Update access for limit requests] |
||
130 | * @return boolean |
||
131 | */ |
||
132 | private function updateAccess() |
||
147 | ) |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * [updateAccount Update access for limit requests] |
||
153 | * @return boolean |
||
154 | */ |
||
155 | private function updateAccount($properties) |
||
156 | { |
||
157 | if( is_array($properties) ) { |
||
158 | $properties = (object) $properties; |
||
159 | } |
||
160 | |||
161 | $this->checkUpdateProperties($properties); |
||
162 | |||
163 | $updateSet = $this->buildUpdateSet($properties); |
||
164 | |||
165 | return $this->DB()-> |
||
166 | query( |
||
167 | "UPDATE responsible_api_users USR |
||
168 | set {$updateSet['set']} |
||
169 | WHERE {$updateSet['where']} |
||
170 | ;", |
||
171 | $updateSet['binds'] |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * [checkUpdateProperties Check if we have the correct update properties] |
||
177 | * @param object $properties |
||
178 | * @return void |
||
179 | */ |
||
180 | private function checkUpdateProperties($properties) |
||
181 | { |
||
182 | if (!isset($properties->update) || |
||
183 | !isset($properties->where) || |
||
184 | (isset($properties->update) && !is_array($properties->update)) || |
||
185 | (isset($properties->where) && !is_array($properties->where)) |
||
186 | ) { |
||
187 | (new exception\errorException) |
||
188 | ->message('No update property was provided. Please read the documentation on updating user accounts.') |
||
189 | ->error('ACCOUNT_UPDATE'); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * [buildUpdateSet description] |
||
195 | * @param object $properties |
||
196 | * @return array |
||
197 | */ |
||
198 | private function buildUpdateSet($properties) |
||
199 | { |
||
200 | $allowedFileds = $binds = []; |
||
201 | $set = ''; |
||
202 | |||
203 | $columns = $this->DB()->query("SHOW COLUMNS FROM responsible_api_users"); |
||
204 | |||
205 | foreach ($columns as $f => $field) { |
||
206 | $allowedFileds[] = $field['Field']; |
||
207 | } |
||
208 | |||
209 | foreach ($properties->update as $u => $update) { |
||
210 | if( !in_array($u, $allowedFileds) ) { |
||
211 | unset($properties->update[$u]); |
||
212 | }else{ |
||
213 | $set .= $u . ' = :' . $u . ','; |
||
214 | $binds[$u] = $update; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | $set = rtrim($set, ','); |
||
219 | $where = key($properties->where) . ' = ' . $properties->where[key($properties->where)]; |
||
220 | |||
221 | return [ |
||
222 | 'set' => $set, |
||
223 | 'where' => $where, |
||
224 | 'binds' => $binds |
||
225 | ]; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * [credentials Set the new account credentials] |
||
230 | * @param string $name |
||
231 | * username |
||
232 | * @param string $mail |
||
233 | * email address |
||
234 | */ |
||
235 | public function credentials($name, $mail) |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * [validate - Validate the new account credentials] |
||
255 | * @return boolean |
||
256 | */ |
||
257 | private function validate($type, $property) |
||
258 | { |
||
259 | $options = $this->getOptions(); |
||
260 | $skipValidatation = false; |
||
261 | |||
262 | if( isset($options['validate']) && $options['validate'] == false ) { |
||
263 | $skipValidatation = true; |
||
264 | } |
||
265 | |||
266 | switch ($type) { |
||
267 | |||
268 | case 'name': |
||
269 | if (!is_string($property) && !$skipValidatation) { |
||
270 | return false; |
||
271 | } |
||
272 | $this->name = preg_replace('/\s+/', '-', strtolower($property)); |
||
273 | |||
274 | return true; |
||
275 | break; |
||
|
|||
276 | |||
277 | case 'mail': |
||
278 | if( !filter_var($property, FILTER_VALIDATE_EMAIL) && !$skipValidatation) { |
||
279 | return false; |
||
280 | } |
||
281 | $this->mail = $property; |
||
282 | |||
283 | return true; |
||
284 | break; |
||
285 | } |
||
286 | |||
287 | return false; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * [getJWT Get the JWT payload] |
||
292 | * @return array |
||
293 | */ |
||
294 | protected function getJWT($key) |
||
295 | { |
||
296 | $token = (new headers\header)->authorizationHeaders(true); |
||
297 | $jwt = new auth\jwt; |
||
298 | $decoded = $jwt->token($token) |
||
299 | ->key($key) |
||
300 | ->decode() |
||
301 | ; |
||
302 | |||
303 | return [ |
||
304 | 'token' => $token, |
||
305 | 'payload' => $decoded, |
||
306 | ]; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * [DB Get DB object] |
||
311 | */ |
||
312 | protected function DB() |
||
313 | { |
||
314 | if (is_null($this->DB)) { |
||
315 | $defaults = $this->getDefaults(); |
||
316 | $config = $defaults['config']; |
||
317 | |||
318 | $this->DB = new connect\DB($config['DB_HOST'], $config['DB_NAME'], $config['DB_USER'], $config['DB_PASSWORD']); |
||
319 | } |
||
320 | return $this->DB; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * [getDefaults Get the Responsible API defaults ] |
||
325 | * @return array |
||
326 | */ |
||
327 | protected function getDefaults() |
||
328 | { |
||
329 | $config = new configuration\config; |
||
330 | $config->responsibleDefault(); |
||
331 | return $config->getDefaults(); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * [setOptions Set the REsponsible API options] |
||
336 | * @param array $options |
||
337 | */ |
||
338 | public function setOptions($options) |
||
339 | { |
||
340 | $this->options = $options; |
||
341 | return $this; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * [getOptions Get the Responsible API options] |
||
346 | * @return array |
||
347 | */ |
||
348 | protected function getOptions() |
||
349 | { |
||
350 | return $this->options; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * [timeNow Create a timestamp of now] |
||
355 | * @return integer |
||
356 | */ |
||
357 | protected function timeNow() |
||
358 | { |
||
359 | $this->timestamp = (new \DateTime('now'))->getTimestamp(); |
||
360 | return $this->timestamp; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * [setAccountID] |
||
365 | */ |
||
366 | public function setAccountID($ACCOUNT_ID) |
||
367 | { |
||
368 | $this->ACCOUNT_ID = $ACCOUNT_ID; |
||
369 | return $this; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * [getAccountID] |
||
374 | * @return string |
||
375 | */ |
||
376 | protected function getAccountID() |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * [setBucket Bucket data token] |
||
383 | * @param string $packed |
||
384 | */ |
||
385 | public function setBucketToken($packed) |
||
386 | { |
||
387 | $this->bucketToken = $packed; |
||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * [getBucketToken Bucket data token] |
||
393 | * @param string $packed |
||
394 | */ |
||
395 | public function getBucketToken() |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * [getClaim Check if a claim is set and not empty] |
||
402 | * @param string $claim |
||
403 | * @return mixed |
||
404 | */ |
||
405 | public function checkVal($option, $key, $default = false) |
||
414 | } |
||
415 | } |
||
416 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.