Skip to content

Commit

Permalink
Add subscribeMailbox to Connection (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruud68 authored Oct 15, 2024
1 parent 2b47a91 commit efc1455
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
}
}
8 changes: 8 additions & 0 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions src/Exception/SubscribeMailboxException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ddeboer\Imap\Exception;

final class SubscribeMailboxException extends AbstractException
{
}
15 changes: 15 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ddeboer\Imap\Exception\CreateMailboxException;
use Ddeboer\Imap\Exception\DeleteMailboxException;
use Ddeboer\Imap\Exception\MailboxDoesNotExistException;
use Ddeboer\Imap\Exception\SubscribeMailboxException;
use Ddeboer\Imap\ImapResource;
use Ddeboer\Imap\Mailbox;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -155,4 +156,18 @@ public function testMailboxSelectionAfterReconnect(): void

$connection->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);
}
}

0 comments on commit efc1455

Please sign in to comment.