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

44 lines
No EOL
1.6 KiB
PHP

<?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;
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');
}
$peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
try
{
$peer = $request->getPeer();
return $rpcRequest->produceResponse(ContactManager::isContact($peer, $peerAddress));
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to check if the contact exists', StandardError::INTERNAL_SERVER_ERROR, $e);
}
}
}