Refactor RPC framework and enhance error handling.

This commit is contained in:
netkas 2024-12-19 15:09:22 -05:00
parent 42ba7013f7
commit ef3b10b286
10 changed files with 383 additions and 245 deletions

View file

@ -4,6 +4,7 @@
use InvalidArgumentException;
use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\SessionState;
use Socialbox\Enums\StandardHeaders;
@ -141,7 +142,7 @@
try
{
return Cryptography::verifyContent(hash('sha1', $decryptedContent), $this->getSignature(), $this->getSession()->getPublicKey());
return Cryptography::verifyContent($decryptedContent, $this->getSignature(), $this->getSession()->getPublicKey(), true);
}
catch(CryptographyException)
{

View file

@ -1,98 +1,112 @@
<?php
namespace Socialbox\Objects;
namespace Socialbox\Objects;
use Socialbox\Enums\StandardError;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Interfaces\SerializableInterface;
class RpcError implements SerializableInterface
{
private string $id;
private string $error;
private StandardError $code;
/**
* Constructs the RPC error object.
*
* @param string $id The ID of the RPC request
* @param StandardError $code The error code
* @param string|null $error The error message
*/
public function __construct(string $id, StandardError $code, ?string $error)
class RpcError implements SerializableInterface
{
$this->id = $id;
$this->code = $code;
private string $id;
private StandardError $code;
private string $error;
if($error === null)
/**
* Constructs the RPC error object.
*
* @param string $id The ID of the RPC request
* @param StandardError|int $code The error code
* @param string|null $error The error message
*/
public function __construct(string $id, StandardError|int $code, ?string $error)
{
$this->error = $code->getMessage();
}
else
{
$this->error = $error;
$this->id = $id;
if(is_int($code))
{
$code = StandardError::tryFrom($code);
if($code === null)
{
$code = StandardError::UNKNOWN;
}
}
$this->code = $code;
if($error === null)
{
$this->error = $code->getMessage();
}
else
{
$this->error = $error;
}
}
}
/**
* Returns the ID of the RPC request.
*
* @return string The ID of the RPC request.
*/
public function getId(): string
{
return $this->id;
}
/**
* Returns the error message.
*
* @return string The error message.
*/
public function getError(): string
{
return $this->error;
}
/**
* Returns the error code.
*
* @return StandardError The error code.
*/
public function getCode(): StandardError
{
return $this->code;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'error' => $this->error,
'code' => $this->code->value
];
}
/**
* Returns the RPC error object from an array of data.
*
* @param array $data The data to construct the RPC error from.
* @return RpcError The RPC error object.
*/
public static function fromArray(array $data): RpcError
{
$errorCode = StandardError::tryFrom($data['code']);
if($errorCode == null)
/**
* Returns the ID of the RPC request.
*
* @return string The ID of the RPC request.
*/
public function getId(): string
{
$errorCode = StandardError::UNKNOWN;
return $this->id;
}
return new RpcError($data['id'], $data['error'], $errorCode);
}
}
/**
* Returns the error code.
*
* @return StandardError The error code.
*/
public function getCode(): StandardError
{
return $this->code;
}
/**
* Returns the error message.
*
* @return string The error message.
*/
public function getError(): string
{
return $this->error;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'code' => $this->code->value,
'error' => $this->error
];
}
/**
* Converts the current object to an RpcException instance.
*
* @return RpcException The RpcException generated from the current object.
*/
public function toRpcException(): RpcException
{
return new RpcException($this->error, $this->code->value);
}
/**
* Returns the RPC error object from an array of data.
*
* @param array $data The data to construct the RPC error from.
* @return RpcError The RPC error object.
*/
public static function fromArray(array $data): RpcError
{
return new RpcError($data['id'], $data['code'], $data['error']);
}
}

View file

