Passed
Push — master ( 2a299a...ed7688 )
by Sebastian
03:38
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authentication;
13
14
use Linna\DataMapper\DomainObjectAbstract;
15
16
/**
17
 * Provide a basic user for authentication system.
18
 * This class is thinked to be used with PHP PDO,
19
 * but should be instantiated like a normal class.
20
 *
21
 * <pre><code class="php">$password = new Password();
22
 *
23
 * $userId = 1;
24
 *
25
 * $pdos = $pdo->prepare('SELECT user_id AS objectId,
26
 *     uuid,
27
 *     name,
28
 *     email,
29
 *     description,
30
 *     password,
31
 *     active,
32
 *     created,
33
 *     last_update AS lastUpdate
34
 *     FROM user WHERE user_id = :id');
35
 *
36
 * $pdos->bindParam(':id', $userId, \PDO::PARAM_INT);
37
 * $pdos->execute();
38
 *
39
 * //pdo return an User class instance
40
 * $user = $pdos->fetchObject('\Linna\Auth\User', [$password]);
41
 *
42
 * //var_dump result
43
 * //root
44
 * var_dump($user->name);
45
 * </code></pre>
46
 */
47
class User extends DomainObjectAbstract
48
{
49
    /**
50
     * @var string Universal unique identifier.
51
     */
52
    public $uuid = '';
53
54
    /**
55
     * @var string User name.
56
     */
57
    public $name = '';
58
59
    /**
60
     * @var string User description.
61
     */
62
    public $description = '';
63
64
    /**
65
     * @var string User e-mail.
66
     */
67
    public $email = '';
68
69
    /**
70
     * @var string User hashed password.
71
     */
72
    public $password = '';
73
74
    /**
75
     * @var int It say if user is active or not.
76
     */
77
    public $active = 0;
78
79
    /**
80
     * @var string User creation date.
81
     */
82
    public $created = '';
83
84
    /**
85
     * @var string Last update.
86
     */
87
    public $lastUpdate = '';
88
89
    /**
90
     * @var Password Password class for manage password.
91
     */
92
    private $passwordUtility;
93
94
    /**
95
     * Class Constructor.
96
     *
97
     * <pre><code class="php">$password = new Password();
98
     *
99
     * $user = new User($password);
100
     * </code></pre>
101
     *
102
     * @param Password $password Password class instance.
103
     */
104 227
    public function __construct(Password $password)
105
    {
106 227
        $this->passwordUtility = $password;
107
108
        //set required type
109
        //do type conversion because PDO doesn't return any original type from db :(.
110 227
        \settype($this->rId, 'integer');
111 227
        \settype($this->objectId, 'integer');
112 227
        \settype($this->active, 'integer');
113 227
    }
114
115
    /**
116
     * Set new user password without do any check.
117
     *
118
     * <pre><code class="php">$user->setPassword('newPassword');</code></pre>
119
     *
120
     * @param string $newPassword User new password.
121
     *
122
     * @return void
123
     */
124 5
    public function setPassword(string $newPassword): void
125
    {
126
        //hash provided password
127 5
        $this->password = $this->passwordUtility->hash($newPassword);
128 5
    }
129
130
    /**
131
     * Change user password only after check old password.
132
     *
133
     * <pre><code class="php">$user->changePassword('newPassword', 'oldPassword');</code></pre>
134
     *
135
     * @param string $newPassword   User new password.
136
     * @param string $oldPassword   User old password.
137
     *
138
     * @return bool
139
     */
140 1
    public function changePassword(string $newPassword, string $oldPassword): bool
141
    {
142
        //verfy password match
143 1
        if ($this->passwordUtility->verify($oldPassword, $this->password)) {
144
145
            //if match set new password
146 1
            $this->password = $this->passwordUtility->hash($newPassword);
147
148 1
            return true;
149
        }
150
151 1
        return false;
152
    }
153
}
154