Added SettingsGetInformationField as a standard method

This commit is contained in:
netkas 2025-01-29 15:26:22 -05:00
parent f76f76d31a
commit 42d276939b

View file

@ -0,0 +1,48 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PeerInformationManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsGetInformationField extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('field'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required field parameter is missing');
}
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
if($fieldName === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is invalid');
}
try
{
$fieldRecord = PeerInformationManager::getField($request->getPeer(), $fieldName);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to retrieve existing information fields', StandardError::INTERNAL_SERVER_ERROR, $e);
}
if($fieldRecord === null)
{
return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested field does not exist');
}
return $rpcRequest->produceResponse($fieldRecord->toInformationFieldState());
}
}