Skip to content

Commit e4bec48

Browse files
authored
Merge pull request #146 from DirectoryTree/bulk-query-operations
Add bulk query operations
2 parents 263450f + 4470447 commit e4bec48

File tree

4 files changed

+500
-0
lines changed

4 files changed

+500
-0
lines changed

src/MessageQuery.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine;
44

5+
use BackedEnum;
56
use DirectoryTree\ImapEngine\Collections\MessageCollection;
67
use DirectoryTree\ImapEngine\Collections\ResponseCollection;
78
use DirectoryTree\ImapEngine\Connection\ConnectionInterface;
@@ -211,6 +212,106 @@ public function destroy(array|int $uids, bool $expunge = false): void
211212
}
212213
}
213214

215+
/**
216+
* {@inheritDoc}
217+
*/
218+
public function flag(BackedEnum|string $flag, string $operation, bool $expunge = false): int
219+
{
220+
$uids = $this->search()->all();
221+
222+
if (empty($uids)) {
223+
return 0;
224+
}
225+
226+
$this->connection()->store(
227+
(array) Str::enums($flag),
228+
$uids,
229+
mode: $operation
230+
);
231+
232+
if ($expunge) {
233+
$this->folder->expunge();
234+
}
235+
236+
return count($uids);
237+
}
238+
239+
/**
240+
* {@inheritDoc}
241+
*/
242+
public function markRead(): int
243+
{
244+
return $this->flag(ImapFlag::Seen, '+');
245+
}
246+
247+
/**
248+
* {@inheritDoc}
249+
*/
250+
public function markUnread(): int
251+
{
252+
return $this->flag(ImapFlag::Seen, '-');
253+
}
254+
255+
/**
256+
* {@inheritDoc}
257+
*/
258+
public function markFlagged(): int
259+
{
260+
return $this->flag(ImapFlag::Flagged, '+');
261+
}
262+
263+
/**
264+
* {@inheritDoc}
265+
*/
266+
public function unmarkFlagged(): int
267+
{
268+
return $this->flag(ImapFlag::Flagged, '-');
269+
}
270+
271+
/**
272+
* {@inheritDoc}
273+
*/
274+
public function delete(bool $expunge = false): int
275+
{
276+
return $this->flag(ImapFlag::Deleted, '+', $expunge);
277+
}
278+
279+
/**
280+
* {@inheritDoc}
281+
*/
282+
public function move(string $folder, bool $expunge = false): int
283+
{
284+
$uids = $this->search()->all();
285+
286+
if (empty($uids)) {
287+
return 0;
288+
}
289+
290+
$this->connection()->move($folder, $uids);
291+
292+
if ($expunge) {
293+
$this->folder->expunge();
294+
}
295+
296+
return count($uids);
297+
}
298+
299+
/**
300+
* {@inheritDoc}
301+
*/
302+
public function copy(string $folder): int
303+
{
304+
$uids = $this->search()->all();
305+
306+
if (empty($uids)) {
307+
return 0;
308+
}
309+
310+
$this->connection()->copy($folder, $uids);
311+
312+
return count($uids);
313+
}
314+
214315
/**
215316
* Process the collection of messages.
216317
*/

src/MessageQueryInterface.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine;
44

5+
use BackedEnum;
56
use DirectoryTree\ImapEngine\Collections\MessageCollection;
67
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier;
78
use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator;
@@ -205,4 +206,61 @@ public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentif
205206
* Destroy the given messages.
206207
*/
207208
public function destroy(array|int $uids, bool $expunge = false): void;
209+
210+
/**
211+
* Add or remove a flag from all messages matching the current query.
212+
*
213+
* @param string $operation '+'|'-'
214+
* @return int The number of messages affected.
215+
*/
216+
public function flag(BackedEnum|string $flag, string $operation, bool $expunge = false): int;
217+
218+
/**
219+
* Mark all messages matching the current query as read.
220+
*
221+
* @return int The number of messages affected.
222+
*/
223+
public function markRead(): int;
224+
225+
/**
226+
* Mark all messages matching the current query as unread.
227+
*
228+
* @return int The number of messages affected.
229+
*/
230+
public function markUnread(): int;
231+
232+
/**
233+
* Mark all messages matching the current query as flagged.
234+
*
235+
* @return int The number of messages affected.
236+
*/
237+
public function markFlagged(): int;
238+
239+
/**
240+
* Unmark all messages matching the current query as flagged.
241+
*
242+
* @return int The number of messages affected.
243+
*/
244+
public function unmarkFlagged(): int;
245+
246+
/**
247+
* Delete all messages matching the current query.
248+
*
249+
* @return int The number of messages affected.
250+
*/
251+
public function delete(bool $expunge = false): int;
252+
253+
/**
254+
* Move all messages matching the current query to the given folder.
255+
*
256+
* @return int The number of messages affected.
257+
*/
258+
public function move(string $folder, bool $expunge = false): int;
259+
260+
/**
261+
* Copy all messages matching the current query to the given folder.
262+
*
263+
* @return int The number of messages affected.
264+
*/
265+
public function copy(string $folder): int;
208266
}

src/Testing/FakeMessageQuery.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine\Testing;
44

5+
use BackedEnum;
56
use DirectoryTree\ImapEngine\Collections\MessageCollection;
67
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
78
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier;
@@ -154,4 +155,72 @@ public function destroy(array|int $uids, bool $expunge = false): void
154155
$messages->values()->all()
155156
);
156157
}
158+
159+
/**
160+
* {@inheritDoc}
161+
*/
162+
public function flag(BackedEnum|string $flag, string $operation, bool $expunge = false): int
163+
{
164+
return count($this->folder->getMessages());
165+
}
166+
167+
/**
168+
* {@inheritDoc}
169+
*/
170+
public function markRead(): int
171+
{
172+
return count($this->folder->getMessages());
173+
}
174+
175+
/**
176+
* {@inheritDoc}
177+
*/
178+
public function markUnread(): int
179+
{
180+
return count($this->folder->getMessages());
181+
}
182+
183+
/**
184+
* {@inheritDoc}
185+
*/
186+
public function markFlagged(): int
187+
{
188+
return count($this->folder->getMessages());
189+
}
190+
191+
/**
192+
* {@inheritDoc}
193+
*/
194+
public function unmarkFlagged(): int
195+
{
196+
return count($this->folder->getMessages());
197+
}
198+
199+
/**
200+
* {@inheritDoc}
201+
*/
202+
public function delete(bool $expunge = false): int
203+
{
204+
$count = count($this->folder->getMessages());
205+
206+
$this->folder->setMessages([]);
207+
208+
return $count;
209+
}
210+
211+
/**
212+
* {@inheritDoc}
213+
*/
214+
public function move(string $folder, bool $expunge = false): int
215+
{
216+
return count($this->folder->getMessages());
217+
}
218+
219+
/**
220+
* {@inheritDoc}
221+
*/
222+
public function copy(string $folder): int
223+
{
224+
return count($this->folder->getMessages());
225+
}
157226
}

0 commit comments

Comments
 (0)