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

Implement bulk-move #306

Merged
merged 8 commits into from
Feb 28, 2018
Merged

Conversation

particleflux
Copy link
Contributor

Implement a new bulk move feature to allow moving messages between mailboxes at once.

This is able to work with an array of message ids or a MessageIterator. For compatibility with the other bulk commands, there also is the possibility to directly input a list of comma separated message ids as a string.

Fixes #305

Copy link
Collaborator

@Slamdunk Slamdunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good.
May you implement this also for imap_mail_copy as the code is almost the same?

src/Mailbox.php Outdated
$numbers = \implode(',', $numbers);
}

return \imap_mail_move($this->resource->getStream(), (string) $numbers, $mailbox->getEncodedName(), \CP_UID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to maintain similarity with:

imap/src/Message.php

Lines 260 to 262 in f90d03f

if (!\imap_mail_move($this->resource->getStream(), (string) $this->getNumber(), $mailbox->getEncodedName(), \CP_UID)) {
throw new MessageMoveException(\sprintf('Message "%s" cannot be moved to "%s"', $this->getNumber(), $mailbox->getName()));
}

Please remove any return and throw the Exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually was about to implement it with an exception, but decided against, as the other bulk commands (clearFlag and setFlag) use a boolean return too.

@Slamdunk Slamdunk added this to the 1.3 milestone Feb 19, 2018
Copy link
Collaborator

@Slamdunk Slamdunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, may you also face this:

May you implement this also for imap_mail_copy as the code is almost the same?

please?

@particleflux
Copy link
Contributor Author

Implemented bulk copy too. I didn't want to produce too much duplicate code, so I refactored the preparation of the message ids into a separate method. This has the nice side effect that the flag commands now work with MessageIterators too.

I'm not sure if a private method is the nicest way to have that implemented. Maybe there could be some kind of MessageList class for that purpose. (That would produce overhead and backward incompatible changes for the caller though $mbox->setFlag(\Seen, new MessageList($array_of_ids))

Copy link
Collaborator

@Slamdunk Slamdunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented bulk copy too. I didn't want to produce too much duplicate code, so I refactored the preparation of the message ids into a separate method. This has the nice side effect that the flag commands now work with MessageIterators too.

Nice 👍

I'm not sure if a private method is the nicest way to have that implemented.

The private method is ok.

Great work. some other few things and 🚢

src/Mailbox.php Outdated
/**
* Prepare message ids for the use with bulk functions.
*
* @param array|MessageIteratorInterface|string $messageIds Message numbers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getArrayCopy isn't a method of MessageIteratorInterface but only of MessageIterator: please change all the doc hints to the latter class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or not. This is now breaking the build since the static analyser complains. I could fix this by typehinting the returned variable in the test - but is that such a good idea?

diff --git a/tests/MailboxTest.php b/tests/MailboxTest.php
index 89cd7c5..84a58da 100644
--- a/tests/MailboxTest.php
+++ b/tests/MailboxTest.php
@@ -10,6 +10,7 @@ use Ddeboer\Imap�xception\MessageDoesNotExistException;
 use Ddeboer\Imap�xception\MessageMoveException;
 use Ddeboer\Imap�xception\ReopenMailboxException;
 use Ddeboer\Imap\MailboxInterface;
+use Ddeboer\Imap\MessageIterator;
 
 /**
  * @covers \Ddeboer\Imap�xception\AbstractException
@@ -234,6 +235,7 @@ final class MailboxTest extends AbstractTest
         $this->assertSame(0, $this->mailbox->count());
 
         // move back by iterator
+        /** @var MessageIterator $messages */
         $messages = $anotherMailbox->getMessages();
         $anotherMailbox->move($messages, $this->mailbox);
         $this->getConnection()->expunge();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but is that such a good idea?

The bad thing is getArrayCopy isn't present in any SPL interface, and this complicates everything a lot.
In a future release we could add it to MessageIteratorInterface but at the time being your solution is enough.

src/Mailbox.php Outdated
public function copy($numbers, MailboxInterface $mailbox)
{
if (!\imap_mail_copy($this->resource->getStream(), $this->prepareMessageIds($numbers), $mailbox->getEncodedName(), \CP_UID)) {
throw new MessageMoveException(\sprintf('Messages cannot be copied to "%s"', $mailbox->getName()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MessageCopyException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed too.

`getArrayCopy` is not a method of `MessageIteratorInterface`, but of
`MessageIterator`
@Slamdunk Slamdunk merged commit d892189 into ddeboer:master Feb 28, 2018
@Slamdunk
Copy link
Collaborator

Thanks @particleflux

@Slamdunk
Copy link
Collaborator

Released in 1.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants