Add new Enums to enhance error handling and session management

This commit is contained in:
netkas 2024-09-13 13:50:50 -04:00
parent 2cf1a55e8f
commit 178e16627f
7 changed files with 197 additions and 0 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace Socialbox\Enums;
enum ReservedUsernames : string
{
case HOST = 'host';
case ANONYMOUS = 'anonymous';
case ADMIN = 'admin';
}

View file

@ -0,0 +1,8 @@
<?php
namespace Socialbox\Enums;
enum SecondLevelAuthentication : string
{
case ONE_TIME_PASSWORD = 'OTP';
}

View file

@ -0,0 +1,21 @@
<?php
namespace Socialbox\Enums;
enum SessionState : string
{
/**
* The session is currently active and usable
*/
case ACTIVE = 'ACTIVE';
/**
* The session has expired and is no longer usable
*/
case EXPIRED = 'EXPIRED';
/**
* The session was closed either by the client or the server and is no longer usable
*/
case CLOSED = 'CLOSED';
}

View file

@ -0,0 +1,46 @@
<?php
namespace Socialbox\Enums;
enum StandardError : int
{
// Fallback Codes
case UNKNOWN = -1;
// RPC Errors
case RPC_METHOD_NOT_FOUND = -1000;
case RPC_INVALID_ARGUMENTS = -1001;
// Server Errors
case INTERNAL_SERVER_ERROR = -2000;
case SERVER_UNAVAILABLE = -2001;
// Authentication/Cryptography Errors
case INVALID_PUBLIC_KEY = -3000;
case SESSION_NOT_FOUND = -3001;
case UNSUPPORTED_AUTHENTICATION_TYPE = -3002;
/**
* Returns the default generic message for the error
*
* @return string
*/
public function getMessage(): string
{
return match ($this)
{
self::UNKNOWN => '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'
};
}
}

View file

@ -0,0 +1,59 @@
<?php
namespace Socialbox\Enums;
/**
* Enumeration of standard header names used in HTTP communication.
*/
enum StandardHeaders : string
{
case CONTENT_TYPE = 'Content-Type';
case CLIENT_NAME = 'Client-Name';
case CLIENT_VERSION = 'Client-Version';
case SESSION_UUID = 'Session-UUID';
case FROM_PEER = 'From-Peer';
case SIGNATURE = 'Signature';
/**
* Determines if the current instance is required based on its type.
*
* @return bool Returns true if the instance is of type CONTENT_TYPE, CLIENT_VERSION, or CLIENT_NAME; otherwise, false.
*/
public function isRequired(): bool
{
return match($this)
{
self::CONTENT_TYPE,
self::CLIENT_VERSION,
self::CLIENT_NAME
=> 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;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Socialbox\Enums;
use Socialbox\Classes\StandardMethods\CreateSession;
use Socialbox\Classes\StandardMethods\Ping;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
enum StandardMethods : string
{
case PING = 'ping';
case CREATE_SESSION = 'create_session';
public function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
return match ($this)
{
self::PING => 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)),
};
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Socialbox\Enums;
enum StandardResponseCodes : int
{
/**
* The RPC Request was successful
*/
case OK = 200;
/**
* The RPC Request was successful but no response was returned
*/
case EMPTY = 204;
/**
* Bad RPC Request, fatal issue with how the client is producing the requests.
*/
case BAD_REQUEST = 400;
/**
* Unexpected Internal Server error, general catch-all for anything RPC out outside related.
* Anything internal via an RPC request should return the RpcError with the error code -2000
*/
case INTERNAL_SERVER_ERROR = 500;
}