Completed
Branch 0.2.1 (e70612)
by Anton
09:15
created

Reset::__invoke()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 50
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 50
rs 5.7647
cc 10
eloc 16
nc 8
nop 1

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
namespace Modules\Auth\Controller {
4
5
	use Modules\Auth, Modules\Entitizer, Utils\Security, Str;
6
7
	class Reset {
8
9
		# Invoker
10
11
		public function __invoke(array $post) {
12
13
			if (Auth::check()) return true;
14
15
			# Declare variables
16
17
			$name = ''; $captcha = '';
18
19
			# Extract post array
20
21
			extract($post);
22
23
			# Validate values
24
25
			if (false === ($name = Auth\Validate::userName($name))) return 'USER_ERROR_NAME_INVALID';
26
27
			if (false === Security::checkCaptcha($captcha)) return 'USER_ERROR_CAPTCHA_INCORRECT';
28
29
			# Create user object
30
31
			$user = Entitizer::get(ENTITY_TYPE_USER);
32
33
			# Init user
34
35
			if (!$user->init($name, 'name')) return 'USER_ERROR_NAME_INCORRECT';
36
37
			if (Auth::admin() && ($user->rank < RANK_ADMINISTRATOR)) return 'USER_ERROR_NAME_INCORRECT';
38
39
			# Check access
40
41
			if (!Auth::admin() && ($user->rank === RANK_GUEST)) return 'USER_ERROR_ACCESS';
42
43
			# Create session
44
45
			$secret = Entitizer::get(ENTITY_TYPE_USER_SECRET, $user->id); $secret->remove();
46
47
			$code = Str::random(40); $ip = REQUEST_CLIENT_IP; $time = REQUEST_TIME;
48
49
			$data = ['id' => $user->id, 'code' => $code, 'ip' => $ip, 'time' => $time];
50
51
			if (!$secret->create($data)) return 'USER_ERROR_AUTH_RESET';
52
53
			# Send mail
54
55
			Auth\Utils\Mail::reset($user, $code);
56
57
			# ------------------------
58
59
			return true;
60
		}
61
	}
62
}
63