Passed
Pull Request — master (#16)
by Anton
03:28
created

Reset::__invoke()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 49
Code Lines 16

Duplication

Lines 7
Ratio 14.29 %

Importance

Changes 0
Metric Value
cc 11
eloc 16
nc 10
nop 1
dl 7
loc 49
rs 5.2653
c 0
b 0
f 0

How to fix   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
/**
4
 * @package Cadmium\System\Modules\Auth
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Auth\Controller {
11
12
	use Modules\Auth, Modules\Entitizer, Utils\Security, Utils\Validate, Str;
13
14
	class Reset {
15
16
		private $user = null;
17
18
		/**
19
		 * Constructor
20
		 */
21
22
		public function __construct() {
23
24
			$this->user = Entitizer::get(TABLE_USERS);
25
		}
26
27
		/**
28
		 * Invoker
29
		 *
30
		 * @return true|string|array : true on success, otherwise an error code, or an array of type [$param_name, $error_code],
31
		 *         where $param_name is a name of param that has triggered the error,
32
		 *         and $error_code is a language phrase related to the error
33
		 */
34
35
		public function __invoke(array $post) {
36
37
			# Declare variables
38
39
			$name_email = ''; $captcha = '';
40
41
			# Extract post array
42
43
			extract($post);
44
45
			# Validate values
46
47 View Code Duplication
			if ((false === ($name = Validate::userName($name_email))) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
49
				(false === ($email = Validate::userEmail($name_email)))) return ['name_email', 'USER_ERROR_NAME_INVALID'];
50
51
			if (false === Security::checkCaptcha($captcha)) return ['captcha', 'USER_ERROR_CAPTCHA_INCORRECT'];
52
53
			# Init user
54
55
			$init_by = ((false !== $name) ? 'name' : 'email');
56
57 View Code Duplication
			if ((!$this->user->init($$init_by, $init_by)) || (Auth::isAdmin() && ($this->user->rank < RANK_ADMINISTRATOR))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
59
				return ['name_email', ('USER_ERROR_' . strtoupper($init_by) .'_INCORRECT')];
60
			}
61
62
			# Check access
63
64
			if (!Auth::isAdmin() && ($this->user->rank === RANK_GUEST)) return 'USER_ERROR_ACCESS';
65
66
			# Create secret
67
68
			$secret = Entitizer::get(TABLE_USERS_SECRETS, $this->user->id); $secret->remove();
69
70
			$code = Str::random(40); $ip = REQUEST_CLIENT_IP; $time = REQUEST_TIME;
71
72
			$data = ['id' => $this->user->id, 'code' => $code, 'ip' => $ip, 'time' => $time];
73
74
			if (!$secret->create($data)) return 'USER_ERROR_AUTH_RESET';
75
76
			# Send mail
77
78
			Auth\Utils\Mail::sendPasswordMessage($this->user, $code);
79
80
			# ------------------------
81
82
			return true;
83
		}
84
	}
85
}
86