From ff4c2a9557ebb42d378e2890439f97bb2a0f5c6b Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 10 Aug 2021 13:59:10 +0300 Subject: [PATCH] feat(endpoints): add and use method getStatusCodeMessage #565 --- src/flextype/core/Endpoints/Endpoints.php | 20 ++++++++++++++++---- src/flextype/core/Endpoints/Utils.php | 10 +++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/flextype/core/Endpoints/Endpoints.php b/src/flextype/core/Endpoints/Endpoints.php index ed86cb9b..2d94687c 100644 --- a/src/flextype/core/Endpoints/Endpoints.php +++ b/src/flextype/core/Endpoints/Endpoints.php @@ -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 * diff --git a/src/flextype/core/Endpoints/Utils.php b/src/flextype/core/Endpoints/Utils.php index 25e4183d..2ab56120 100644 --- a/src/flextype/core/Endpoints/Utils.php +++ b/src/flextype/core/Endpoints/Utils.php @@ -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