Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interfaces for easier testing capabilities #25

Merged
merged 4 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Collections/MessageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
namespace DirectoryTree\ImapEngine\Collections;

use DirectoryTree\ImapEngine\Message;
use DirectoryTree\ImapEngine\MessageInterface;

class MessageCollection extends PaginatedCollection
{
/**
* Find a message by its UID.
*/
public function find(int $uid): ?Message
public function find(int $uid): ?MessageInterface
{
return $this->first(
fn (Message $message) => $message->uid() === $uid
fn (MessageInterface $message) => $message->uid() === $uid
);
}

/**
* Find a message by its UID or throw an exception.
*/
public function findOrFail(int $uid): Message
public function findOrFail(int $uid): MessageInterface
{
return $this->firstOrFail(
fn (Message $message) => $message->uid() === $uid
fn (MessageInterface $message) => $message->uid() === $uid
);
}
}
10 changes: 10 additions & 0 deletions src/FileMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DirectoryTree\ImapEngine;

use BadMethodCallException;

class FileMessage implements MessageInterface
{
use HasParsedMessage;
Expand All @@ -13,6 +15,14 @@ public function __construct(
protected string $contents
) {}

/**
* {@inheritDoc}
*/
public function uid(): int
{
throw new BadMethodCallException('FileMessage does not support a UID');
}

/**
* Get the string representation of the message.
*/
Expand Down
32 changes: 16 additions & 16 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Support\ItemNotFoundException;
use JsonSerializable;

class Folder implements Arrayable, JsonSerializable
class Folder implements Arrayable, FolderInterface, JsonSerializable
{
/**
* The folder's cached capabilities.
Expand Down Expand Up @@ -55,33 +55,33 @@ public function flags(): array
}

/**
* Get the folder delimiter.
* {@inheritDoc}
*/
public function delimiter(): string
{
return $this->delimiter;
}

/**
* Get the folder name.
* {@inheritDoc}
*/
public function name(): string
{
return last(explode($this->delimiter, $this->path));
}

/**
* Determine if the folder is the same as the given folder.
* {@inheritDoc}
*/
public function is(Folder $folder): bool
public function is(FolderInterface $folder): bool
{
return $this->path === $folder->path
&& $this->mailbox->config('host') === $folder->mailbox->config('host')
&& $this->mailbox->config('username') === $folder->mailbox->config('username');
return $this->path === $folder->path()
&& $this->mailbox->config('host') === $folder->mailbox()->config('host')
&& $this->mailbox->config('username') === $folder->mailbox()->config('username');
}

/**
* Begin querying for messages.
* {@inheritDoc}
*/
public function messages(): MessageQuery
{
Expand All @@ -92,7 +92,7 @@ public function messages(): MessageQuery
}

/**
* Begin idling on the current folder.
* {@inheritDoc}
*/
public function idle(callable $callback, ?callable $query = null, int $timeout = 300): void
{
Expand Down Expand Up @@ -134,7 +134,7 @@ function (int $msgn) use ($callback, $fetch) {
}

/**
* Move or rename the current folder.
* {@inheritDoc}
*/
public function move(string $newPath): void
{
Expand All @@ -144,15 +144,15 @@ public function move(string $newPath): void
}

/**
* Select the current folder.
* {@inheritDoc}
*/
public function select(bool $force = false): void
{
$this->mailbox->select($this, $force);
}

/**
* Get the folder's status.
* {@inheritDoc}
*/
public function status(): array
{
Expand All @@ -171,7 +171,7 @@ public function status(): array
}

/**
* Examine the current folder and get detailed status information.
* {@inheritDoc}
*/
public function examine(): array
{
Expand All @@ -181,7 +181,7 @@ public function examine(): array
}

/**
* Expunge the mailbox and return the expunged message sequence numbers.
* {@inheritDoc}
*/
public function expunge(): array
{
Expand All @@ -191,7 +191,7 @@ public function expunge(): array
}

/**
* Delete the current folder.
* {@inheritDoc}
*/
public function delete(): void
{
Expand Down
78 changes: 78 additions & 0 deletions src/FolderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace DirectoryTree\ImapEngine;

interface FolderInterface
{
/**
* Get the folder's mailbox.
*/
public function mailbox(): MailboxInterface;

/**
* Get the folder path.
*/
public function path(): string;

/**
* Get the folder flags.
*
* @return string[]
*/
public function flags(): array;

/**
* Get the folder delimiter.
*/
public function delimiter(): string;

/**
* Get the folder name.
*/
public function name(): string;

/**
* Determine if the current folder is the same as the given.
*/
public function is(FolderInterface $folder): bool;

/**
* Begin querying for messages.
*/
public function messages(): MessageQuery;

/**
* Begin idling on the current folder.
*/
public function idle(callable $callback, ?callable $query = null, int $timeout = 300): void;

/**
* Move or rename the current folder.
*/
public function move(string $newPath): void;

/**
* Select the current folder.
*/
public function select(bool $force = false): void;

/**
* Get the folder's status.
*/
public function status(): array;

/**
* Examine the current folder and get detailed status information.
*/
public function examine(): array;

/**
* Expunge the mailbox and return the expunged message sequence numbers.
*/
public function expunge(): array;

/**
* Delete the current folder.
*/
public function delete(): void;
}
20 changes: 10 additions & 10 deletions src/FolderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DirectoryTree\ImapEngine\Collections\FolderCollection;
use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse;

class FolderRepository
class FolderRepository implements FolderRepositoryInterface
{
/**
* Constructor.
Expand All @@ -15,41 +15,41 @@ public function __construct(
) {}

/**
* Find a folder.
* {@inheritDoc}
*/
public function find(string $folder): ?Folder
public function find(string $folder): ?FolderInterface
{
return $this->get($folder)->first();
}

/**
* Find a folder or throw an exception.
* {@inheritDoc}
*/
public function findOrFail(string $folder): Folder
public function findOrFail(string $folder): FolderInterface
{
return $this->get($folder)->firstOrFail();
}

/**
* Create a new folder.
* {@inheritDoc}
*/
public function create(string $folder): Folder
public function create(string $folder): FolderInterface
{
$this->mailbox->connection()->create($folder);

return $this->find($folder);
}

/**
* Find or create a folder.
* {@inheritDoc}
*/
public function firstOrCreate(string $folder): Folder
public function firstOrCreate(string $folder): FolderInterface
{
return $this->find($folder) ?? $this->create($folder);
}

/**
* Get the mailboxes folders.
* {@inheritDoc}
*/
public function get(?string $match = '*', ?string $reference = ''): FolderCollection
{
Expand Down
33 changes: 33 additions & 0 deletions src/FolderRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DirectoryTree\ImapEngine;

use DirectoryTree\ImapEngine\Collections\FolderCollection;

interface FolderRepositoryInterface
{
/**
* Find a folder.
*/
public function find(string $folder): ?FolderInterface;

/**
* Find a folder or throw an exception.
*/
public function findOrFail(string $folder): FolderInterface;

/**
* Create a new folder.
*/
public function create(string $folder): FolderInterface;

/**
* Find or create a folder.
*/
public function firstOrCreate(string $folder): FolderInterface;

/**
* Get the mailboxes folders.
*/
public function get(?string $match = '*', ?string $reference = ''): FolderCollection;
}
Loading