From efc1455d26235320694192eba5ebeabf8484a9a4 Mon Sep 17 00:00:00 2001 From: Ruud Date: Tue, 15 Oct 2024 12:03:38 +0200 Subject: [PATCH] Add `subscribeMailbox` to `Connection` (#581) --- src/Connection.php | 8 ++++++++ src/ConnectionInterface.php | 8 ++++++++ src/Exception/SubscribeMailboxException.php | 9 +++++++++ tests/ConnectionTest.php | 15 +++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/Exception/SubscribeMailboxException.php diff --git a/src/Connection.php b/src/Connection.php index 538080e9..7260f6f2 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -10,6 +10,7 @@ use Ddeboer\Imap\Exception\ImapNumMsgException; use Ddeboer\Imap\Exception\ImapQuotaException; use Ddeboer\Imap\Exception\MailboxDoesNotExistException; +use Ddeboer\Imap\Exception\SubscribeMailboxException; /** * A connection to an IMAP server that is authenticated for a user. @@ -177,4 +178,11 @@ private function initMailboxNames(): void $this->mailboxNames[$name] = $mailboxInfo; } } + + public function subscribeMailbox(string $name): void + { + if (false === \imap_subscribe($this->resource->getStream(), $this->server . \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8'))) { + throw new SubscribeMailboxException(\sprintf('Can not subscribe to "%s" mailbox at "%s"', $name, $this->server)); + } + } } diff --git a/src/ConnectionInterface.php b/src/ConnectionInterface.php index 5d27854c..a2cf11a9 100644 --- a/src/ConnectionInterface.php +++ b/src/ConnectionInterface.php @@ -7,6 +7,7 @@ use Ddeboer\Imap\Exception\CreateMailboxException; use Ddeboer\Imap\Exception\DeleteMailboxException; use Ddeboer\Imap\Exception\MailboxDoesNotExistException; +use Ddeboer\Imap\Exception\SubscribeMailboxException; /** * A connection to an IMAP server that is authenticated for a user. @@ -76,4 +77,11 @@ public function createMailbox(string $name): MailboxInterface; * @throws DeleteMailboxException */ public function deleteMailbox(MailboxInterface $mailbox): void; + + /** + * Subscribe to mailbox. + * + * @throws SubscribeMailboxException + */ + public function subscribeMailbox(string $name): void; } diff --git a/src/Exception/SubscribeMailboxException.php b/src/Exception/SubscribeMailboxException.php new file mode 100644 index 00000000..9cb86475 --- /dev/null +++ b/src/Exception/SubscribeMailboxException.php @@ -0,0 +1,9 @@ +close(); } + + public function testSubscribeMailbox(): void + { + $connection = $this->getConnection(); + + $name = \uniqid('test_'); + $mailbox = $connection->createMailbox($name); + $connection->subscribeMailbox($name); + + $connection->deleteMailbox($mailbox); + + $this->expectException(SubscribeMailboxException::class); + $connection->subscribeMailbox($name); + } }