Files
provider_imap/lib/Client/Protocol/CommandInteraction.php
Sebastian Krupinski 7f562d6aba feat: initial version
Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
2026-02-20 16:41:19 -05:00

70 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Gricob\IMAP\Protocol;
use Generator;
use Gricob\IMAP\Protocol\Command\Command;
use Gricob\IMAP\Protocol\Command\Continuable;
use Gricob\IMAP\Protocol\Response\Line\Line;
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
use Gricob\IMAP\Protocol\Response\Response;
use Gricob\IMAP\Transport\Connection;
use RuntimeException;
final readonly class CommandInteraction implements ContinuationHandler
{
public function __construct(
private Connection $connection,
private ResponseHandler $responseHandler,
private string $tag,
private Command $command,
) {
}
public function interact(): Response
{
$request = sprintf(
"%s %s\r\n",
$this->tag,
$this->command,
);
$this->connection->send($request);
$streamResponse = $this->connection->receive();
return $this->responseHandler->handle($this->tag, $streamResponse, $this);
}
/**
* Like interact() but yields each untagged Line immediately as it arrives.
* The terminal Status is the generator's return value.
*
* @return Generator<int, Line, mixed, Status>
*/
public function streamInteract(): Generator
{
$request = sprintf(
"%s %s\r\n",
$this->tag,
$this->command,
);
$this->connection->send($request);
$streamResponse = $this->connection->receive();
yield from $this->responseHandler->stream($this->tag, $streamResponse, $this);
}
public function continue(): void
{
if (!$this->command instanceof Continuable) {
throw new RuntimeException(
sprintf('Command %s does not support continuable interaction', $this->command->command())
);
}
$this->connection->send($this->command->continue()."\r\n");
}
}