Refactor AddressBookGetContacts and ContactManager to improve parameter validation and error handling
https://github.com/nosial/Socialbox-PHP/issues/34
This commit is contained in:
parent
eadf1cd3a4
commit
b29f3ed1e2
2 changed files with 10 additions and 18 deletions
|
@ -6,7 +6,6 @@
|
||||||
use Socialbox\Classes\Configuration;
|
use Socialbox\Classes\Configuration;
|
||||||
use Socialbox\Enums\StandardError;
|
use Socialbox\Enums\StandardError;
|
||||||
use Socialbox\Exceptions\DatabaseOperationException;
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
|
|
||||||
use Socialbox\Exceptions\Standard\StandardRpcException;
|
use Socialbox\Exceptions\Standard\StandardRpcException;
|
||||||
use Socialbox\Interfaces\SerializableInterface;
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
use Socialbox\Managers\ContactManager;
|
use Socialbox\Managers\ContactManager;
|
||||||
|
@ -23,32 +22,20 @@
|
||||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||||
{
|
{
|
||||||
$limit = Configuration::getPoliciesConfiguration()->getGetContactsLimit();
|
$limit = Configuration::getPoliciesConfiguration()->getGetContactsLimit();
|
||||||
if($rpcRequest->containsParameter('limit', true))
|
if($rpcRequest->containsParameter('limit'))
|
||||||
{
|
{
|
||||||
$limit = (int)$rpcRequest->getParameter('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;
|
$page = 0;
|
||||||
if($rpcRequest->containsParameter('page', true))
|
if($rpcRequest->containsParameter('page'))
|
||||||
{
|
{
|
||||||
$page = (int)$rpcRequest->getParameter('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
|
try
|
||||||
{
|
{
|
||||||
return $rpcRequest->produceResponse(ContactManager::getStandardContacts($request->getPeer(), $limit, $page));
|
return $rpcRequest->produceResponse(ContactManager::getStandardContacts($request->getPeer()->getUuid(), $limit, $page));
|
||||||
}
|
}
|
||||||
catch(DatabaseOperationException $e)
|
catch(DatabaseOperationException $e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
use ncc\ThirdParty\Symfony\Uid\UuidV4;
|
use ncc\ThirdParty\Symfony\Uid\UuidV4;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
|
use Socialbox\Classes\Configuration;
|
||||||
use Socialbox\Classes\Cryptography;
|
use Socialbox\Classes\Cryptography;
|
||||||
use Socialbox\Classes\Database;
|
use Socialbox\Classes\Database;
|
||||||
use Socialbox\Classes\Validator;
|
use Socialbox\Classes\Validator;
|
||||||
|
@ -317,12 +318,16 @@
|
||||||
{
|
{
|
||||||
if ($page < 1)
|
if ($page < 1)
|
||||||
{
|
{
|
||||||
$page = 1;
|
throw new InvalidArgumentException('The page number cannot be less than 1');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($limit < 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 = [];
|
$contacts = [];
|
||||||
|
|
Loading…
Add table
Reference in a new issue