Skip to content

Commit f87faf0

Browse files
authored
Merge pull request #25 from DirectoryTree/interfaces
Add interfaces for easier testing capabilities
2 parents 6865679 + 453d728 commit f87faf0

File tree

11 files changed

+246
-56
lines changed

11 files changed

+246
-56
lines changed

src/Collections/MessageCollection.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
namespace DirectoryTree\ImapEngine\Collections;
44

55
use DirectoryTree\ImapEngine\Message;
6+
use DirectoryTree\ImapEngine\MessageInterface;
67

78
class MessageCollection extends PaginatedCollection
89
{
910
/**
1011
* Find a message by its UID.
1112
*/
12-
public function find(int $uid): ?Message
13+
public function find(int $uid): ?MessageInterface
1314
{
1415
return $this->first(
15-
fn (Message $message) => $message->uid() === $uid
16+
fn (MessageInterface $message) => $message->uid() === $uid
1617
);
1718
}
1819

1920
/**
2021
* Find a message by its UID or throw an exception.
2122
*/
22-
public function findOrFail(int $uid): Message
23+
public function findOrFail(int $uid): MessageInterface
2324
{
2425
return $this->firstOrFail(
25-
fn (Message $message) => $message->uid() === $uid
26+
fn (MessageInterface $message) => $message->uid() === $uid
2627
);
2728
}
2829
}

src/FileMessage.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace DirectoryTree\ImapEngine;
44

