Total Complexity | 42 |
Total Lines | 443 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SingUp 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 SingUp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class SingUp extends AbstractionController |
||
17 | { |
||
18 | use \Drone\Error\ErrorTrait; |
||
19 | |||
20 | /** |
||
21 | * @var UsersEntity |
||
22 | */ |
||
23 | private $userAdapter; |
||
24 | |||
25 | /** |
||
26 | * @return UsersEntity |
||
27 | */ |
||
28 | private function getUserAdapter() |
||
29 | { |
||
30 | if (!is_null($this->userAdapter)) |
||
31 | return $this->userAdapter; |
||
32 | |||
33 | $this->userAdapter = new EntityAdapter(new UserTbl(new User())); |
||
34 | |||
35 | return $this->userAdapter; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Checks user session and redirect to other module if exists any active session |
||
40 | * |
||
41 | * @return null |
||
42 | */ |
||
43 | private function checkSession() |
||
44 | { |
||
45 | $config = include 'module/Auth/config/user.config.php'; |
||
46 | $method = $config["authentication"]["method"]; |
||
47 | $key = $config["authentication"]["key"]; |
||
48 | |||
49 | $global_config = include 'config/global.config.php'; |
||
50 | |||
51 | /** URL TO REDIRECT: |
||
52 | * |
||
53 | * By default if there isn't a session active, it will be redirected to the module in the user config file ($config["redirect"]). |
||
54 | * If a last URI requested exists, it will be redirecto to it. |
||
55 | * |
||
56 | * Other modules must have the following line of code inside init method to ensure last uri redirection. |
||
57 | * $_SESSION["last_uri_" . $global_config["project"]["id"]] = $_SERVER["REQUEST_URI"]; |
||
58 | * It should be an unique session id for the app to prevent bad redirections with other projects. |
||
59 | */ |
||
60 | if (array_key_exists("last_uri_" . $global_config["project"]["id"], $_SESSION) || !empty($_SESSION["last_uri_" . $global_config["project"]["id"]])) |
||
61 | $location = $_SESSION["last_uri_" . $global_config["project"]["id"]]; |
||
62 | else |
||
63 | $location = $this->getBasePath() . "/public/" . $config["redirect"]; |
||
64 | |||
65 | switch ($method) |
||
66 | { |
||
67 | case '_COOKIE': |
||
68 | |||
69 | if (array_key_exists($key, $_COOKIE) || !empty($_COOKIE[$key])) |
||
70 | header("location: " . $location); |
||
71 | |||
72 | break; |
||
73 | |||
74 | case '_SESSION': |
||
75 | |||
76 | if (array_key_exists($key, $_SESSION) || !empty($_SESSION[$key])) |
||
77 | header("location: " . $location); |
||
78 | |||
79 | break; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Shows register form |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function index() |
||
89 | { |
||
90 | # STANDARD VALIDATIONS [check method] |
||
91 | if (!$this->isGet()) |
||
92 | { |
||
93 | $http = new Http(); |
||
94 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
95 | |||
96 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
97 | } |
||
98 | |||
99 | $this->checkSession(); |
||
100 | |||
101 | return []; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Does register process |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function attemp() |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Does email checking |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | public function verifyEmail() |
||
328 | { |
||
329 | # data to send |
||
330 | $data = []; |
||
331 | |||
332 | # TRY-CATCH-BLOCK |
||
333 | try { |
||
334 | |||
335 | # STANDARD VALIDATIONS [check method] |
||
336 | if (!$this->isGet()) |
||
337 | { |
||
338 | $http = new Http(); |
||
339 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
340 | |||
341 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
342 | } |
||
343 | |||
344 | # STANDARD VALIDATIONS [check needed arguments] |
||
345 | $needles = ['token', 'user']; |
||
346 | |||
347 | array_walk($needles, function($item) { |
||
348 | if (!array_key_exists($item, $_GET)) |
||
349 | { |
||
350 | $http = new Http(); |
||
351 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
352 | |||
353 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
354 | } |
||
355 | }); |
||
356 | |||
357 | $config = include 'module/Auth/config/user.config.php'; |
||
358 | $username_str = $config["authentication"]["gateway"]["credentials"]["username"]; |
||
359 | $state_field = $config["authentication"]["gateway"]["table_info"]["columns"]["state_field"]; |
||
360 | |||
361 | $pending_state = $config["authentication"]["gateway"]["table_info"]["column_values"]["state_field"]["pending_email"]; |
||
362 | $active_state = $config["authentication"]["gateway"]["table_info"]["column_values"]["state_field"]["user_active"]; |
||
363 | |||
364 | # catch arguments |
||
365 | $token = $_GET["token"]; |
||
366 | $user = $_GET["user"]; |
||
367 | |||
368 | $row = $this->getUserAdapter()->select([ |
||
369 | $username_str => $user, |
||
370 | "TOKEN" => $token |
||
371 | ]); |
||
372 | |||
373 | if (!count($row)) |
||
374 | throw new \Drone\Exception\Exception("Token has expired or username does not exists!."); |
||
375 | |||
376 | $user = array_shift($row); |
||
377 | |||
378 | if ($user->{$state_field} <> $pending_state) |
||
379 | throw new \Drone\Exception\Exception("This email address had verified before!.", 300); |
||
380 | |||
381 | $user->exchangeArray([ |
||
382 | $state_field => $active_state |
||
383 | ]); |
||
384 | |||
385 | $this->getUserAdapter()->update($user, [ |
||
386 | $username_str => $user->{$username_str} |
||
387 | ]); |
||
388 | |||
389 | # SUCCESS-MESSAGE |
||
390 | $data["process"] = "success"; |
||
391 | } |
||
392 | catch (\Drone\Exception\Exception $e) |
||
393 | { |
||
394 | # ERROR-MESSAGE |
||
395 | $data["process"] = "warning"; |
||
396 | $data["message"] = $e->getMessage(); |
||
397 | } |
||
398 | catch (\Exception $e) |
||
399 | { |
||
400 | $file = str_replace('\\', '', __CLASS__); |
||
401 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
402 | |||
403 | # stores the error code |
||
404 | if (($errorCode = $storage->store($e)) === false) |
||
405 | { |
||
406 | $errors = $storage->getErrors(); |
||
407 | |||
408 | # if error storing is not possible, handle it (internal app error) |
||
409 | $this->handleErrors($errors, __METHOD__); |
||
410 | } |
||
411 | |||
412 | # errors retrived by the use of ErrorTrait |
||
413 | if (count($this->getErrors())) |
||
414 | $this->handleErrors($this->getErrors(), __METHOD__); |
||
415 | |||
416 | $data["code"] = $errorCode; |
||
417 | $data["message"] = $e->getMessage(); |
||
418 | |||
419 | $config = include 'config/application.config.php'; |
||
420 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
421 | |||
422 | # redirect view |
||
423 | $this->setMethod('error'); |
||
424 | |||
425 | return $data; |
||
426 | } |
||
427 | |||
428 | return $data; |
||
429 | } |
||
430 | |||
431 | private function handleErrors(Array $errors, $method) |
||
459 | } |
||
460 | } |
||
461 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths