diff --git a/src/flextype/core/Endpoints/Api.php b/src/flextype/core/Endpoints/Api.php new file mode 100644 index 00000000..023b4a6a --- /dev/null +++ b/src/flextype/core/Endpoints/Api.php @@ -0,0 +1,119 @@ + [ + 'title' => 'Bad Request', + 'message' => 'Validation for this particular item failed', + 'http_status_code' => 400 + ], + 401 => [ + 'title' => 'Unauthorized', + 'message' => 'Token is wrong', + 'http_status_code' => 401 + ], + 404 => [ + 'title' => 'Not Found', + 'message' => 'Not Found', + 'http_status_code' => 404 + ], + ]; + + /** + * Get Status Code Message. + * + * @param int $status Status Code. + * + * @return array Message. + */ + public function getStatusCodeMessage(int $status): array + { + return $this->statusCodeMessages[$status]; + } + + /** + * Validate Api Request. + */ + public function validateApiRequest(array $options) + { + if (! isset($options['api']) && ! is_string($options['api'])) { + return $this->getStatusCodeMessage(400); + } + + if (! isset($options['request'])) { + return $this->getStatusCodeMessage(400); + } + + $data = array_merge($options['request']->getQueryParams() ?? [], $options['request']->getParsedBody() ?? []); + + // Check is api enabled + if (! registry()->get('flextype.settings.api.' . $options['api'] . '.enabled')) { + return $this->getStatusCodeMessage(400); + } + + // Fetch token + $tokenData = tokens()->fetch($data['token']); + + if (! isset($tokenData['state']) || + ! isset($tokenData['limit_calls']) || + ! isset($tokenData['calls'])) { + return $this->getStatusCodeMessage(400); + } + + if ( + $tokenData['state'] === 'disabled' || + ($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls']) + ) { + return $this->getStatusCodeMessage(400); + } + + // Update token calls + tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]); + + return []; + } + + /** + * Get API response. + * + * @param array $body Response body. + * @param int $status Status code. + * + * @return ResponseInterface Response. + */ + public function getApiResponse($response, array $body = [], int $status = 200) + { + if (count($body) > 0) { + $response->getBody()->write(serializers()->json()->encode($body)); + } + + $response->withStatus($status); + $response->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')); + + return $response; + } +} diff --git a/src/flextype/core/Endpoints/Endpoints.php b/src/flextype/core/Endpoints/Endpoints.php deleted file mode 100644 index fe0568d5..00000000 --- a/src/flextype/core/Endpoints/Endpoints.php +++ /dev/null @@ -1,73 +0,0 @@ - [ - 'title' => 'Bad Request', - 'message' => 'Validation for this particular item failed', - ], - 401 => [ - 'title' => 'Unauthorized', - 'message' => 'Token is wrong', - ], - 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 - * - * @param ResponseInterface $response PSR7 response. - * @param array $body Response body. - * @param int $status Status code. - * - * @return ResponseInterface Response. - */ - public function getApiResponse(ResponseInterface $response, array $body = [], int $status = 200): ResponseInterface - { - if (count($body) > 0) { - $response->getBody()->write(serializers()->json()->encode($body)); - } - - $response->withStatus($status); - $response->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')); - - return $response; - } -}