Refactor SettingsAddInformationField to improve type casting for field and privacy parameters, and enhance error logging for rollback failures

https://github.com/nosial/Socialbox-PHP/issues/54
This commit is contained in:
netkas 2025-03-12 15:20:35 -04:00
parent e1b61e631f
commit 0618ecc00f
Signed by: netkas
GPG key ID: 4D8629441B76E4CC

View file

@ -4,6 +4,7 @@
use Exception; use Exception;
use Socialbox\Abstracts\Method; use Socialbox\Abstracts\Method;
use Socialbox\Classes\Logger;
use Socialbox\Enums\Flags\SessionFlags; use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\PrivacyState; use Socialbox\Enums\PrivacyState;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
@ -31,7 +32,8 @@
{ {
throw new MissingRpcArgumentException('field'); throw new MissingRpcArgumentException('field');
} }
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
$fieldName = InformationFieldName::tryFrom(strtoupper((string)$rpcRequest->getParameter('field')));
if($fieldName === null) if($fieldName === null)
{ {
throw new InvalidRpcArgumentException('field'); throw new InvalidRpcArgumentException('field');
@ -42,17 +44,14 @@
{ {
throw new MissingRpcArgumentException('value'); throw new MissingRpcArgumentException('value');
} }
$value = $rpcRequest->getParameter('value'); $value = $rpcRequest->getParameter('value');
if(!$fieldName->validate($value))
{
throw new InvalidRpcArgumentException('value');
}
// Privacy parameter is optional // Privacy parameter is optional
$privacy = null; $privacy = null;
if($rpcRequest->containsParameter('privacy') && $rpcRequest->getParameter('privacy') !== null) if($rpcRequest->containsParameter('privacy') && $rpcRequest->getParameter('privacy') !== null)
{ {
$privacy = PrivacyState::tryFrom(strtoupper($rpcRequest->getParameter('privacy'))); $privacy = PrivacyState::tryFrom(strtoupper((string)$rpcRequest->getParameter('privacy')));
if($privacy === null) if($privacy === null)
{ {
throw new InvalidRpcArgumentException('privacy'); throw new InvalidRpcArgumentException('privacy');
@ -133,8 +132,7 @@
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {
// Something is seriously wrong if we can't roll back the information field Logger::getLogger()->critical('Failed to rollback information field', $e);
throw new StandardRpcException('Failed to rollback the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
} }
if($e instanceof StandardRpcException) if($e instanceof StandardRpcException)