Skip to content

Commit 3b70001

Browse files
committed
Add findOrFail messages method
1 parent c6cd0c9 commit 3b70001

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/MessageQuery.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DirectoryTree\ImapEngine;
44

55
use DirectoryTree\ImapEngine\Collections\MessageCollection;
6+
use DirectoryTree\ImapEngine\Collections\ResponseCollection;
67
use DirectoryTree\ImapEngine\Connection\ConnectionInterface;
78
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
89
use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse;
@@ -525,24 +526,44 @@ public function paginate(int $perPage = 5, $page = null, string $pageName = 'pag
525526
return $this->get()->paginate($perPage, $this->page, $pageName, true);
526527
}
527528

529+
/**
530+
* Find a message by the given identifier type or throw an exception.
531+
*/
532+
public function findOrFail(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentifier::Uid)
533+
{
534+
$uid = $this->uid($id, $identifier)
535+
->firstOrFail() // Untagged response
536+
->tokenAt(3) // ListData
537+
->tokenAt(1) // Atom
538+
->value; // UID
539+
540+
return $this->process(new MessageCollection([$uid]))->firstOrFail();
541+
}
542+
528543
/**
529544
* Find a message by the given identifier type.
530545
*/
531-
public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentifier::Uid): Message
546+
public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentifier::Uid): ?Message
532547
{
533-
// If the sequence is not UID, we'll need to fetch the UID first.
534-
$uid = match ($identifier) {
535-
ImapFetchIdentifier::Uid => $id,
536-
ImapFetchIdentifier::MessageNumber => $this->connection()->uid([$id]) // ResponseCollection
537-
->firstOrFail() // Untagged response
538-
->tokenAt(3) // ListData
539-
->tokenAt(1) // Atom
540-
->value // UID
541-
};
548+
if (! $response = $this->uid($id, $identifier)->first()) {
549+
return null;
550+
}
551+
552+
$uid = $response->tokenAt(3) // ListData
553+
->tokenAt(1) // Atom
554+
->value; // UID
542555

543556
return $this->process(new MessageCollection([$uid]))->first();
544557
}
545558

559+
/**
560+
* Get the UID for theb given identifier.
561+
*/
562+
protected function uid(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentifier::Uid): ResponseCollection
563+
{
564+
return $this->connection()->uid([$id], $identifier);
565+
}
566+
546567
/**
547568
* Get the connection instance.
548569
*/

tests/Integration/MessagesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DirectoryTree\ImapEngine\DraftMessage;
66
use DirectoryTree\ImapEngine\Folder;
77
use DirectoryTree\ImapEngine\Message;
8+
use Illuminate\Support\ItemNotFoundException;
89

910
function folder(): Folder
1011
{
@@ -54,6 +55,38 @@ function folder(): Folder
5455
expect($folder->messages()->first()->uid())->toBe($uid);
5556
});
5657

58+
test('find', function () {
59+
$folder = folder();
60+
61+
$uid = $folder->messages()->append(
62+
new DraftMessage(
63+
64+
text: 'hello world',
65+
),
66+
);
67+
68+
$message = $folder->messages()->find($uid);
69+
70+
expect($message)->toBeInstanceOf(Message::class);
71+
});
72+
73+
test('find or fail', function () {
74+
$folder = folder();
75+
76+
$uid = $folder->messages()->append(
77+
new DraftMessage(
78+
79+
text: 'hello world',
80+
),
81+
);
82+
83+
expect($folder->messages()->findOrFail($uid))->toBeInstanceOf(Message::class);
84+
85+
expect(function () use ($folder) {
86+
$folder->messages()->findOrFail(999);
87+
})->toThrow(ItemNotFoundException::class);
88+
});
89+
5790
test('append', function () {
5891
$folder = folder();
5992

0 commit comments

Comments
 (0)