From 178e16627f2f8bd57d8cfd56f9ed80c85db081c7 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 13 Sep 2024 13:50:50 -0400 Subject: [PATCH] Add new Enums to enhance error handling and session management --- src/Socialbox/Enums/ReservedUsernames.php | 10 ++++ .../Enums/SecondLevelAuthentication.php | 8 +++ src/Socialbox/Enums/SessionState.php | 21 +++++++ src/Socialbox/Enums/StandardError.php | 46 +++++++++++++++ src/Socialbox/Enums/StandardHeaders.php | 59 +++++++++++++++++++ src/Socialbox/Enums/StandardMethods.php | 26 ++++++++ src/Socialbox/Enums/StandardResponseCodes.php | 27 +++++++++ 7 files changed, 197 insertions(+) create mode 100644 src/Socialbox/Enums/ReservedUsernames.php create mode 100644 src/Socialbox/Enums/SecondLevelAuthentication.php create mode 100644 src/Socialbox/Enums/SessionState.php create mode 100644 src/Socialbox/Enums/StandardError.php create mode 100644 src/Socialbox/Enums/StandardHeaders.php create mode 100644 src/Socialbox/Enums/StandardMethods.php create mode 100644 src/Socialbox/Enums/StandardResponseCodes.php diff --git a/src/Socialbox/Enums/ReservedUsernames.php b/src/Socialbox/Enums/ReservedUsernames.php new file mode 100644 index 0000000..651d22b --- /dev/null +++ b/src/Socialbox/Enums/ReservedUsernames.php @@ -0,0 +1,10 @@ + 'Unknown Error', + + self::RPC_METHOD_NOT_FOUND => 'The request method was not found', + self::RPC_INVALID_ARGUMENTS => 'The request method contains one or more invalid arguments', + + self::INTERNAL_SERVER_ERROR => 'Internal server error', + self::SERVER_UNAVAILABLE => 'Server temporarily unavailable', + + self::INVALID_PUBLIC_KEY => 'The given public key is not valid', + self::SESSION_NOT_FOUND => 'The requested session UUID was not found', + self::UNSUPPORTED_AUTHENTICATION_TYPE => 'The requested authentication type is not supported by the server' + }; + + } +} \ No newline at end of file diff --git a/src/Socialbox/Enums/StandardHeaders.php b/src/Socialbox/Enums/StandardHeaders.php new file mode 100644 index 0000000..ec0b6fb --- /dev/null +++ b/src/Socialbox/Enums/StandardHeaders.php @@ -0,0 +1,59 @@ + true, + + default => false, + }; + } + + /** + * Retrieves an array of required headers. + * + * @return array An array containing only the headers that are marked as required. + */ + public static function getRequiredHeaders(): array + { + /** @var StandardHeaders $header */ + return array_filter(StandardHeaders::toArray(), fn($header) => $header->isRequired()); + } + + /** + * @return array + */ + public static function toArray(): array + { + $results = []; + foreach(StandardHeaders::values() as $header) + { + $results[$header->getValue()] = $header; + } + + return $results; + } +} \ No newline at end of file diff --git a/src/Socialbox/Enums/StandardMethods.php b/src/Socialbox/Enums/StandardMethods.php new file mode 100644 index 0000000..456d2b7 --- /dev/null +++ b/src/Socialbox/Enums/StandardMethods.php @@ -0,0 +1,26 @@ + Ping::execute($request, $rpcRequest), + self::CREATE_SESSION => CreateSession::execute($request, $rpcRequest), + + default => $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, sprintf('The method %s is not handled by the server', $this->value)), + }; + } +} \ No newline at end of file diff --git a/src/Socialbox/Enums/StandardResponseCodes.php b/src/Socialbox/Enums/StandardResponseCodes.php new file mode 100644 index 0000000..a6c1769 --- /dev/null +++ b/src/Socialbox/Enums/StandardResponseCodes.php @@ -0,0 +1,27 @@ +