Refactor AddressBookGetContacts and ContactManager to improve parameter validation and error handling

https://github.com/nosial/Socialbox-PHP/issues/34
This commit is contained in:
netkas 2025-03-11 19:14:58 -04:00
parent eadf1cd3a4
commit b29f3ed1e2
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
2 changed files with 10 additions and 18 deletions

View file

@ -6,7 +6,6 @@
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\ContactManager;
@ -23,32 +22,20 @@
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
$limit = Configuration::getPoliciesConfiguration()->getGetContactsLimit();
if($rpcRequest->containsParameter('limit', true))
if($rpcRequest->containsParameter('limit'))
{
$limit = (int)$rpcRequest->getParameter('limit');
if($limit <= 0)
{
throw new InvalidRpcArgumentException('limit', 'Invalid limit, must be greater than 0');
}
$limit = min($limit, Configuration::getPoliciesConfiguration()->getGetContactsLimit());
}
$page = 0;
if($rpcRequest->containsParameter('page', true))
if($rpcRequest->containsParameter('page'))
{
$page = (int)$rpcRequest->getParameter('page');
if($page < 0)
{
throw new InvalidRpcArgumentException('page', 'Invalid page, must be greater than or equal to 0');
}
$page = max($page, 0);
}
try
{
return $rpcRequest->produceResponse(ContactManager::getStandardContacts($request->getPeer(), $limit, $page));
return $rpcRequest->produceResponse(ContactManager::getStandardContacts($request->getPeer()->getUuid(), $limit, $page));
}
catch(DatabaseOperationException $e)
{

View file

@ -7,6 +7,7 @@
use ncc\ThirdParty\Symfony\Uid\UuidV4;
use PDO;
use PDOException;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Database;
use Socialbox\Classes\Validator;
@ -317,12 +318,16 @@
{
if ($page < 1)
{
$page = 1;
throw new InvalidArgumentException('The page number cannot be less than 1');
}
if ($limit < 1)
{
$limit = 1;
throw new InvalidArgumentException('The limit cannot be less than 1');
}
elseif($limit > Configuration::getPoliciesConfiguration()->getGetContactsLimit())
{
throw new InvalidArgumentException('The limit cannot exceed a value of ' . Configuration::getPoliciesConfiguration()->getGetContactsLimit());
}
$contacts = [];