Completed
Push — master ( aeb1e8...e32bea )
by Wanderson
02:48
created

UserDAO::mapRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 12
nc 1
nop 1
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
	 * @param array $row
21
	 * @return User
22
	 */
23
	protected function mapObject($row) {
24
		$obj = new User();
25
		$obj->setId($row['id']);
26
		$obj->setActive($row['is_active']);
27
		$obj->setAccessLevel($row['access_level']);
28
		$obj->setGroupId($row['group_id']);
29
		$obj->setName($row['name']);
30
		$obj->setEmail($row['email']);
31
		$obj->setPasswordHash($row['password_hash']);
32
		$obj->setRecoreryHash($row['recovery_hash']);
33
		$obj->setImage($row['image']);
34
		$obj->setLastLogin($row['last_login']);
35
		return $obj;
36
	}
37
38
	/**
39
	 * @param User $obj
40
	 * @return mixed[]
41
	 */
42
	protected function mapRow($obj) {
43
		$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...
44
		$row['is_active'] = $obj->isActive();
45
		$row['access_level'] = $obj->getAccessLevel();
46
		$row['group_id'] = $obj->getGroupId();
47
		$row['name'] = $obj->getName();
48
		$row['email'] = $obj->getEmail();
49
		$row['password_hash'] = $obj->getPasswordHash();
50
		$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...
51
		$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...
52
		$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...
53
		return $row;
54
	}
55
56
	/**
57
	 * Insere o primeiro usuario
58
	 * @param string $email
59
	 * @param string $password
60
	 */
61
	public function insertFirst($email, $password) {
62
		$totalUser = $this->numRows();
63
64
		if ($totalUser == 0) {
65
			$user = new User();
66
			$user->setName('Administrador');
67
			$user->setEmail($email);
68
			$user->setPassword($password);
69
			$user->setActive(true);
70
			$user->setAccessLevel(User::ACCESS_ADMIN);
71
			$this->save($user);
72
		}
73
	}
74
75
}
76