generated from Nodarx/template
refactor: use custom imap client
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
58
lib/Client/Command/FetchResponseParser.php
Normal file
58
lib/Client/Command/FetchResponseParser.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user