5+
use BadMethodCallException;
6+
57
class FileMessage implements MessageInterface
68
{
79
use HasParsedMessage;
@@ -13,6 +15,14 @@ public function __construct(
1315
protected string $contents
1416
) {}
1517

18+
/**
19+
* {@inheritDoc}
20+
*/
21+
public function uid(): int
22+
{
23+
throw new BadMethodCallException('FileMessage does not support a UID');
24+
}
25+
1626
/**
1727
* Get the string representation of the message.
1828
*/

src/Folder.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Illuminate\Support\ItemNotFoundException;
1212
use JsonSerializable;
1313

14-
class Folder implements Arrayable, JsonSerializable
14+
class Folder implements Arrayable, FolderInterface, JsonSerializable
1515
{
1616
/**
1717
* The folder's cached capabilities.
@@ -55,33 +55,33 @@ public function flags(): array
5555
}
5656

5757
/**
58-
* Get the folder delimiter.
58+
* {@inheritDoc}
5959
*/
6060
public function delimiter(): string
6161
{
6262
return $this->delimiter;
6363
}
6464

6565
/**
66-
* Get the folder name.
66+
* {@inheritDoc}
6767
*/
6868
public function name(): string
6969
{
7070
return last(explode($this->delimiter, $this->path));
7171
}
7272

7373
/**
74-
* Determine if the folder is the same as the given folder.
74+
* {@inheritDoc}
7575
*/
76-
public function is(Folder $folder): bool
76+
public function is(FolderInterface $folder): bool
7777
{
78-
return $this->path === $folder->path
79-
&& $this->mailbox->config('host') === $folder->mailbox->config('host')
80-
&& $this->mailbox->config('username') === $folder->mailbox->config('username');
78+
return $this->path === $folder->path()
79+
&& $this->mailbox->config('host') === $folder->mailbox()->config('host')
80+
&& $this->mailbox->config('username') === $folder->mailbox()->config('username');
8181
}
8282

8383
/**
84-
* Begin querying for messages.
84+
* {@inheritDoc}
8585
*/
8686
public function messages(): MessageQuery
8787
{
@@ -92,7 +92,7 @@ public function messages(): MessageQuery
9292
}
9393

9494
/**
95-
* Begin idling on the current folder.
95+
* {@inheritDoc}
9696
*/
9797
public function idle(callable $callback, ?callable $query = null, int $timeout = 300): void
9898
{
@@ -134,7 +134,7 @@ function (int $msgn) use ($callback, $fetch) {
134134
}
135135

136136
/**
137-
* Move or rename the current folder.
137+
* {@inheritDoc}
138138
*/
139139
public function move(string $newPath): void
140140
{
@@ -144,15 +144,15 @@ public function move(string $newPath): void
144144
}
145145

146146
/**
147-
* Select the current folder.
147+
* {@inheritDoc}
148148
*/
149149
public function select(bool $force = false): void
150150
{
151151
$this->mailbox->select($this, $force);
152152
}
153153

154154
/**
155-
* Get the folder's status.
155+
* {@inheritDoc}
156156
*/
157157
public function status(): array
158158
{
@@ -171,7 +171,7 @@ public function status(): array
171171
}
172172

173173
/**
174-
* Examine the current folder and get detailed status information.
174+
* {@inheritDoc}
175175
*/
176176
public function examine(): array
177177
{
@@ -181,7 +181,7 @@ public function examine(): array
181181
}
182182

183183
/**
184-
* Expunge the mailbox and return the expunged message sequence numbers.
184+
* {@inheritDoc}
185185
*/
186186
public function expunge(): array
187187
{
@@ -191,7 +191,7 @@ public function expunge(): array
191191
}
192192

193193
/**
194-
* Delete the current folder.
194+
* {@inheritDoc}
195195
*/
196196
public function delete(): void
197197
{

src/FolderInterface.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine;
4+
5+
interface FolderInterface
6+
{
7+
/**
8+
* Get the folder's mailbox.
9+
*/
10+
public function mailbox(): MailboxInterface;
11+
12+
/**
13+
* Get the folder path.
14+
*/
15+
public function path(): string;
16+
17+
/**
18+
* Get the folder flags.
19+
*
20+
* @return string[]
21+
*/
22+
public function flags(): array;
23+
24+
/**
25+
* Get the folder delimiter.
26+
*/
27+
public function delimiter(): string;
28+
29+
/**
30+
* Get the folder name.
31+
*/
32+
public function name(): string;
33+
34+
/**
35+
* Determine if the current folder is the same as the given.
36+
*/
37+
public function is(FolderInterface $folder): bool;
38+
39+
/**
40+
* Begin querying for messages.
41+
*/
42+
public function messages(): MessageQuery;
43+
44+
/**
45+
* Begin idling on the current folder.
46+
*/
47+
public function idle(callable $callback, ?callable $query = null, int $timeout = 300): void;
48+
49+
/**
50+
* Move or rename the current folder.
51+
*/
52+
public function move(string $newPath): void;
53+
54+
/**
55+
* Select the current folder.
56+
*/
57+
public function select(bool $force = false): void;
58+
59+
/**
60+
* Get the folder's status.
61+
*/
62+
public function status(): array;
63+
64+
/**
65+
* Examine the current folder and get detailed status information.
66+
*/
67+
public function examine(): array;
68+
69+
/**
70+
* Expunge the mailbox and return the expunged message sequence numbers.
71+
*/
72+
public function expunge(): array;
73+
74+
/**
75+
* Delete the current folder.
76+
*/
77+
public function delete(): void;
78+
}

src/FolderRepository.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DirectoryTree\ImapEngine\Collections\FolderCollection;
66
use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse;
77

8-
class FolderRepository
8+
class FolderRepository implements FolderRepositoryInterface
99
{
1010
/**
1111
* Constructor.
@@ -15,41 +15,41 @@ public function __construct(
1515
) {}
1616

1717
/**
18-
* Find a folder.
18+
* {@inheritDoc}
1919
*/
20-
public function find(string $folder): ?Folder
20+
public function find(string $folder): ?FolderInterface
2121
{
2222
return $this->get($folder)->first();
2323
}
2424

2525
/**
26-
* Find a folder or throw an exception.
26+
* {@inheritDoc}
2727
*/
28-
public function findOrFail(string $folder): Folder
28+
public function findOrFail(string $folder): FolderInterface
2929
{
3030
return $this->get($folder)->firstOrFail();
3131
}
3232

3333
/**
34-
* Create a new folder.
34+
* {@inheritDoc}
3535
*/
36-
public function create(string $folder): Folder
36+
public function create(string $folder): FolderInterface
3737
{
3838
$this->mailbox->connection()->create($folder);
3939

4040
return $this->find($folder);
4141
}
4242

4343
/**
44-
* Find or create a folder.
44+
* {@inheritDoc}
4545
*/
46-
public function firstOrCreate(string $folder): Folder
46+
public function firstOrCreate(string $folder): FolderInterface
4747
{
4848
return $this->find($folder) ?? $this->create($folder);
4949
}
5050

5151
/**
52-
* Get the mailboxes folders.
52+
* {@inheritDoc}
5353
*/
5454
public function get(?string $match = '*', ?string $reference = ''): FolderCollection
5555
{

src/FolderRepositoryInterface.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine;
4+
5+
use DirectoryTree\ImapEngine\Collections\FolderCollection;
6+
7+
interface FolderRepositoryInterface
8+
{
9+
/**
10+
* Find a folder.
11+
*/
12+
public function find(string $folder): ?FolderInterface;
13+
14+
/**
15+
* Find a folder or throw an exception.
16+
*/
17+
public function findOrFail(string $folder): FolderInterface;
18+
19+
/**
20+
* Create a new folder.
21+
*/
22+
public function create(string $folder): FolderInterface;
23+
24+
/**
25+
* Find or create a folder.
26+
*/
27+
public function firstOrCreate(string $folder): FolderInterface;
28+
29+
/**
30+
* Get the mailboxes folders.
31+
*/
32+
public function get(?string $match = '*', ?string $reference = ''): FolderCollection;
33+
}

0 commit comments

Comments
 (0)