User.php 4.6 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 string|null
Jacob Priddy's avatar
Jacob Priddy committed
42
     */
Jacob Priddy's avatar
Jacob Priddy committed
43
    protected ?string $password;
Jacob Priddy's avatar
Jacob Priddy committed
44
45

    /**
46
     * @var string|null
Jacob Priddy's avatar
Jacob Priddy committed
47
     */
48
    protected ?string $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

    /**
     * @param int         $id
     * @param string      $firstName
     * @param string      $lastName
     * @param string      $displayName
     * @param string|null $emplid
     * @param string      $email
72
73
     * @param string|null $password
     * @param string|null $doorcode
Jacob Priddy's avatar
Jacob Priddy committed
74
75
76
     * @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
84
85
86
87
88
89
90
    public function __construct(
        int $id,
        string $firstName,
        string $lastName,
        string $displayName,
        ?string $emplid,
        string $email,
        ?string $password,
        ?string $doorcode,
        ?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
    public function matchCredentials(?string $email, ?string $password): bool
    {
Jacob Priddy's avatar
Jacob Priddy committed
191
192
193
        if (!$password || !$email) {
            return false;
        }
Jacob Priddy's avatar
Jacob Priddy committed
194

Jacob Priddy's avatar
Jacob Priddy committed
195
196
197
        return $this->getEmail() === $email && $this->getPassword() === $password;
    }

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

    /**
     * @return string|null
     */
    public function getPassword(): ?string
    {
        return $this->password;
    }

Jacob Priddy's avatar
Jacob Priddy committed
214
215
216
217
    /**
     * @param string $doorcode
     * @return bool
     */
Jacob Priddy's avatar
Jacob Priddy committed
218
219
    public function hasDoorcodeOf(?string $doorcode): bool
    {
220
221
222
223
        if (!$doorcode) {
            return false;
        }

Jacob Priddy's avatar
Jacob Priddy committed
224
225
        return $this->getDoorcode() === $doorcode;
    }
226

Jacob Priddy's avatar
Jacob Priddy committed
227
228
229
230
231
232
233
234
    /**
     * @return string|null
     */
    public function getDoorcode(): ?string
    {
        return $this->doorcode;
    }

Jacob Priddy's avatar
Jacob Priddy committed
235
236
    public function hasEmailOf(?string $email): bool
    {
237
238
        return $this->getEmail() === strtolower($email);
    }
Jacob Priddy's avatar
Jacob Priddy committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    public function hasFirstNameOf(?string $name): bool
    {
        if (!$name) {
            return false;
        }

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

    public function is(?User $user): bool
    {
        if (!$user) {
            return null;
        }

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