*/ final class FetchOneCommand implements CommandInterface { private readonly FetchTarget $target; private readonly FetchOptions $options; public function __construct(FetchTarget|int|string $target, ?FetchOptions $options = null) { $this->target = $target instanceof FetchTarget ? $target : FetchTarget::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->toCommand(), $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); } }