Skip to content

Commit d8006ef

Browse files
committed
Use trait to unify folder comparison logic
1 parent 9cc5496 commit d8006ef

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/ComparesFolders.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine;
4+
5+
trait ComparesFolders
6+
{
7+
/**
8+
* Determine if two folders are the same.
9+
*/
10+
protected function isSameFolder(FolderInterface $a, FolderInterface $b): bool
11+
{
12+
return $a->path() === $b->path()
13+
&& $a->mailbox()->config('host') === $b->mailbox()->config('host')
14+
&& $a->mailbox()->config('username') === $b->mailbox()->config('username');
15+
}
16+
}

src/Folder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class Folder implements Arrayable, FolderInterface, JsonSerializable
1616
{
17+
use ComparesFolders;
18+
1719
/**
1820
* Constructor.
1921
*/
@@ -73,9 +75,7 @@ public function name(): string
7375
*/
7476
public function is(FolderInterface $folder): bool
7577
{
76-
return $this->path === $folder->path()
77-
&& $this->mailbox->config('host') === $folder->mailbox()->config('host')
78-
&& $this->mailbox->config('username') === $folder->mailbox()->config('username');
78+
return $this->isSameFolder($this, $folder);
7979
}
8080

8181
/**

src/Testing/FakeFolder.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine\Testing;
44

5+
use DirectoryTree\ImapEngine\ComparesFolders;
56
use DirectoryTree\ImapEngine\Exceptions\Exception;
67
use DirectoryTree\ImapEngine\FolderInterface;
78
use DirectoryTree\ImapEngine\MailboxInterface;
@@ -10,6 +11,8 @@
1011

1112
class FakeFolder implements FolderInterface
1213
{
14+
use ComparesFolders;
15+
1316
/**
1417
* Constructor.
1518
*/
@@ -69,9 +72,7 @@ public function name(): string
6972
*/
7073
public function is(FolderInterface $folder): bool
7174
{
72-
return $this->path === $folder->path()
73-
&& $this->mailbox->config('host') === $folder->mailbox()->config('host')
74-
&& $this->mailbox->config('username') === $folder->mailbox()->config('username');
75+
return $this->isSameFolder($this, $folder);
7576
}
7677

7778
/**
@@ -173,6 +174,8 @@ public function setMailbox(MailboxInterface $mailbox): FakeFolder
173174

174175
/**
175176
* Set the folder's messages.
177+
*
178+
* @param FakeMessage[] $messages
176179
*/
177180
public function setMessages(array $messages): FakeFolder
178181
{
@@ -181,6 +184,24 @@ public function setMessages(array $messages): FakeFolder
181184
return $this;
182185
}
183186

187+
/**
188+
* Get the folder's messages.
189+
*
190+
* @return FakeMessage[]
191+
*/
192+
public function getMessages(): array
193+
{
194+
return $this->messages;
195+
}
196+
197+
/**
198+
* Add a message to the folder.
199+
*/
200+
public function addMessage(FakeMessage $message): void
201+
{
202+
$this->messages[] = $message;
203+
}
204+
184205
/**
185206
* Set the folder's delimiter.
186207
*/

0 commit comments

Comments
 (0)