generated from Nodarx/template
58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXM\ProviderImap\Client\Command;
|
|
|
|
use Generator;
|
|
use KTXM\ProviderImap\Client\ImapException;
|
|
use KTXM\ProviderImap\Client\Message;
|
|
use KTXM\ProviderImap\Client\MessageParser;
|
|
use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse;
|
|
use KTXM\ProviderImap\Client\Protocol\Response\UntaggedResponse;
|
|
use KTXM\ProviderImap\Client\Protocol\ResponseStream;
|
|
|
|
final class FetchResponseParser
|
|
{
|
|
public function parseOne(ResponseStream $responses): Message
|
|
{
|
|
$message = null;
|
|
|
|
foreach ($this->parseMany($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;
|
|
}
|
|
|
|
/**
|
|
* @return Generator<int, Message>
|
|
*/
|
|
public function parseMany(ResponseStream $responses): Generator
|
|
{
|
|
foreach ($responses as $response) {
|
|
if ($response instanceof UntaggedResponse && MessageParser::isFetchMessage($response->payload())) {
|
|
yield MessageParser::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.');
|
|
}
|
|
} |