User.php 5.5 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
<?php


namespace Source\Entities;

use Carbon\Carbon;

Jacob Priddy's avatar
Jacob Priddy committed
8
9
class User
{
Jacob Priddy's avatar
Jacob Priddy committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    /**
     * @var int
     */
    protected int $id;

    /**
     * @var string
     */
    protected string $firstName;

    /**
     * @var string
     */
    protected string $lastName;

    /**
     * @var string
     */
    protected string $displayName;

    /**
     * @var string|null
     */
    protected ?string $emplid;

    /**
     * @var string
     */
    protected string $email;

    /**
Jacob Priddy's avatar
Jacob Priddy committed
41
     * @var \Source\Entities\Password|null
Jacob Priddy's avatar
Jacob Priddy committed
42
     */
Jacob Priddy's avatar
Jacob Priddy committed
43
    protected ?Password $password;
Jacob Priddy's avatar
Jacob Priddy committed
44
45

    /**
46
     * @var \Source\Entities\HashedSearchable|null
Jacob Priddy's avatar
Jacob Priddy committed
47
     */
48
    protected ?HashedSearchable $doorcode;
Jacob Priddy's avatar
Jacob Priddy committed
49
50

    /**
Jacob Priddy's avatar
Jacob Priddy committed
51
     * @var Carbon|null
Jacob Priddy's avatar
Jacob Priddy committed
52
     */
Jacob Priddy's avatar
Jacob Priddy committed
53
    protected ?Carbon $expiresAt;
Jacob Priddy's avatar
Jacob Priddy committed
54
55

    /**
Jacob Priddy's avatar
Jacob Priddy committed
56
     * @var Carbon|null
Jacob Priddy's avatar
Jacob Priddy committed
57
     */
Jacob Priddy's avatar
Jacob Priddy committed
58
    protected ?Carbon $createdAt;
Jacob Priddy's avatar
Jacob Priddy committed
59
60

    /**
Jacob Priddy's avatar
Jacob Priddy committed
61
     * @var Carbon|null
Jacob Priddy's avatar
Jacob Priddy committed
62
     */
Jacob Priddy's avatar
Jacob Priddy committed
63
    protected ?Carbon $updatedAt;
Jacob Priddy's avatar
Jacob Priddy committed
64
65

    /**
66
67
68
69
70
71
72
73
74
75
76
     * @param int                                    $id
     * @param string                                 $firstName
     * @param string                                 $lastName
     * @param string                                 $displayName
     * @param string                                 $email
     * @param string|null                            $emplid
     * @param \Source\Entities\Password|null         $password
     * @param \Source\Entities\HashedSearchable|null $doorcode
     * @param Carbon|null                            $expiresAt
     * @param Carbon|null                            $createdAt
     * @param Carbon|null                            $updatedAt
Jacob Priddy's avatar
Jacob Priddy committed
77
     */
Jacob Priddy's avatar
Jacob Priddy committed
78
79
80
81
82
83
    public function __construct(
        int $id,
        string $firstName,
        string $lastName,
        string $displayName,
        string $email,
Jacob Priddy's avatar
Jacob Priddy committed
84
85
        ?string $emplid = null,
        ?Password $password = null,
86
        ?HashedSearchable $doorcode = null,
Jacob Priddy's avatar
Jacob Priddy committed
87
88
89
90
        ?Carbon $expiresAt = null,
        ?Carbon $createdAt = null,
        ?Carbon $updatedAt = null
    ) {
Jacob Priddy's avatar
Jacob Priddy committed
91
92
93
94
95
        $this->id = $id;
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->displayName = $displayName;
        $this->emplid = $emplid;
Jacob Priddy's avatar
Jacob Priddy committed
96
        $this->email = strtolower($email);
Jacob Priddy's avatar
Jacob Priddy committed
97
98
99
100
101
102
103
104
105
106
        $this->password = $password;
        $this->doorcode = $doorcode;
        $this->expiresAt = $expiresAt;
        $this->createdAt = $createdAt;
        $this->updatedAt = $updatedAt;
    }

    /**
     * @return string
     */
Jacob Priddy's avatar
Jacob Priddy committed
107
108
    public function getFirstName(): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
109
110
111
112
113
114
        return $this->firstName;
    }

    /**
     * @return string
     */
Jacob Priddy's avatar
Jacob Priddy committed
115
116
    public function getLastName(): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
117
118
119
120
121
122
        return $this->lastName;
    }

    /**
     * @return string
     */
Jacob Priddy's avatar
Jacob Priddy committed
123
124
    public function getDisplayName(): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
125
126
127
128
129
130
        return $this->displayName;
    }

    /**
     * @return string|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
131
132
    public function getEmplid(): ?string
    {
Jacob Priddy's avatar
Jacob Priddy committed
133
134
135
136
        return $this->emplid;
    }

    /**
Jacob Priddy's avatar
Jacob Priddy committed
137
     * @return Carbon|null
Jacob Priddy's avatar
Jacob Priddy committed
138
     */
Jacob Priddy's avatar
Jacob Priddy committed
139
140
    public function getExpiresAt(): ?Carbon
    {
Jacob Priddy's avatar
Jacob Priddy committed
141
142
143
144
        return $this->expiresAt;
    }

    /**
Jacob Priddy's avatar
Jacob Priddy committed
145
     * @return Carbon|null
Jacob Priddy's avatar
Jacob Priddy committed
146
     */
