File tree Expand file tree Collapse file tree 5 files changed +36
-0
lines changed
Expand file tree Collapse file tree 5 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ public static function create(UserEntity $userEntity): int|string|false
1818 $ userBean ->last_name = $ userEntity ->getLastName ();
1919 $ userBean ->email = $ userEntity ->getEmail ();
2020 $ userBean ->phone = $ userEntity ->getPhone ();
21+ $ userBean ->password = $ userEntity ->getPassword ();
2122 $ userBean ->created_date = $ userEntity ->getCreationDate ();
2223
2324 try {
@@ -89,4 +90,10 @@ public static function remove(string $userUuid): bool
8990
9091 return false ;
9192 }
93+
94+ public static function doesEmailExist (string $ email ): bool
95+ {
96+ // If R::findOne doesn't find any rows, it returns NULL (meaning, the email address doesn't exist)
97+ return R::findOne (self ::TABLE_NAME , 'email = :email ' , ['email ' => $ email ]) !== null ;
98+ }
9299}
Original file line number Diff line number Diff line change 22namespace PH7 \ApiSimpleMenu \Route ;
33
44use PH7 \ApiSimpleMenu \Route \Exception \NotFoundException ;
5+ use PH7 \ApiSimpleMenu \Service \Exception \EmailExistsException ;
56use PH7 \ApiSimpleMenu \Service \User ;
67use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
78
@@ -59,6 +60,14 @@ public function getResponse(): string
5960 'code ' => $ e ->getCode ()
6061 ]
6162 ];
63+ } catch (EmailExistsException $ e ) {
64+ HttpResponse::setHeadersByCode (StatusCode::BAD_REQUEST );
65+
66+ $ response = [
67+ 'errors ' => [
68+ 'message ' => $ e ->getMessage ()
69+ ]
70+ ];
6271 }
6372
6473 return json_encode ($ response );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PH7 \ApiSimpleMenu \Service \Exception ;
4+
5+ use RuntimeException ;
6+
7+ class EmailExistsException extends RuntimeException
8+ {
9+ }
Original file line number Diff line number Diff line change 22namespace PH7 \ApiSimpleMenu \Service ;
33
44use PH7 \ApiSimpleMenu \Dal \UserDal ;
5+ use PH7 \ApiSimpleMenu \Service \Exception \EmailExistsException ;
56use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
67use PH7 \ApiSimpleMenu \Validation \UserValidation ;
78use PH7 \JustHttp \StatusCode ;
@@ -27,8 +28,15 @@ public function create(mixed $data): array|object
2728 ->setLastName ($ data ->last )
2829 ->setEmail ($ data ->email )
2930 ->setPhone ($ data ->phone )
31+ ->setPassword (password_hash ($ data ->password , PASSWORD_ARGON2I ))
3032 ->setCreationDate (date (self ::DATE_TIME_FORMAT ));
3133
34+ $ email = $ userEntity ->getEmail ();
35+ if (UserDal::doesEmailExist ($ email )) {
36+ throw new EmailExistsException (
37+ sprintf ('Email address %s already exists ' , $ email )
38+ );
39+ }
3240
3341 if (UserDal::create ($ userEntity ) === false ) {
3442 // Set an internal error 500 when we cannot add an entry to the database
Original file line number Diff line number Diff line change @@ -10,13 +10,16 @@ class UserValidation
1010 private const MINIMUM_NAME_LENGTH = 2 ;
1111 private const MAXIMUM_NAME_LENGTH = 40 ;
1212
13+ private const MINIMUM_PASSWORD_LENGTH = 5 ;
14+
1315 public function __construct (private readonly mixed $ data ) {}
1416
1517 public function isCreationSchemaValid (): bool
1618 {
1719 $ schemaValidation =
1820 v::attribute ('first ' , v::stringType ()->length (self ::MINIMUM_NAME_LENGTH , self ::MAXIMUM_NAME_LENGTH ))
1921 ->attribute ('last ' , v::stringType ()->length (self ::MINIMUM_NAME_LENGTH , self ::MAXIMUM_NAME_LENGTH ))
22+ ->attribute ('password ' , v::stringType ()->length (self ::MINIMUM_PASSWORD_LENGTH ))
2023 ->attribute ('email ' , v::email ())
2124 ->attribute ('phone ' , v::phone ());
2225
You can’t perform that action at this time.
0 commit comments