File tree Expand file tree Collapse file tree 8 files changed +43
-26
lines changed Expand file tree Collapse file tree 8 files changed +43
-26
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ final class TokenKeyDal
88{
99 public const TABLE_NAME = 'secretkeys ' ;
1010
11- public static function saveSecretKey (string $ jwtKey )
11+ public static function saveSecretKey (string $ jwtKey ): void
1212 {
1313 $ tokenBean = R::dispense (self ::TABLE_NAME );
1414 $ tokenBean ->secretKey = $ jwtKey ;
Original file line number Diff line number Diff line change @@ -89,6 +89,9 @@ public static function getByEmail(string $email): UserEntity
8989 return (new UserEntity ())->unserialize ($ userBean ?->export());
9090 }
9191
92+ /**
93+ * @throws \RedBeanPHP\RedException\SQL
94+ */
9295 public static function setToken (string $ jwtToken , string $ userUuid ): void
9396 {
9497 $ bindings = ['userUuid ' => $ userUuid ];
Original file line number Diff line number Diff line change 22namespace PH7 \ApiSimpleMenu \Route ;
33
44use PH7 \ApiSimpleMenu \Service \FoodItem ;
5- use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
6-
7- use PH7 \JustHttp \StatusCode ;
8- use PH7 \PhpHttpResponseHeader \Http ;
95
106enum FoodItemAction: string
117{
@@ -21,22 +17,10 @@ public function getResponse(): string
2117 $ itemId = $ _REQUEST ['id ' ] ?? '' ; // using the null coalescing operator
2218
2319 $ item = new FoodItem ();
24- try {
25- $ response = match ($ this ) {
26- self ::RETRIEVE_ALL => $ item ->retrieveAll (),
27- self ::RETRIEVE => $ item ->retrieve ($ itemId ),
28- };
29- } catch (InvalidValidationException $ e ) {
30- // Send 400 http status code
31- Http::setHeadersByCode (StatusCode::BAD_REQUEST );
32-
33- $ response = [
34- 'errors ' => [
35- 'message ' => $ e ->getMessage (),
36- 'code ' => $ e ->getCode ()
37- ]
38- ];
39- }
20+ $ response = match ($ this ) {
21+ self ::RETRIEVE_ALL => $ item ->retrieveAll (),
22+ self ::RETRIEVE => $ item ->retrieve ($ itemId ),
23+ };
4024
4125 return json_encode ($ response );
4226 }
Original file line number Diff line number Diff line change 44use PH7 \JustHttp \StatusCode ;
55use PH7 \PhpHttpResponseHeader \Http ;
66
7+ // PHP 7.4 anonymous arrow function
78$ getResponse = fn (): string => json_encode (['error ' => 'Request not found ' ]);
89
910// Send HTTP 404 Not Found
Original file line number Diff line number Diff line change 33
44use PH7 \ApiSimpleMenu \Route \Exception \NotFoundException ;
55use PH7 \ApiSimpleMenu \Service \Exception \CredentialsInvalidException ;
6+ use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
7+ use PH7 \JustHttp \StatusCode ;
8+ use PH7 \PhpHttpResponseHeader \Http as HttpResponse ;
69
710$ resource = $ _REQUEST ['resource ' ] ?? null ;
811
1417 };
1518} catch (CredentialsInvalidException $ e ) {
1619 response ([
17- 'message ' => $ e ->getMessage ()
20+ 'errors ' => [
21+ 'message ' => $ e ->getMessage ()
22+ ]
23+ ]);
24+ } catch (InvalidValidationException $ e ) {
25+ // Send 400 http status code
26+ HttpResponse::setHeadersByCode (StatusCode::BAD_REQUEST );
27+
28+ response ([
29+ 'errors ' => [
30+ 'message ' => $ e ->getMessage (),
31+ 'code ' => $ e ->getCode ()
32+ ]
1833 ]);
1934} catch (NotFoundException $ e ) {
2035 // FYI, not-found.Route already sends a 404 Not Found HTTP code
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 \CannotLoginUserException ;
56use PH7 \ApiSimpleMenu \Service \Exception \EmailExistsException ;
67use PH7 \ApiSimpleMenu \Service \SecretKey ;
78use PH7 \ApiSimpleMenu \Service \User ;
8- use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
99
1010use PH7 \JustHttp \StatusCode ;
1111use PH7 \PhpHttpResponseHeader \Http as HttpResponse ;
@@ -57,14 +57,13 @@ public function getResponse(): string
5757 self ::RETRIEVE => $ user ->retrieve ($ userId ),
5858 self ::REMOVE => $ user ->remove ($ postBody ),
5959 };
60- } catch (InvalidValidationException $ e ) {
60+ } catch (CannotLoginUserException $ e ) {
6161 // Send 400 http status code
6262 HttpResponse::setHeadersByCode (StatusCode::BAD_REQUEST );
6363
6464 $ response = [
6565 'errors ' => [
6666 'message ' => $ e ->getMessage (),
67- 'code ' => $ e ->getCode ()
6867 ]
6968 ];
7069 } catch (EmailExistsException $ e ) {
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 CannotLoginUserException extends RuntimeException
8+ {
9+ }
Original file line number Diff line number Diff line change 11<?php
22namespace PH7 \ApiSimpleMenu \Service ;
33
4+ use Exception ;
45use Firebase \JWT \JWT ;
56use PH7 \ApiSimpleMenu \Dal \UserDal ;
7+ use PH7 \ApiSimpleMenu \Service \Exception \CannotLoginUserException ;
68use PH7 \ApiSimpleMenu \Service \Exception \EmailExistsException ;
79use PH7 \ApiSimpleMenu \Service \Exception \CredentialsInvalidException ;
810use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
@@ -47,7 +49,11 @@ public function login(mixed $data): array
4749 $ _ENV ['JWT_ALGO_ENCRYPTION ' ]
4850 );
4951
50- UserDal::setToken ($ jwtToken , $ user ->getUserUuid ());
52+ try {
53+ UserDal::setToken ($ jwtToken , $ user ->getUserUuid ());
54+ } catch (Exception $ e ) {
55+ throw new CannotLoginUserException ('Cannot set token to user ' );
56+ }
5157
5258 return [
5359 'message ' => sprintf ('%s successfully logged in ' , $ userName ),
You can’t perform that action at this time.
0 commit comments