refactor: unify response-code parsing

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-09-25 11:54:28 -04:00
parent 09d0bbd817
commit 68ca777150
3 changed files with 51 additions and 26 deletions
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace KTXM\ProviderImap\Client\Protocol\Command;
use KTXM\ProviderImap\Client\Protocol\Parser\ResponseCodeParser;
use KTXM\ProviderImap\Client\Protocol\StringEncoder;
use KTXM\ProviderImap\Client\Result\MessageTransferResult;
use KTXM\ProviderImap\Client\Protocol\Command\Argument\MessageTarget;
@@ -22,6 +23,7 @@ use KTXM\ProviderImap\Client\Protocol\SessionState;
*/
final class MessageTransferCommand implements CommandInterface
{
private readonly ResponseCodeParser $responseCodeParser;
private readonly string $operation;
private readonly SequenceSet $sequenceSet;
private readonly IdentifierMode $identifierMode;
@@ -31,6 +33,8 @@ final class MessageTransferCommand implements CommandInterface
MessageTarget|string|SequenceSet|null $target = null,
private readonly string $destinationMailbox = '',
) {
$this->responseCodeParser = new ResponseCodeParser();
$resolvedTarget = match (true) {
$target instanceof MessageTarget => $target,
$target instanceof SequenceSet => MessageTarget::sequence($target),
@@ -184,7 +188,7 @@ final class MessageTransferCommand implements CommandInterface
bool &$tryCreate,
?string &$highestModSeq,
): void {
$responseCode = $this->parseResponseCode($text);
$responseCode = $this->responseCodeParser->parse($text);
if ($responseCode === null) {
return;
}
@@ -216,24 +220,4 @@ final class MessageTransferCommand implements CommandInterface
'destinationUids' => $responseCode['arguments'][2],
];
}
/**
* @return ?array{name:string, arguments:list<string>, text:string}
*/
private function parseResponseCode(string $text): ?array
{
$text = trim($text);
if (preg_match('/^\[([A-Z0-9.-]+)(?:\s+([^\]]+))?\](?:\s*(.*))?$/i', $text, $matches) !== 1) {
return null;
}
$arguments = trim($matches[2] ?? '');
return [
'name' => strtoupper($matches[1]),
'arguments' => $arguments === '' ? [] : (preg_split('/\s+/', $arguments) ?: []),
'text' => trim($matches[3] ?? ''),
];
}
}
}