generated from Nodarx/template
67 lines
2.0 KiB
PHP
67 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXM\ProviderImap\Client\Command;
|
|
|
|
use Generator;
|
|
use KTXM\ProviderImap\Client\FetchTarget;
|
|
use KTXM\ProviderImap\Client\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\SequenceSet;
|
|
use KTXM\ProviderImap\Client\SessionContext;
|
|
use KTXM\ProviderImap\Client\SessionState;
|
|
|
|
/**
|
|
* @implements CommandInterface<Generator<int, Message>>
|
|
*/
|
|
final class FetchManyCommand implements CommandInterface
|
|
{
|
|
private readonly FetchTarget $target;
|
|
private readonly FetchOptions $options;
|
|
|
|
public function __construct(FetchTarget|string|SequenceSet|null $target = null, ?FetchOptions $options = null)
|
|
{
|
|
$this->target = match (true) {
|
|
$target instanceof FetchTarget => $target,
|
|
$target instanceof SequenceSet => FetchTarget::sequence($target),
|
|
is_string($target) => FetchTarget::sequence($target),
|
|
default => FetchTarget::all(),
|
|
};
|
|
$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): Generator
|
|
{
|
|
if ($context->selectedMailbox() === null) {
|
|
throw new ImapException('FETCH requires a selected mailbox.');
|
|
}
|
|
|
|
return (new FetchResponseParser())->parseMany($responses);
|
|
}
|
|
} |