Completed
Push — master ( e32bea...07ff99 )
by Wanderson
02:49
created

UserDAO::insertFirst()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace Win\Authentication;
4
5
use Win\DAO\DAO;
6
use Win\Authentication\User;
7
8
/**
9
 * User DAO
10
 */
11
class UserDAO extends DAO implements UserDAOInterface {
12
13
	const TABLE = 'user';
14
	const ALIAS = 'Usuário';
15
16
	/** @var User */
17
	protected $obj;
18
19
	/**
20
	 * @return string|null
21
	 */
22
	protected function validate() {
23
		if (strlen($this->obj->getName()) < 2) {
24
			return 'O campo Nome deve possuir pelo menos 2 caracteres.';
25
		} elseif (strlen($this->obj->getEmail()) == 0) {
26
			return 'O campo E-mail deve ser preenchido.';
27
		} elseif (!filter_var($this->obj->getEmail(), FILTER_VALIDATE_EMAIL)) {
28
			return 'O campo E-mail deve ser um e-mail válido.';
29
		} elseif (strlen($this->obj->getPassword()) < 4) {
30
			return 'A senha deve possuir pelo menos 4 caracteres.';
31
		}
32
		return null;
33
	}
34
35
	/**
36
	 * @param array $row
37
	 * @return User
38
	 */
39
	protected function mapObject($row) {
40
		$obj = new User();
41
		$obj->setId($row['id']);
42
		$obj->setActive($row['is_active']);
43
		$obj->setAccessLevel($row['access_level']);
44
		$obj->setGroupId($row['group_id']);
45
		$obj->setName($row['name']);
46
		$obj->setEmail($row['email']);
47
		$obj->setPasswordHash($row['password_hash']);
48
		$obj->setRecoreryHash($row['recovery_hash']);
49
		$obj->setImage($row['image']);
50
		$obj->setLastLogin($row['last_login']);
51
		return $obj;
52
	}
53
54
	/**
55
	 * @param User $obj
56
	 * @return mixed[]
57
	 */
58
	protected function mapRow($obj) {
59
		$row['id'] = $obj->getId();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$row was never initialized. Although not strictly required by PHP, it is generally a good practice to add $row = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
60
		$row['is_active'] = $obj->isActive();
61
		$row['access_level'] = $obj->getAccessLevel();
62
		$row['group_id'] = $obj->getGroupId();
63
		$row['name'] = $obj->getName();
64
		$row['email'] = $obj->getEmail();
65
		$row['password_hash'] = $obj->getPasswordHash();
66
		$row['recovery_hash'] = $obj->getRecoreryHash();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $row['recovery_hash'] is correct as $obj->getRecoreryHash() (which targets Win\Authentication\User::getRecoreryHash()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
		$row['image'] = $obj->getImage();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $row['image'] is correct as $obj->getImage() (which targets Win\Authentication\User::getImage()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
		$row['last_login'] = $obj->getLastLogin();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $row['last_login'] is correct as $obj->getLastLogin() (which targets Win\Authentication\User::getLastLogin()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
		return $row;
70
	}
71
72
	/**
73
	 * Insere o primeiro usuario
74
	 * @param string $email
75
	 * @param string $password
76
	 * @return string|null
77
	 */
78
	public function insertFirst($email, $password) {
79
		$totalUser = $this->numRows();
80
81
		if ($totalUser == 0) {
82
			$user = new User();
83
			$user->setName('Administrador');
84
			$user->setEmail($email);
85
			$user->setPassword($password);
86
			$user->setActive(true);
87
			$user->setAccessLevel(User::ACCESS_ADMIN);
88
			return $this->save($user);
89
		}
90
	}
91
92
	/**
93
	 * Atualiza data ultimo login
94
	 * @param User $user
95
	 * @return string|null
96
	 */
97
	public function updateLastLogin(User $user) {
98
		$now = date('Y-m-d H:i:s');
99
		$user->setLastLogin($now);
100
		return $this->save($user);
101
	}
102
103
	/**
104
	 * Gera/Atualiza um novo recoveryHash
105
	 * @param User $user
106
	 * @return string|null
107
	 */
108
	public function updateRecoveryHash(User $user) {
109
		$hash = md5($user->getEmail() . date('Y-m-d'));
110
		$user->setRecoreryHash($hash);
111
		return $this->save($user);
112
	}
113
114
	/**
115
	 * Atualiza a senha
116
	 * @param User $user
117
	 * @param string $newPassword1
118
	 * @param string $newPassword2
119
	 * @return string erro
120
	 */
121
	public function updatePassword(User $user, $newPassword1, $newPassword2) {
122
		$error = null;
123
		if ($newPassword1 != $newPassword2) {
124
			$error = 'A senha deve ser informada duas vezes iguais.';
125
		}
126
		if (!$error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
127
			$user->setPassword($newPassword1);
128
			$error = $this->save($user);
129
		}
130
		return $error;
131
	}
132
133
}
134