@ -23,7 +23,7 @@
* @param string|null $id The ID of the request.
* @param array|null $parameters The parameters of the request.
*/
public function __construct(string $method, ?string $id, ?array $parameters)
public function __construct(string $method, ?string $id, ?array $parameters=null)
{
$this->method = $method;
$this->parameters = $parameters;

View file

@ -1,84 +1,84 @@
<?php
namespace Socialbox\Objects;
namespace Socialbox\Objects;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Interfaces\SerializableInterface;
class RpcResponse implements SerializableInterface
{
private string $id;
private mixed $result;
/**
* Constructs the response object.
*
* @param string $id The ID of the response.
* @param mixed|null $result The result of the response.
*/
public function __construct(string $id, mixed $result)
class RpcResponse implements SerializableInterface
{
$this->id = $id;
$this->result = $result;
}
private string $id;
private mixed $result;
/**
* Returns the ID of the response.
*
* @return string The ID of the response.
*/
public function getId(): string
{
return $this->id;
}
/**
* Returns the result of the response.
*
* @return mixed|null The result of the response.
*/
public function getResult(): mixed
{
return $this->result;
}
/**
* Converts the given data to an array.
*
* @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value.
* @return mixed The converted data as an array if applicable, or the original data.
*/
private function convertToArray(mixed $data): mixed
{
// If the data is an instance of SerializableInterface, call toArray on it
if ($data instanceof SerializableInterface)
/**
* Constructs the response object.
*
* @param string $id The ID of the response.
* @param mixed|null $result The result of the response.
*/
public function __construct(string $id, mixed $result)
{
return $data->toArray();
$this->id = $id;
$this->result = $result;
}
return $data;
}
/**
* Returns the ID of the response.
*
* @return string The ID of the response.
*/
public function getId(): string
{
return $this->id;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'result' => $this->convertToArray($this->result)
];
}
/**
* Returns the result of the response.
*
* @return mixed|null The result of the response.
*/
public function getResult(): mixed
{
return $this->result;
}
/**
* Returns the response object from an array of data.
*
* @param array $data The data to construct the response from.
* @return RpcResponse The response object.
*/
public static function fromArray(array $data): RpcResponse
{
return new RpcResponse($data['id'], $data['result']);
}
}
/**
* Converts the given data to an array.
*
* @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value.
* @return mixed The converted data as an array if applicable, or the original data.
*/
private function convertToArray(mixed $data): mixed
{
// If the data is an instance of SerializableInterface, call toArray on it
if ($data instanceof SerializableInterface)
{
return $data->toArray();
}
return $data;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'result' => $this->convertToArray($this->result)
];
}
/**
* Returns the response object from an array of data.
*
* @param array $data The data to construct the response from.
* @return RpcResponse The response object.
*/
public static function fromArray(array $data): RpcResponse
{
return new RpcResponse($data['id'], $data['result']);
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace Socialbox\Objects;
class RpcResult
{
private ?RpcResponse $response;
private ?RpcError $error;
/**
* Constructor for initializing the instance with a response or error.
*
* @param RpcResponse|RpcError|array $response An instance of RpcResponse, RpcError, or an associative array
* containing error or result data to initialize the class.
* @return void
*/
public function __construct(RpcResponse|RpcError|array $response)
{
if(is_array($response))
{
if(isset($response['error']) && isset($response['code']))
{
$response = RpcError::fromArray($response);
}
elseif(isset($response['result']))
{
$response = RpcResponse::fromArray($response);
}
else
{
$response = null;
}
}
if($response === null)
{
$this->error = null;
$this->response = null;
return;
}
if($response instanceof RpcResponse)
{
$this->response = $response;
$this->error = null;
return;
}
if($response instanceof RpcError)
{
$this->error = $response;
$this->response = null;
}
}
/**
* Checks whether the operation was successful.
*
* @return bool True if there is no error, otherwise false.
*/
public function isSuccess(): bool
{
return $this->error === null;
}
/**
* Checks if the instance contains no error and no response.
*
* @return bool True if both error and response are null, otherwise false.
*/
public function isEmpty(): bool
{
return $this->error === null && $this->response === null;
}
/**
* Retrieves the error associated with the instance, if any.
*
* @return RpcError|null The error object if an error exists, or null if no error is present.
*/
public function getError(): ?RpcError
{
return $this->error;
}
/**
* Retrieves the RPC response if available.
*
* @return RpcResponse|null The response object if set, or null if no response is present.
*/
public function getResponse(): ?RpcResponse
{
return $this->response;
}
}