Skip to content

Commit

Permalink
Minor refactoring for improving code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Jul 13, 2023
1 parent 530188b commit 4043c89
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Dal/TokenKeyDal.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class TokenKeyDal
{
public const TABLE_NAME = 'secretkeys';

public static function saveSecretKey(string $jwtKey)
public static function saveSecretKey(string $jwtKey): void
{
$tokenBean = R::dispense(self::TABLE_NAME);
$tokenBean->secretKey = $jwtKey;
Expand Down
3 changes: 3 additions & 0 deletions src/Dal/UserDal.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public static function getByEmail(string $email): UserEntity
return (new UserEntity())->unserialize($userBean?->export());
}

/**
* @throws \RedBeanPHP\RedException\SQL
*/
public static function setToken(string $jwtToken, string $userUuid): void
{
$bindings = ['userUuid' => $userUuid];
Expand Down
24 changes: 4 additions & 20 deletions src/Route/food-item.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
namespace PH7\ApiSimpleMenu\Route;

use PH7\ApiSimpleMenu\Service\FoodItem;
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;

use PH7\JustHttp\StatusCode;
use PH7\PhpHttpResponseHeader\Http;

enum FoodItemAction: string
{
Expand All @@ -21,22 +17,10 @@ public function getResponse(): string
$itemId = $_REQUEST['id'] ?? ''; // using the null coalescing operator

$item = new FoodItem();
try {
$response = match ($this) {
self::RETRIEVE_ALL => $item->retrieveAll(),
self::RETRIEVE => $item->retrieve($itemId),
};
} catch (InvalidValidationException $e) {
// Send 400 http status code
Http::setHeadersByCode(StatusCode::BAD_REQUEST);

$response = [
'errors' => [
'message' => $e->getMessage(),
'code' => $e->getCode()
]
];
}
$response = match ($this) {
self::RETRIEVE_ALL => $item->retrieveAll(),
self::RETRIEVE => $item->retrieve($itemId),
};

return json_encode($response);
}
Expand Down
1 change: 1 addition & 0 deletions src/Route/not-found.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PH7\JustHttp\StatusCode;
use PH7\PhpHttpResponseHeader\Http;

// PHP 7.4 anonymous arrow function
$getResponse = fn(): string => json_encode(['error' => 'Request not found']);

// Send HTTP 404 Not Found
Expand Down
17 changes: 16 additions & 1 deletion src/Route/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
use PH7\ApiSimpleMenu\Service\Exception\CredentialsInvalidException;
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
use PH7\JustHttp\StatusCode;
use PH7\PhpHttpResponseHeader\Http as HttpResponse;

$resource = $_REQUEST['resource'] ?? null;

Expand All @@ -14,7 +17,19 @@
};
} catch (CredentialsInvalidException $e) {
response([
'message' => $e->getMessage()
'errors' => [
'message' => $e->getMessage()
]
]);
} catch (InvalidValidationException $e) {
// Send 400 http status code
HttpResponse::setHeadersByCode(StatusCode::BAD_REQUEST);

response([
'errors' => [
'message' => $e->getMessage(),
'code' => $e->getCode()
]
]);
} catch (NotFoundException $e) {
// FYI, not-found.Route already sends a 404 Not Found HTTP code
Expand Down
5 changes: 2 additions & 3 deletions src/Route/user.routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
namespace PH7\ApiSimpleMenu\Route;

use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
use PH7\ApiSimpleMenu\Service\Exception\CannotLoginUserException;
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
use PH7\ApiSimpleMenu\Service\SecretKey;
use PH7\ApiSimpleMenu\Service\User;
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;

use PH7\JustHttp\StatusCode;
use PH7\PhpHttpResponseHeader\Http as HttpResponse;
Expand Down Expand Up @@ -57,14 +57,13 @@ public function getResponse(): string
self::RETRIEVE => $user->retrieve($userId),
self::REMOVE => $user->remove($postBody),
};
} catch (InvalidValidationException $e) {
} catch (CannotLoginUserException $e) {
// Send 400 http status code
HttpResponse::setHeadersByCode(StatusCode::BAD_REQUEST);

$response = [
'errors' => [
'message' => $e->getMessage(),
'code' => $e->getCode()
]
];
} catch (EmailExistsException $e) {
Expand Down
9 changes: 9 additions & 0 deletions src/Service/Exception/CannotLoginUserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PH7\ApiSimpleMenu\Service\Exception;

use RuntimeException;

class CannotLoginUserException extends RuntimeException
{
}
8 changes: 7 additions & 1 deletion src/Service/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace PH7\ApiSimpleMenu\Service;

use Exception;
use Firebase\JWT\JWT;
use PH7\ApiSimpleMenu\Dal\UserDal;
use PH7\ApiSimpleMenu\Service\Exception\CannotLoginUserException;
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
use PH7\ApiSimpleMenu\Service\Exception\CredentialsInvalidException;
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
Expand Down Expand Up @@ -47,7 +49,11 @@ public function login(mixed $data): array
$_ENV['JWT_ALGO_ENCRYPTION']
);

UserDal::setToken($jwtToken, $user->getUserUuid());
try {
UserDal::setToken($jwtToken, $user->getUserUuid());
} catch (Exception $e) {
throw new CannotLoginUserException('Cannot set token to user');
}

return [
'message' => sprintf('%s successfully logged in', $userName),
Expand Down

0 comments on commit 4043c89

Please sign in to comment.