Files
provider_imap/lib/Client/Protocol/Command/FetchOneCommand.php
T
Sebastian 9a86292bcf refactor: re-organize files
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-09-25 11:32:25 -04:00

65 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ProviderImap\Client\Protocol\Command;
use KTXM\ProviderImap\Client\Protocol\IdentifierMode;
use KTXM\ProviderImap\Client\Protocol\Parser\FetchResponseParser;
use KTXM\ProviderImap\Client\Protocol\Command\Argument\MessageTarget;
use KTXM\ProviderImap\Client\Protocol\Command\Argument\FetchOptions;
use KTXM\ProviderImap\Client\ImapException;
use KTXM\ProviderImap\Client\Message;
use KTXM\ProviderImap\Client\Protocol\RequestFrame;
use KTXM\ProviderImap\Client\Protocol\ResponseStream;
use KTXM\ProviderImap\Client\Protocol\SessionContext;
use KTXM\ProviderImap\Client\Protocol\SessionState;
/**
* @implements CommandInterface<Message>
*/
final class FetchOneCommand implements CommandInterface
{
private readonly MessageTarget $target;
private readonly FetchOptions $options;
public function __construct(MessageTarget|int|string $target, ?FetchOptions $options = null)
{
$this->target = $target instanceof MessageTarget
? $target
: MessageTarget::sequence($target);
$this->options = $options ?? FetchOptions::default();
}
public function name(): string
{
return 'FETCH';
}
public function allowedStates(): array
{
return [SessionState::Selected];
}
public function encode(string $tag, SessionContext $context): RequestFrame
{
unset($tag, $context);
return new RequestFrame(sprintf(
'%s %s (%s)',
$this->target->identifierMode() === IdentifierMode::Uid ? 'UID FETCH' : 'FETCH',
$this->target->sequenceSet()->toCommand(),
$this->options->toCommand(),
));
}
public function handle(ResponseStream $responses, SessionContext $context): Message
{
if ($context->selectedMailbox() === null) {
throw new ImapException('FETCH requires a selected mailbox.');
}
return (new FetchResponseParser())->parseOne($responses);
}
}