generated from Nodarx/template
feat: initial version
Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
This commit was merged in pull request #1.
This commit is contained in:
70
lib/Client/Protocol/CommandInteraction.php
Normal file
70
lib/Client/Protocol/CommandInteraction.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user