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

Recover::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Validate, Str;
13
14
	class Recover {
15
16
		private $user = null;
17
18
		/**
19
		 * Constructor
20
		 */
21
22
		public function __construct(Entitizer\Entity\User $user) {
23
24
			$this->user = $user;
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
			$password_new = ''; $password_retype = '';
40
41
			# Extract post array
42
43
			extract($post);
44
45
			# Validate values
46
47
			if (false === ($password_new = Validate::userPassword($password_new)))
48
49
				return ['password_new', 'USER_ERROR_PASSWORD_NEW_INVALID'];
50
51
			if (0 !== strcmp($password_new, $password_retype))
52
53
				return ['password_retype', 'USER_ERROR_PASSWORD_MISMATCH'];
54
55
			# Encode password
56
57
			$auth_key = Str::random(40); $password = Str::encode($auth_key, $password_new);
58
59
			# Update user
60
61
			$data = ['auth_key' => $auth_key, 'password' => $password];
62
63
			if (!$this->user->edit($data)) return 'USER_ERROR_AUTH_RECOVER';
64
65
			# Remove secret
66
67
			Entitizer::get(TABLE_USERS_SECRETS, $this->user->id)->remove();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Entity\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
69
			# ------------------------
70
71
			return true;
72
		}
73
	}
74
}
75