Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap; |
| 6 | |
| 7 | use Ddeboer\Imap\Exception\InvalidResourceException; |
| 8 | use Ddeboer\Imap\Exception\ReopenMailboxException; |
| 9 | |
| 10 | /** |
| 11 | * An imap resource stream. |
| 12 | */ |
| 13 | final class ImapResource implements ImapResourceInterface |
| 14 | { |
| 15 | /** |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 16 | * @var resource |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 17 | */ |
| 18 | private $resource; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 19 | private ?MailboxInterface $mailbox = null; |
| 20 | private static ?string $lastMailboxUsedCache = null; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param resource $resource |
| 26 | */ |
| 27 | public function __construct($resource, MailboxInterface $mailbox = null) |
| 28 | { |
| 29 | $this->resource = $resource; |
| 30 | $this->mailbox = $mailbox; |
| 31 | } |
| 32 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 33 | public function getStream() |
| 34 | { |
| 35 | if (false === \is_resource($this->resource) || 'imap' !== \get_resource_type($this->resource)) { |
| 36 | throw new InvalidResourceException('Supplied resource is not a valid imap resource'); |
| 37 | } |
| 38 | |
| 39 | $this->initMailbox(); |
| 40 | |
| 41 | return $this->resource; |
| 42 | } |
| 43 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 44 | public function clearLastMailboxUsedCache(): void |
| 45 | { |
| 46 | self::$lastMailboxUsedCache = null; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * If connection is not currently in this mailbox, switch it to this mailbox. |
| 51 | */ |
| 52 | private function initMailbox(): void |
| 53 | { |
| 54 | if (null === $this->mailbox || self::isMailboxOpen($this->mailbox, $this->resource)) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | \imap_reopen($this->resource, $this->mailbox->getFullEncodedName()); |
| 59 | |
| 60 | if (self::isMailboxOpen($this->mailbox, $this->resource)) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | throw new ReopenMailboxException(\sprintf('Cannot reopen mailbox "%s"', $this->mailbox->getName())); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check whether the current mailbox is open. |
| 69 | * |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 70 | * @param resource $resource |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 71 | */ |
| 72 | private static function isMailboxOpen(MailboxInterface $mailbox, $resource): bool |
| 73 | { |
| 74 | $currentMailboxName = $mailbox->getFullEncodedName(); |
| 75 | if ($currentMailboxName === self::$lastMailboxUsedCache) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | self::$lastMailboxUsedCache = null; |
| 80 | $check = \imap_check($resource); |
| 81 | $return = false !== $check && $check->Mailbox === $currentMailboxName; |
| 82 | |
| 83 | if (true === $return) { |
| 84 | self::$lastMailboxUsedCache = $currentMailboxName; |
| 85 | } |
| 86 | |
| 87 | return $return; |
| 88 | } |
| 89 | } |