From 5be20b0e702b72f0e3b78d30ba3cef44998c701a Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 11 Mar 2025 15:03:26 -0400 Subject: [PATCH] Refactor EncryptionCloseChannel to simplify error handling and remove unused code https://github.com/nosial/Socialbox-PHP/issues/19 --- .../EncryptionCloseChannel.php | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionCloseChannel.php b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionCloseChannel.php index 159348c..5b54a70 100644 --- a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionCloseChannel.php +++ b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionCloseChannel.php @@ -4,13 +4,10 @@ use Exception; use Socialbox\Abstracts\Method; - use Socialbox\Classes\Logger; - use Socialbox\Classes\Validator; use Socialbox\Enums\StandardError; use Socialbox\Enums\Status\EncryptionChannelStatus; use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\RpcException; - use Socialbox\Exceptions\Standard\InvalidRpcArgumentException; use Socialbox\Exceptions\Standard\MissingRpcArgumentException; use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Interfaces\SerializableInterface; @@ -31,14 +28,17 @@ { 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 { - return self::handleExternal($request, $rpcRequest); + if ($request->isExternal()) + { + 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); @@ -98,15 +98,6 @@ } 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) { throw StandardRpcException::fromRpcException($e); @@ -145,13 +136,11 @@ { return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested encryption channel was not found'); } - - if(!$encryptionChannel->isParticipant($request->getIdentifyAs())) + elseif(!$encryptionChannel->isParticipant($request->getIdentifyAs())) { return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The requested encryption channel is not accessible'); } - - if($encryptionChannel->getStatus() === EncryptionChannelStatus::CLOSED) + elseif($encryptionChannel->getStatus() === EncryptionChannelStatus::CLOSED) { return $rpcRequest->produceResponse(false); }