2024-08-31 16:36:22 -04:00
|
|
|
<?php
|
|
|
|
|
2024-09-13 13:52:38 -04:00
|
|
|
namespace Socialbox;
|
|
|
|
|
|
|
|
use Socialbox\Classes\RpcHandler;
|
|
|
|
use Socialbox\Enums\StandardError;
|
|
|
|
use Socialbox\Enums\StandardMethods;
|
|
|
|
use Socialbox\Exceptions\RpcException;
|
2024-08-31 16:36:22 -04:00
|
|
|
|
2024-08-31 17:11:25 -04:00
|
|
|
class Socialbox
|
2024-08-31 16:36:22 -04:00
|
|
|
{
|
2024-09-13 13:52:38 -04:00
|
|
|
public static function handleRpc(): void
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$clientRequest = RpcHandler::getClientRequest();
|
|
|
|
}
|
|
|
|
catch(RpcException $e)
|
|
|
|
{
|
|
|
|
http_response_code($e->getCode());
|
|
|
|
print($e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach($clientRequest->getRequests() as $rpcRequest)
|
|
|
|
{
|
|
|
|
$method = StandardMethods::tryFrom($rpcRequest->getMethod());
|
|
|
|
|
|
|
|
if($method === false)
|
|
|
|
{
|
|
|
|
$response = $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist');;
|
|
|
|
if($response !== null)
|
|
|
|
{
|
|
|
|
$results[] = $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $method->execute($clientRequest, $rpcRequest);
|
|
|
|
if($response !== null)
|
|
|
|
{
|
|
|
|
$results[] = $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($results) > 0)
|
|
|
|
{
|
|
|
|
print(json_encode($results));
|
|
|
|
return;
|
|
|
|
}
|
2024-08-31 16:36:22 -04:00
|
|
|
|
2024-09-13 13:52:38 -04:00
|
|
|
http_response_code(204);
|
|
|
|
}
|
2024-08-31 16:36:22 -04:00
|
|
|
}
|