socialbox-php/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookContactExists.php

51 lines
1.8 KiB
PHP
Raw Normal View History

2025-01-30 00:24:11 -05:00
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException;
2025-01-30 00:24:11 -05:00
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\ContactManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcRequest;
class AddressBookContactExists extends Method
{
/**
* Returns True if the contact exists in the address book, False otherwise.
*
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('peer'))
{
throw new MissingRpcArgumentException('peer');
2025-01-30 00:24:11 -05:00
}
try
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e);
2025-01-30 00:24:11 -05:00
}
try
{
$peer = $request->getPeer();
return $rpcRequest->produceResponse(ContactManager::isContact($peer, $address));
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to check if the contact exists', StandardError::INTERNAL_SERVER_ERROR, $e);
2025-01-30 00:24:11 -05:00
}
}
}