Completed
Push — master ( 5139b6...b16139 )
by Sebastian
02:13
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
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, 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
 *     name,
27
 *     email,
28
 *     description,
29
 *     password,
30
 *     active,
31
 *     created,
32
 *     last_update AS lastUpdate
33
 *     FROM user WHERE user_id = :id');
34
 *
35
 * $pdos->bindParam(':id', $userId, \PDO::PARAM_INT);
36
 * $pdos->execute();
37
 *
38
 * //pdo return an User class instance
39
 * $user = $pdos->fetchObject('\Linna\Auth\User', [$password]);
40
 *
41
 * //var_dump result
42
 * //root
43
 * var_dump($user->name);
44
 * </code></pre>
45
 */
46
class User extends DomainObjectAbstract
47
{
48
    /**
49
     * @var string User name.
50
     */
51
    public $name;
52
53
    /**
54
     * @var string User description.
55
     */
56
    public $description;
57
58
    /**
59
     * @var string User e-mail.
60
     */
61
    public $email;
62
63
    /**
64
     * @var string User hashed password.
65
     */
66
    public $password;
67
68
    /**
69
     * @var int It say if user is active or not.
70
     */
71
    public $active = 0;
72
73
    /**
74
     * @var string User creation date.
75
     */
76
    public $created;
77
78
    /**
79
     * @var string Last update.
80
     */
81
    public $lastUpdate;
82
83
    /**
84
     * @var object Password class for manage password.
85
     */
86
    private $passwordUtility;
87
88
    /**
89
     * Class Constructor.
90
     *
91
     * <pre><code class="php">$password = new Password();
92
     *
93
     * $user = new User($password);
94
     * </code></pre>
95
     *
96
     * @param Password $password
97
     */
98 15
    public function __construct(Password $password)
99
    {
100 15
        $this->passwordUtility = $password;
101
102
        //set required type
103
        //do type conversion because PDO doesn't return any original type from db :(.
104 15
        settype($this->objectId, 'integer');
105 15
        settype($this->active, 'integer');
106 15
    }
107
108
    /**
109
     * Set new user password without do any check.
110
     *
111
     * <pre><code class="php">$password = new Password();
112
     *
113
     * $user = new User($password);
114
     *
115
     * $user->setPassword('newPassword');
116
     * </code></pre>
117
     *
118
     * @param string $newPassword
119
     */
120 2
    public function setPassword(string $newPassword)
121
    {
122
        //hash provided password
123 2
        $this->password = $this->passwordUtility->hash($newPassword);
124 2
    }
125
126
    /**
127
     * Change user password only after check old password.
128
     *
129
     * <pre><code class="php">$password = new Password();
130
     *
131
     * $user = new User($password);
132
     *
133
     * $user->chagePassword('newPassword', 'oldPassword');
134
     * </code></pre>
135
     *
136
     * @param string $newPassword
137
     * @param string $oldPassword
138
     *
139
     * @return bool
140
     */
141 1
    public function chagePassword(string $newPassword, string $oldPassword) : bool
142
    {
143
        //verfy password match
144 1
        if ($this->passwordUtility->verify($oldPassword, $this->password)) {
145
146
            //if match set new password
147 1
            $this->password = $this->passwordUtility->hash($newPassword);
148
149 1
            return true;
150
        }
151
152 1
        return false;
153
    }
154
}
155