Skip to content

Commit 7c66484

Browse files
committed
Extract message response parsing into static class
1 parent 8e37aed commit 7c66484

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine\Connection\Responses;
4+
5+
class MessageResponseParser
6+
{
7+
/**
8+
* Get the flags from an untagged response.
9+
*
10+
* @return array<string, string[]>
11+
*/
12+
public static function getFlags(UntaggedResponse $response): array
13+
{
14+
$data = $response->tokenAt(3);
15+
16+
$uid = $data->lookup('UID')->value;
17+
$flags = $data->lookup('FLAGS')->values();
18+
19+
return [$uid => $flags];
20+
}
21+
22+
/**
23+
* Get the body header from an untagged response.
24+
*
25+
* @return array<string, string>
26+
*/
27+
public static function getBodyHeader(UntaggedResponse $response): array
28+
{
29+
$data = $response->tokenAt(3);
30+
31+
$uid = $data->lookup('UID')->value;
32+
$headers = $data->lookup('[HEADER]')->value;
33+
34+
return [$uid => $headers];
35+
}
36+
37+
/**
38+
* Get the body text from an untagged response.
39+
*
40+
* @return array<string, string>
41+
*/
42+
public static function getBodyText(UntaggedResponse $response): array
43+
{
44+
$data = $response->tokenAt(3);
45+
46+
$uid = $data->lookup('UID')->value;
47+
$contents = $data->lookup('[TEXT]')->value;
48+
49+
return [$uid => $contents];
50+
}
51+
}

src/MessageQuery.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DirectoryTree\ImapEngine\Collections\ResponseCollection;
77
use DirectoryTree\ImapEngine\Connection\ConnectionInterface;
88
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
9+
use DirectoryTree\ImapEngine\Connection\Responses\MessageResponseParser;
910
use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse;
1011
use DirectoryTree\ImapEngine\Connection\Tokens\Atom;
1112
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier;
@@ -336,36 +337,15 @@ protected function fetch(Collection $messages): array
336337

337338
$flags = $this->fetchFlags ? $this->connection()
338339
->flags($uids)
339-
->mapWithKeys(function (UntaggedResponse $response) {
340-
$data = $response->tokenAt(3);
341-
342-
$uid = $data->lookup('UID')->value;
343-
$flags = $data->lookup('FLAGS')->values();
344-
345-
return [$uid => $flags];
346-
}) : new Collection;
340+
->mapWithKeys(MessageResponseParser::getFlags(...))->all() : [];
347341

348342
$headers = $this->fetchHeaders ? $this->connection()
349343
->bodyHeader($uids, $this->fetchAsUnread)
350-
->mapWithKeys(function (UntaggedResponse $response) {
351-
$data = $response->tokenAt(3);
352-
353-
$uid = $data->lookup('UID')->value;
354-
$headers = $data->lookup('[HEADER]')->value;
355-
356-
return [$uid => $headers];
357-
}) : new Collection;
344+
->mapWithKeys(MessageResponseParser::getBodyHeader(...))->all() : [];
358345

359346
$contents = $this->fetchBody ? $this->connection()
360347
->bodyText($uids, $this->fetchAsUnread)
361-
->mapWithKeys(function (UntaggedResponse $response) {
362-
$data = $response->tokenAt(3);
363-
364-
$uid = $data->lookup('UID')->value;
365-
$contents = $data->lookup('[TEXT]')->value;
366-
367-
return [$uid => $contents];
368-
}) : new Collection;
348+
->mapWithKeys(MessageResponseParser::getBodyText(...)) : [];
369349

370350
return [
371351
'uids' => $uids,
@@ -380,13 +360,7 @@ protected function fetch(Collection $messages): array
380360
*/
381361
protected function newMessage(int $uid, array $flags, string $headers, string $contents): Message
382362
{
383-
return new Message(
384-
$this->folder,
385-
$uid,
386-
$flags,
387-
$headers,
388-
$contents,
389-
);
363+
return new Message($this->folder, $uid, $flags, $headers, $contents);
390364
}
391365

392366
/**
@@ -446,11 +420,11 @@ public function get(): MessageCollection
446420
*/
447421
public function append(string $message, mixed $flags = null): int
448422
{
449-
$result = $this->connection()->append(
423+
$response = $this->connection()->append(
450424
$this->folder->path(), $message, Str::enums($flags),
451425
);
452426

453-
return $result // TAG4 OK [APPENDUID <uidvalidity> <uid>] APPEND completed.
427+
return $response // TAG4 OK [APPENDUID <uidvalidity> <uid>] APPEND completed.
454428
->tokenAt(2) // [APPENDUID <uidvalidity> <uid>]
455429
->tokenAt(2) // <uid>
456430
->value;

0 commit comments

Comments
 (0)