Jacob Priddy's avatar
Jacob Priddy committed
147
148
    public function getCreatedAt(): ?Carbon
    {
Jacob Priddy's avatar
Jacob Priddy committed
149
150
151
152
        return $this->createdAt;
    }

    /**
Jacob Priddy's avatar
Jacob Priddy committed
153
     * @return Carbon|null
Jacob Priddy's avatar
Jacob Priddy committed
154
     */
Jacob Priddy's avatar
Jacob Priddy committed
155
156
    public function getUpdatedAt(): ?Carbon
    {
Jacob Priddy's avatar
Jacob Priddy committed
157
158
159
160
        return $this->updatedAt;
    }

    /**
Jacob Priddy's avatar
Jacob Priddy committed
161
     * @param string $id
Jacob Priddy's avatar
Jacob Priddy committed
162
163
     * @return bool
     */
164
    public function hasIdOf(?string $id): bool
Jacob Priddy's avatar
Jacob Priddy committed
165
    {
Jacob Priddy's avatar
Jacob Priddy committed
166
167
168
169
        if ($id === null) {
            return false;
        }

Jacob Priddy's avatar
Jacob Priddy committed
170
        return $this->getId() === (int)$id;
Jacob Priddy's avatar
Jacob Priddy committed
171
    }
Jacob Priddy's avatar
Jacob Priddy committed
172

Jacob Priddy's avatar
Jacob Priddy committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

Jacob Priddy's avatar
Jacob Priddy committed
189
190
191
192
193
    /**
     * @param string|null $email
     * @param string|null $password
     * @return bool
     */
Jacob Priddy's avatar
Jacob Priddy committed
194
195
    public function matchCredentials(?string $email, ?string $password): bool
    {
Jacob Priddy's avatar
Jacob Priddy committed
196
        if (!$this->password) {
Jacob Priddy's avatar
Jacob Priddy committed
197
198
            return false;
        }
Jacob Priddy's avatar
Jacob Priddy committed
199

Jacob Priddy's avatar
Jacob Priddy committed
200
        return $this->hasEmailOf($email) && $this->password->matches($password);
Jacob Priddy's avatar
Jacob Priddy committed
201
202
    }

Jacob Priddy's avatar
Jacob Priddy committed
203
204
205
206
207
208
209
210
211
    /**
     * @return string
     */
    public function getEmail(): string
    {
        return $this->email;
    }

    /**
Jacob Priddy's avatar
Jacob Priddy committed
212
     * @return \Source\Entities\Password|null
Jacob Priddy's avatar
Jacob Priddy committed
213
     */
Jacob Priddy's avatar
Jacob Priddy committed
214
    public function getPassword(): ?Password
Jacob Priddy's avatar
Jacob Priddy committed
215
216
217
218
    {
        return $this->password;
    }

Jacob Priddy's avatar
Jacob Priddy committed
219
    /**
220
     * @param \Source\Entities\HashedSearchable|null $doorcode
Jacob Priddy's avatar
Jacob Priddy committed
221
222
     * @return bool
     */
223
    public function hasDoorcodeOf(?HashedSearchable $doorcode): bool
Jacob Priddy's avatar
Jacob Priddy committed
224
    {
Jacob Priddy's avatar
Jacob Priddy committed
225
        if (!$doorcode || !$this->getDoorcode()) {
226
227
228
            return false;
        }

229
        return $this->getDoorcode()->getHash() === $doorcode->getHash();
Jacob Priddy's avatar
Jacob Priddy committed
230
    }
231

Jacob Priddy's avatar
Jacob Priddy committed
232
    /**
233
     * @return \Source\Entities\HashedSearchable|null
Jacob Priddy's avatar
Jacob Priddy committed
234
     */
235
    public function getDoorcode(): ?HashedSearchable
Jacob Priddy's avatar
Jacob Priddy committed
236
237
238
239
    {
        return $this->doorcode;
    }

Jacob Priddy's avatar
Jacob Priddy committed
240
241
242
243
    /**
     * @param string|null $email
     * @return bool
     */
Jacob Priddy's avatar
Jacob Priddy committed
244
245
    public function hasEmailOf(?string $email): bool
    {
Jacob Priddy's avatar
Jacob Priddy committed
246
247
248
249
        if (!$email) {
            return false;
        }

250
251
        return $this->getEmail() === strtolower($email);
    }
Jacob Priddy's avatar
Jacob Priddy committed
252

Jacob Priddy's avatar
Jacob Priddy committed
253
254
255
256
    /**
     * @param string|null $name
     * @return bool
     */
Jacob Priddy's avatar
Jacob Priddy committed
257
258
259
260
261
262
263
264
265
    public function hasFirstNameOf(?string $name): bool
    {
        if (!$name) {
            return false;
        }

        return $this->getFirstName() === $name;
    }

Jacob Priddy's avatar
Jacob Priddy committed
266
267
268
269
    /**
     * @param \Source\Entities\User|null $user
     * @return bool
     */
Jacob Priddy's avatar
Jacob Priddy committed
270
271
272
    public function is(?User $user): bool
    {
        if (!$user) {
273
            return false;
Jacob Priddy's avatar
Jacob Priddy committed
274
275
276
277
        }

        return $this->hasEmailOf($user);
    }
Jacob Priddy's avatar
Jacob Priddy committed
278
}