Skip to content

Commit 4043c89

Browse files
committed
Minor refactoring for improving code quality
1 parent 530188b commit 4043c89

8 files changed

+43
-26
lines changed

src/Dal/TokenKeyDal.php

+1-1
Original file line numberDiff line numberDiff 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;

src/Dal/UserDal.php

+3
Original file line numberDiff line numberDiff 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];

src/Route/food-item.routes.php

+4-20
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
namespace PH7\ApiSimpleMenu\Route;
33

44
use PH7\ApiSimpleMenu\Service\FoodItem;
5-
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
6-
7-
use PH7\JustHttp\StatusCode;
8-
use PH7\PhpHttpResponseHeader\Http;
95

106
enum 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
}

src/Route/not-found.routes.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PH7\JustHttp\StatusCode;
55
use 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

src/Route/routes.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
55
use 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

@@ -14,7 +17,19 @@
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

src/Route/user.routes.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
namespace PH7\ApiSimpleMenu\Route;
33

44
use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
5+
use PH7\ApiSimpleMenu\Service\Exception\CannotLoginUserException;
56
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
67
use PH7\ApiSimpleMenu\Service\SecretKey;
78
use PH7\ApiSimpleMenu\Service\User;
8-
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
99

1010
use PH7\JustHttp\StatusCode;
1111
use 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 numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Service\Exception;
4+
5+
use RuntimeException;
6+
7+
class CannotLoginUserException extends RuntimeException
8+
{
9+
}

src/Service/User.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
namespace PH7\ApiSimpleMenu\Service;
33

4+
use Exception;
45
use Firebase\JWT\JWT;
56
use PH7\ApiSimpleMenu\Dal\UserDal;
7+
use PH7\ApiSimpleMenu\Service\Exception\CannotLoginUserException;
68
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
79
use PH7\ApiSimpleMenu\Service\Exception\CredentialsInvalidException;
810
use 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),

0 commit comments

Comments
 (0)