Refactor EncryptionCloseChannel to simplify error handling and remove unused code

https://github.com/nosial/Socialbox-PHP/issues/19
This commit is contained in:
netkas 2025-03-11 15:03:26 -04:00
parent d5e540789b
commit 5be20b0e70
Signed by: netkas
GPG key ID: 4D8629441B76E4CC

View file

@ -4,13 +4,10 @@
use Exception; use Exception;
use Socialbox\Abstracts\Method; use Socialbox\Abstracts\Method;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Validator;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
use Socialbox\Enums\Status\EncryptionChannelStatus; use Socialbox\Enums\Status\EncryptionChannelStatus;
use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\RpcException; use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
use Socialbox\Exceptions\Standard\MissingRpcArgumentException; use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Exceptions\Standard\StandardRpcException;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
@ -31,15 +28,18 @@
{ {
throw new MissingRpcArgumentException('channel_uuid'); throw new MissingRpcArgumentException('channel_uuid');
} }
elseif(!Validator::validateUuid($rpcRequest->getParameter('channel_uuid')))
{
throw new InvalidRpcArgumentException('channel_uuid', 'The given channel uuid is not a valid UUID V4');
}
if($request->isExternal()) try
{
if ($request->isExternal())
{ {
return self::handleExternal($request, $rpcRequest); return self::handleExternal($request, $rpcRequest);
} }
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('There was an error while handling the external request', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return self::handleInternal($request, $rpcRequest); return self::handleInternal($request, $rpcRequest);
} }
@ -98,15 +98,6 @@
} }
catch(Exception $e) catch(Exception $e)
{ {
try
{
EncryptionChannelManager::declineChannel($rpcRequest->getParameter('channel_uuid'), true);
}
catch(DatabaseOperationException $e)
{
Logger::getLogger()->error('Error declining channel as server', $e);
}
if($e instanceof RpcException) if($e instanceof RpcException)
{ {
throw StandardRpcException::fromRpcException($e); throw StandardRpcException::fromRpcException($e);
@ -145,13 +136,11 @@
{ {
return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested encryption channel was not found'); return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested encryption channel was not found');
} }
elseif(!$encryptionChannel->isParticipant($request->getIdentifyAs()))
if(!$encryptionChannel->isParticipant($request->getIdentifyAs()))
{ {
return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The requested encryption channel is not accessible'); return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The requested encryption channel is not accessible');
} }
elseif($encryptionChannel->getStatus() === EncryptionChannelStatus::CLOSED)
if($encryptionChannel->getStatus() === EncryptionChannelStatus::CLOSED)
{ {
return $rpcRequest->produceResponse(false); return $rpcRequest->produceResponse(false);
} }