generated from Nodarx/template
56fc949e83
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXM\ProviderImap\Client\Protocol;
|
|
|
|
use Generator;
|
|
use KTXM\ProviderImap\Client\ImapException;
|
|
use KTXM\ProviderImap\Client\Message;
|
|
use KTXM\ProviderImap\Client\Protocol\Parser\FetchMessageParser;
|
|
use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse;
|
|
use KTXM\ProviderImap\Client\Protocol\Response\UntaggedResponse;
|
|
|
|
/**
|
|
* Reads FETCH messages from a response stream and validates command completion.
|
|
*/
|
|
final class FetchResultReader
|
|
{
|
|
public function readOne(ResponseStream $responses): Message
|
|
{
|
|
$message = null;
|
|
|
|
foreach ($this->readMany($responses) as $summary) {
|
|
if ($message !== null) {
|
|
throw new ImapException('FETCH returned multiple messages for a single-message request.');
|
|
}
|
|
|
|
$message = $summary;
|
|
}
|
|
|
|
if ($message === null) {
|
|
throw new ImapException('FETCH did not return a message summary.');
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
/**
|
|
* Consume the generator fully to reach and validate the tagged completion.
|
|
*
|
|
* @return Generator<int, Message>
|
|
*/
|
|
public function readMany(ResponseStream $responses): Generator
|
|
{
|
|
foreach ($responses as $response) {
|
|
if ($response instanceof UntaggedResponse && FetchMessageParser::isFetchMessage($response->raw())) {
|
|
yield FetchMessageParser::parse($response->raw());
|
|
continue;
|
|
}
|
|
|
|
if ($response instanceof TaggedResponse) {
|
|
if (!$response->isOk()) {
|
|
throw new ImapException('FETCH failed: ' . $response->text());
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
throw new ImapException('FETCH did not receive a tagged completion response.');
|
|
}
|
|
}
|