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

Adding an undelete() message method #386

Merged
merged 2 commits into from
Feb 7, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Exception/MessageUndeleteException.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ddeboer\Imap\Exception;

final class MessageUndeleteException extends AbstractException
{
}
15 changes: 15 additions & 0 deletions src/Message.php
Expand Up @@ -10,6 +10,7 @@
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\Exception\MessageMoveException;
use Ddeboer\Imap\Exception\MessageStructureException;
use Ddeboer\Imap\Exception\MessageUndeleteException;

/**
* An IMAP message (e-mail).
Expand Down Expand Up @@ -314,6 +315,20 @@ public function delete(): void
}
}

/**
* Undelete message.
*
* @throws MessageUndeleteException
*/
public function undelete(): void
{
// 'deleted' header changed, force to reload headers, would be better to set deleted flag to false on header
$this->clearHeaders();
if (!\imap_undelete($this->resource->getStream(), $this->getNumber(), \FT_UID)) {
throw new MessageUndeleteException(\sprintf('Message "%s" cannot be undeleted', $this->getNumber()));
}
}

/**
* Set Flag Message.
*
Expand Down
5 changes: 5 additions & 0 deletions src/MessageInterface.php
Expand Up @@ -100,6 +100,11 @@ public function move(MailboxInterface $mailbox): void;
*/
public function delete(): void;

/**
* Undelete message.
*/
public function undelete(): void;

/**
* Set Flag Message.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/MessageTest.php
Expand Up @@ -359,6 +359,24 @@ public function testDelete()
}
}

public function testUndelete()
{
$this->createTestMessage($this->mailbox, 'Message A');
$this->createTestMessage($this->mailbox, 'Message B');
$this->createTestMessage($this->mailbox, 'Message C');

$message = $this->mailbox->getMessage(3);
$message->delete();
$message->undelete();
$this->assertFalse($message->isDeleted());
$this->getConnection()->expunge();

$this->assertCount(3, $this->mailbox);
$this->assertSame('Message A', $this->mailbox->getMessage(1)->getSubject());
$this->assertSame('Message B', $this->mailbox->getMessage(2)->getSubject());
$this->assertSame('Message C', $this->mailbox->getMessage(3)->getSubject());
}

public function testMove()
{
$mailboxOne = $this->createMailbox();
Expand Down