11<?php
2- namespace PH7 \ApiSimpleMenu ;
2+ namespace PH7 \ApiSimpleMenu \ Service ;
33
4+ use PH7 \ApiSimpleMenu \Dal \UserDal ;
45use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
56use PH7 \ApiSimpleMenu \Validation \UserValidation ;
7+ use PH7 \JustHttp \StatusCode ;
8+ use PH7 \PhpHttpResponseHeader \Http ;
69use Ramsey \Uuid \Uuid ;
10+ use RedBeanPHP \RedException \SQL ;
711use Respect \Validation \Validator as v ;
12+ use PH7 \ApiSimpleMenu \Entity \User as UserEntity ;
813
914class User
1015{
11- public readonly ? string $ userId ;
16+ public const DATE_TIME_FORMAT = ' Y-m-d H:i:s ' ;
1217
1318 public function __construct (
1419 public readonly string $ name ,
@@ -22,7 +27,26 @@ public function create(mixed $data): object
2227 // validate data
2328 $ userValidation = new UserValidation ($ data );
2429 if ($ userValidation ->isCreationSchemaValid ()) {
25- $ data ->userId = Uuid::uuid4 (); // assigning a UUID to the user
30+ $ userUuid = Uuid::uuid4 (); // assigning a UUID to the user
31+
32+ $ userEntity = new UserEntity ();
33+ $ userEntity
34+ ->setUserUuid ($ userUuid )
35+ ->setFirstName ($ data ->first )
36+ ->setLastName ($ data ->last )
37+ ->setEmail ($ data ->email )
38+ ->setPhone ($ data ->phone )
39+ ->setCreationDate (date (self ::DATE_TIME_FORMAT ));
40+
41+ try {
42+ UserDal::create ($ userEntity );
43+ } catch (SQL $ exception ) {
44+ // Set an internal error when we cannot add an entry to the database
45+ Http::setHeadersByCode (StatusCode::INTERNAL_SERVER_ERROR );
46+
47+ // Set to empty result, because an issue happened. The client has to handle this properly
48+ $ data = [];
49+ }
2650
2751 return $ data ; // return statement exists the function and doesn't go beyond this scope
2852 }
@@ -38,7 +62,7 @@ public function retrieveAll(): array
3862 public function retrieve (string $ userId ): self
3963 {
4064 if (v::uuid ()->validate ($ userId )) {
41- $ this -> userId = $ userId ;
65+ // TODO To be implemented
4266
4367 return $ this ;
4468 }
@@ -60,7 +84,7 @@ public function update(mixed $postBody): object
6084 public function remove (string $ userId ): bool
6185 {
6286 if (v::uuid ()->validate ($ userId )) {
63- $ this -> userId = $ userId ;
87+ // TODO To be implemented
6488 } else {
6589 throw new InvalidValidationException ("Invalid user UUID " );
6690 }
0 commit comments