1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 05:07:41 +02:00

feat(endpoints): add and use method getStatusCodeMessage #565

This commit is contained in:
Awilum
2021-08-10 13:59:10 +03:00
parent 2246f88954
commit ff4c2a9557
2 changed files with 21 additions and 9 deletions

View File

@@ -19,21 +19,33 @@ class Endpoints
* @var array
* @access public
*/
public array $statusCodeMessages = [
'400' => [
private array $statusCodeMessages = [
400 => [
'title' => 'Bad Request',
'message' => 'Validation for this particular item failed',
],
'401' => [
401 => [
'title' => 'Unauthorized',
'message' => 'Token is wrong',
],
'404' => [
404 => [
'title' => 'Not Found',
'message' => 'Not Found',
]
];
/**
* Get Status Code Message.
*
* @param int $status Status Code.
*
* @return array Message.
*/
public function getStatusCodeMessage(int $status): array
{
return $this->statusCodeMessages[$status];
}
/**
* Get API responce
*

View File

@@ -34,17 +34,17 @@ class Utils extends Endpoints
// Check is utils api enabled
if (! registry()->get('flextype.settings.api.utils.enabled')) {
return $this->getApiResponse($response, $this->statusCodeMessages['400'], 400);
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
}
// Check is token param exists
if (! isset($data['token'])) {
return $this->getApiResponse($response, $this->statusCodeMessages['400'], 400);
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
}
// Check is token exists
if (! tokens()->has($data['token'])) {
return $this->getApiResponse($response, $this->statusCodeMessages['401'], 401);
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
}
// Fetch token
@@ -52,13 +52,13 @@ class Utils extends Endpoints
// Verify access token
if (password_verify($tokenData['hashed_access_token'], $data['access_token'])) {
return $this->getApiResponse($response, $this->statusCodeMessages['401'], 401);
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
}
// Check token state and limit_calls
if ($tokenData['state'] === 'disabled' ||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])) {
return $this->getApiResponse($response, $this->statusCodeMessages['400'], 400);
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
}
// Update token calls