generated from Nodarx/template
70 lines
1.9 KiB
PHP
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");
|
|
}
|
|
} |