-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserModel.php
972 lines (719 loc) · 23.9 KB
/
UserModel.php
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
<?php
/**
* @license MIT
* super-week
*
* Copyright (c) 2023 Abraham Ukachi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @project super-week
* @name User Model
* @test test/user_model.php
* @file UserModel.php
* @author: Abraham Ukachi <abraham.ukachi@laplateforme.io>
* @version: 0.0.1
*
* Example usage:
*
* 1+|> // Get all users using the `findAll()` method
* -|>
* -|> use App\Model\UserModel;
* -|>
* -|> $userModel = new UserModel();
* -|> $users = $userModel->findAll();
* -|>
*/
// declare a namespace for this `UserModel` class
namespace App\Model;
// use these classes
use App\Model\Helper\Database;
use pdo;
// use datetime;
// use PDO;
// use PDOException;
/**
* A class that represents our `users` table in the database
* NOTE: This class currently extends a Database class
*
* TODO: Create an abstract Model class that extends the abstract Database class,
* which in turn should implement a DatabaseInterface
*/
class UserModel extends Database {
// Declare some constants
// Declare some public properties
// Declare some private properties
private ?int $id = null;
private ?string $email = null;
private ?string $firstName = null;
private ?string $lastName = null;
/**
* User constructor.
* NOTE: This constructor is called when a new instance of the User class is created
*/
public function __construct() {
// call the parent/Database constructor
parent::__construct(Database::TYPE_PDO); // <- via PDO
// connect to the database
$this->dbConnection();
// If the user is *appears to be* connected...
if ($this->isConnected()) {
// ...get the user's token from session
$token = $this->getSessionToken();
// get the user's details from the database using this `$token`
$user = $this->findByToken($token);
// If the user exists...
if ($user) {
// ...populate the `UserModel`'s properties
$this->populateProps($user);
} else {
// ...else, disconnect the user 'cause duh! Somethin' doesn't seam right #LOL
$this->disconnect();
}
}
}
// PUBLIC SETTERS
/**
* Sets the first name of the user
* NOTE: This method sets the `firstName` property only after
* updating the `first_name` column in the database w/ the given $fistName
*
* @param string $firstName - the first name of the user to set
* @return void
*/
public function setFirstName(string $firstName): void {
// create an sql query named `update_firstname_query`,
$update_firstname_query = sprintf(<<<SQL
UPDATE %s
SET %s = :firstName
WHERE %s = :id
SQL,
// update (table)
$this::TABLE_USERS,
// fields
$this::FIELD_FIRST_NAME, // <- set
$this::FIELD_ID // <- where
);
try { // <- try to prepare and execute the `update_firstname_query`
// prepare the `update_firstname_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($update_firstname_query);
// execute our PDO statement
$pdo_stmt->execute([
':firstName' => $firstName,
':id' => $this->id
]);
// Now, we can set the `firstName` property
$this->firstName = $firstName;
} catch (PDOException $e) { // <- catch any PDO exceptions
// log the error message
error_log($e->getMessage());
}
}
/**
* Sets the last name of the user
* NOTE: This method sets the `lastName` property only after
* updating the `last_name` column in the database w/ the given $lastName
*
* @param string $lastName - the last name of the user to set
* @return void
*/
public function setLastName(string $lastName): void {
// create an sql query named `update_lastname_query`,
$update_lastname_query = sprintf(<<<SQL
UPDATE %s
SET %s = :lastName
WHERE %s = :id
SQL,
// update (table)
$this::TABLE_USERS,
// fields
$this::FIELD_LAST_NAME, // <- set
$this::FIELD_ID // <- where
);
try { // <- try to prepare and execute the `update_lastname_query`
// prepare the `update_lastname_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($update_lastname_query);
// execute our PDO statement
$pdo_stmt->execute([
':lastName' => $lastName,
':id' => $this->id
]);
// Now, we can set the `lastName` property
$this->lastName = $lastName;
} catch (PDOException $e) { // <- catch any PDO exceptions
// log the error message
error_log($e->getMessage());
}
}
// PUBLIC GETTERS
/**
* Returns the id of the user
*/
public function getId(): int {
return $this->id ?? -1;
}
/**
* Returns the first name of the user
*
* @return string
*/
public function getFirstName(): string {
return $this->firstName ?? '';
}
/**
* Returns the last name of the user
*
* @return string
*/
public function getLastName(): string {
return $this->lastName ?? '';
}
/**
* Returns the full name of the user
*
* @return string
*/
public function getFullName(): string {
// get the first and last names as `$firstName` and `$lastName` respectively
$firstName = $this->getFirstName();
$lastName = $this->getLastName();
// return the full name of the user
return $firstName . ' ' . $lastName;
}
/**
* Returns a total count of all the users in the database
*
* @return int
*/
public function countAll(): int {
// find all the users in the database
$allUsers = $this->findAll();
// return the toal count of all the users in the database
return count($allUsers);
}
// PUBLIC METHODS
/**
* Method used to find all the users in the database
*
* @return array - an associative array of all the users in the database
*/
public function findAll(): array {
// create an sql query named `find_users_query`,
// using a heredoc syntax - https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
$find_users_query = sprintf(<<<SQL
SELECT *
FROM %s
SQL,
// from (table)
$this::TABLE_USERS
);
// prepare the `find_users_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($find_users_query);
// execute our PDO statement
$pdo_stmt->execute();
// fetch all the results from `pdo_stmt` as an associative array
$results = $pdo_stmt->fetchAll(PDO::FETCH_ASSOC);
// return the results
return $results;
}
/**
* Creates a new user with the given parameters
* NOTE: This method inserts a new user into the database
*
* @param string $email - the email of the user
* @param string $firstName - the first name of the user
* @param string $lastName - the last name of the user
* @param string $password - the password of the user
*
* @return array|false - Returns an array including the user's id of the newly created user, or false if the user was not created
*/
public function create(string $email, string $firstName, string $lastName, string $password): array|false {
// Initialize the `result` boolean variable
$result = false;
// create an sql query named `create_user_query`,
$create_user_query = sprintf(<<<SQL
INSERT INTO %s
(%s, %s, %s, %s)
VALUES
(:email, :firstName, :lastName, :password)
SQL,
// table
self::TABLE_USERS,
// fields
self::FIELD_EMAIL,
self::FIELD_FIRST_NAME,
self::FIELD_LAST_NAME,
self::FIELD_PASSWORD
);
try { // <- try to create a new user
// prepare the `create_user_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($create_user_query);
// execute our PDO statement with the given parameters,
// and store the result in a variable named `$result`
$pdo_stmt->execute([
':email' => $email,
':firstName' => $firstName,
':lastName' => $lastName,
':password' => $password
]);
// get the last inserted id as `userId`
$userId = $this->pdo->lastInsertId();
// update the `result` w/ a short-syntax associative array of the new user
$result = [
'id' => $userId,
'email' => $email,
'first_name' => $firstName,
'last_name' => $lastName,
'password' => $password
];
} catch (PDOException $e) { // <- catch any PDOException errors
// log the error
error_log($e->getMessage());
}
// return the `result`
return $result;
}
/**
* Method used to connect the user with the given `$email` and `$password`
*
* @param string $email - the email of the user
* @param string $password - the password of the user
*
* @return array|false - Returns an array including the user's id of the connected user, or FALSE if the user was not created
*/
public function connect(string $email, string $password): array|false {
// TODO: Check if the user is already connected
// Initialize the `result` boolean variable
$result = false;
// Find the user with the given email as `user`
$user = $this->findByEmail($email, true);
// If the user exists and if the password is correct...
if ($user && password_verify($password, $user['password'])) {
// ...get the user's id as `userId`
$userId = $user['id'];
// generate a new token for the user as `token`
$token = $this->generateToken(); // returns eg.: "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6"
// store the `token` in the session and database
$this->storeUserToken($userId, $token);
// add the `token` to the `user` array
$user[self::FIELD_TOKEN] = $token;
// populate the `userModel` properties with the `populateProps()` method
$this->populateProps($user);
// update the `result` variable by setting it to the `$user` array
$result = $user;
}
// return the `result`
return $result;
}
/**
* Disconnects the user
* NOTE: This method removes the user's token from the database and session
*
* @return bool - Returns TRUE if the user was disconnected, FALSE otherwise
*/
public function disconnect(): bool {
// Initialize the `result` boolean variable
$result = false;
// If the user is conected...
if ($this->isConnected()) {
// ...get the user's id as `userId`
$userId = $this->getId();
// remove the user's token from the database
$this->removeUserToken($userId);
// remove the user's token from the session
$this->removeSessionToken();
// update the `result` variable by setting it to TRUE
$result = true;
}else {
// HACK: Even if the user is not connected,
// we still wanna remove the session token JUST TO BE SAFE ;)
$this->removeSessionToken();
}
// return `result`
return $result;
}
/**
* Checks if the current user is connected
*
* @return bool - Returns TRUE if the user is connected, FALSE otherwise
*/
public function isConnected(): bool {
// Initialize the `result` boolean variable
$result = false;
// IDEA: Verify that the session token exists in the database
// get the session token as `sessionToken`
$sessionToken = $this->getSessionToken();
// DEBUG [4dbsmaster]: tell me about it ;)
//printf('<pre>sessionToken: %s</pre>', $sessionToken);
// if there's a session token and if this `sessionToken` is verified...
if (!empty($sessionToken) && $this->verifyToken($sessionToken)) {
// ...update the `result` variable by setting it to TRUE
$result = true;
}
// return `result`
return $result;
}
/**
* Method used to find a user with the given `$userId`
*
* @param string $userId - the id to find the user with
*
* @return array|false - Returns an array containing the user's details, or FALSE if the user was not found
*
*/
public function findById(string $userId): array|false {
// Initialize the `result` boolean variable
$result = false;
// create a `find_user_by_id_query` sql query
$find_user_by_id_query = sprintf(<<<SQL
SELECT %s, %s, %s, %s, %s
FROM %s
WHERE %s = :userId
SQL,
// select (fields)
self::FIELD_ID,
self::FIELD_EMAIL,
self::FIELD_FIRST_NAME,
self::FIELD_LAST_NAME,
self::FIELD_TOKEN,
// from (table)
self::TABLE_USERS,
// where (field)
self::FIELD_ID
);
try { // <- try to prepare and execute our query
// prepare the `find_user_by_id_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($find_user_by_id_query);
// execute our PDO statement with the given parameters
$pdo_stmt->execute([
':userId' => $userId
]);
// fetch the results from `pdo_stmt` as an associative array
$result = $pdo_stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { // <- catch any PDOException errors
// log the error
error_log($e->getMessage());
}
// return the `result`
return $result;
}
/**
* Method used to find a user with the given `$token`
*
* @param string $token - the token to find the user with
*
* @return array|false - Returns an array containing the user's details, or FALSE if the user was not found
*
*/
public function findByToken(string $token): array|false {
// Initialize the `result` boolean variable
$result = false;
// create a `find_user_by_token_query` sql query
$find_user_by_token_query = sprintf(<<<SQL
SELECT %s, %s, %s, %s, %s
FROM %s
WHERE %s = :token
SQL,
// select (fields)
self::FIELD_ID,
self::FIELD_EMAIL,
self::FIELD_FIRST_NAME,
self::FIELD_LAST_NAME,
self::FIELD_TOKEN,
// from (table)
self::TABLE_USERS,
// where (field)
self::FIELD_TOKEN
);
try { // <- try to prepare and execute our query
// prepare the `find_user_by_token_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($find_user_by_token_query);
// execute our PDO statement with the given parameters
$pdo_stmt->execute([
':token' => $token
]);
// fetch the results from `pdo_stmt` as an associative array
$result = $pdo_stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { // <- catch any PDOException errors
// log the error
error_log($e->getMessage());
}
// return the `result`
return $result;
}
/**
* Method used to find a user by their email
*
* @param string $email - the email of the user
* @param bool $includesPassword - whether or not the password should be included in the result
*
* @return array|false - Returns an array containing the user's details, or FALSE if the user does not exist
*/
public function findByEmail(string $email, bool $includesPassword = false): array|false {
// Initialize the `result` boolean variable
$result = false;
// create a `find_user_by_email_query` sql query
$find_user_by_email_query = sprintf(<<<SQL
SELECT %s, %s, %s, %s, %s %s
FROM %s
WHERE %s = :email
SQL,
// select (fields)
self::FIELD_ID,
self::FIELD_EMAIL,
self::FIELD_FIRST_NAME,
self::FIELD_LAST_NAME,
self::FIELD_TOKEN,
$includesPassword ? ', ' . self::FIELD_PASSWORD : '',
// from (table)
self::TABLE_USERS,
// where (field)
self::FIELD_EMAIL
);
try { // <- try to prepare and execute our query
// prepare the `find_user_by_email_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($find_user_by_email_query);
// execute our PDO statement with the given parameters
$pdo_stmt->execute([
':email' => $email
]);
// fetch the results from `pdo_stmt` as an associative array
$result = $pdo_stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { // <- catch any PDOException errors
// log the error
error_log($e->getMessage());
}
// return the `result`
return $result;
}
// PRIVATE SETTERS
/**
* Method used to populate the `userModel` properties with the given `$user`
*
* @param array $user - the user to populate the properties with
*
* @return void
* @private
*/
private function populateProps(array $user): void {
// set the `id` property to the `id` of the given `$user`
$this->id = $user[self::FIELD_ID];
// set the `email` property to the `email` of the given `$user`
$this->email = $user[self::FIELD_EMAIL];
// set the `firstName` property to the `first_name` of the given `$user`
$this->firstName = $user[self::FIELD_FIRST_NAME];
// set the `lastName` property to the `last_name` of the given `$user`
$this->lastName = $user[self::FIELD_LAST_NAME];
}
// PRIVATE GETTERS
/**
* Method used to get the user's token from the session
*
* @return string - the token from the session
* @private
*/
private function getSessionToken(): string {
// Safely create the `user` session
$this->createUserSession();
// return the `token` session variable
return $_SESSION['user']['token'] ?? '';
}
/**
* Method used to remove the user's token from the session
* NOTE: This method is private, and is only used internally ;)
*
* @return void
* @private
*/
private function removeSessionToken(): void {
// Safely create the `user` session
$this->createUserSession();
// unset the `token` session variable
unset($_SESSION['user']['token']);
}
/**
* Method used to remove or delete the user's token from the database
*
* @param int $userId - the id of the user to remove the token from
* @return bool - whether or not the token was successfully removed
* @private
*/
private function removeUserToken($userId): bool {
// Initialize the `result` boolean variable
$result = false;
// create a `remove_user_token_query` sql query
$remove_user_token_query = sprintf(<<<SQL
UPDATE %s
SET %s = NULL
WHERE %s = :id
SQL,
// update (table)
self::TABLE_USERS,
// set (field)
self::FIELD_TOKEN,
// where (field)
self::FIELD_ID
);
try { // <- try to prepare and execute our query
// prepare the `remove_user_token_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($remove_user_token_query);
// execute our PDO statement with the given parameters
$pdo_stmt->execute([
':id' => $userId
]);
// set the `result` to true
$result = true;
} catch (PDOException $e) { // <- catch any PDOException errors
// log the error
error_log($e->getMessage());
}
// return the `result`
return $result;
}
// PRIVATE METHODS
/**
* Creates a `user` session variable, if it doesn't exist
* NOTE: This method is private, and is only used internally ;)
*
* @return void
*/
private function createUserSession(): void {
// If there's no `user` session variable
if (!isset($_SESSION['user'])) {
// set the `user` session variable to an empty array
$_SESSION['user'] = [];
}
}
/**
* Updates the `user` session variable with the given `$key` and `$value`
* NOTE: This method is private, and is only used internally ;)
*
* @param string $key - the key to update
* @param mixed $value - the value to update the key with
*
* @return void
*/
private function updateUserSession(string $key, mixed $value): void {
// Safely create the `user` session
$this->createUserSession();
// Update the `user` session variable with the given `$key` and `$value`
$_SESSION['user'][$key] = $value;
}
/**
* Generates a random token
*
* @return string - the generated token
*
* @private
*/
private function generateToken(): string {
// Initialize the `token` string variable
$token = '';
// Generate a random token
$token = bin2hex(random_bytes(32)); // <- 32 bytes = 256 bits = 64 hex characters | eg.: 1a79a4d60de6718e8e5b326e338ae533
// return the `token`
return $token;
}
/**
* Method used to store the given `$token` in the `user` database *AND* session variable
*
* @param int $userId - the id of the user to store the token for
* @param string $token - the token to store
*
* @return void
* @private
*/
private function storeUserToken(int $userId, string $token): void {
// IDEA: Store the user's token in the database first, then update its value in the session variable
// create a `store_user_token_query` sql query
$store_user_token_query = sprintf(<<<SQL
UPDATE %s
SET %s = :token
WHERE %s = :id
SQL,
// table
self::TABLE_USERS,
// fields
self::FIELD_TOKEN,
self::FIELD_ID
);
// try to prepare and execute our query
try {
// prepare the `store_user_token_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($store_user_token_query);
// execute our PDO statement with the given parameters
$pdo_stmt->execute([
':token' => $token,
':id' => $userId
]);
// Now, we need to store the token in the session variable ;)
// Update the `token` key in the `user` session variable with the given `$token`
$this->updateUserSession('token', $token);
} catch (PDOException $e) {
// log the error
error_log($e->getMessage());
}
}
/**
* Checks if the given `$token` is valid or exists in the database
* NOTE: This method is private, and is only used internally ;)
*
* @param string $token - the token to check
*
* @return bool - Returns TRUE if the token is valid, FALSE otherwise
*
* @private
*/
private function verifyToken(string $token): bool {
// Initialize the `result` boolean variable
$result = false;
// create a `verify_token_query` sql query
$verify_token_query = sprintf(<<<SQL
SELECT %s
FROM %s
WHERE %s = :token
SQL,
// select (field)
self::FIELD_ID,
// from (table)
self::TABLE_USERS,
// where (field)
self::FIELD_TOKEN
);
// try to prepare and execute our query
try {
// prepare the `verify_token_query` as a PDO statement
$pdo_stmt = $this->pdo->prepare($verify_token_query);
// execute our PDO statement with the given parameters
$pdo_stmt->execute([
':token' => $token
]);
// get the result from `pdo_stmt` as an associative array,
// and assign it to a `user` variable
$user = $pdo_stmt->fetch(PDO::FETCH_ASSOC);
// update the `result` variable accordingly
$result = empty($user) ? false : true; // <- if `$user` is empty, set `result` to false, otherwise set it to true
} catch (PDOException $e) {
// log the error
error_log($e->getMessage());
}
// return the `result` variable
return $result;
}
}