Skip to content

Commit f7052aa

Browse files
committed
Fix infinite loop potential and fire event when attempts exceeded
1 parent a7820a0 commit f7052aa

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/Commands/HandleMessageReceived.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class HandleMessageReceived
1212
* Constructor.
1313
*/
1414
public function __construct(
15-
protected WatchMailbox $command
15+
protected WatchMailbox $command,
16+
protected int &$attempts,
17+
protected ?int &$lastReceivedAt = null,
1618
) {}
1719

1820
/**
@@ -24,6 +26,10 @@ public function __invoke(MessageInterface $message): void
2426
"Message received: [{$message->uid()}]"
2527
);
2628

29+
$this->attempts = 0;
30+
31+
$this->lastReceivedAt = time();
32+
2733
Event::dispatch(new MessageReceived($message));
2834
}
2935
}

src/Commands/WatchMailbox.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace DirectoryTree\ImapEngine\Laravel\Commands;
44

55
use DirectoryTree\ImapEngine\FolderInterface;
6+
use DirectoryTree\ImapEngine\Laravel\Events\MailboxWatchAttemptsExceeded;
67
use DirectoryTree\ImapEngine\Laravel\Facades\Imap;
78
use DirectoryTree\ImapEngine\Laravel\Support\LoopInterface;
89
use DirectoryTree\ImapEngine\MailboxInterface;
910
use DirectoryTree\ImapEngine\Message;
1011
use Exception;
1112
use Illuminate\Console\Command;
13+
use Illuminate\Support\Facades\Event;
1214
use Illuminate\Support\Str;
1315

1416
class WatchMailbox extends Command
@@ -40,14 +42,14 @@ public function handle(LoopInterface $loop): void
4042

4143
$attempts = 0;
4244

43-
$loop->run(function () use ($mailbox, $with, &$attempts) {
45+
$lastReceivedAt = null;
46+
47+
$loop->run(function () use ($mailbox, $name, $with, &$attempts, &$lastReceivedAt) {
4448
try {
4549
$folder = $this->folder($mailbox);
4650

47-
$attempts = 0;
48-
4951
$folder->idle(
50-
new HandleMessageReceived($this),
52+
new HandleMessageReceived($this, $attempts, $lastReceivedAt),
5153
new ConfigureIdleQuery($with),
5254
$this->option('timeout')
5355
);
@@ -65,6 +67,8 @@ public function handle(LoopInterface $loop): void
6567
if ($attempts >= $this->option('attempts')) {
6668
$this->info("Exception: {$e->getMessage()}");
6769

70+
Event::dispatch(new MailboxWatchAttemptsExceeded($name, $e, $lastReceivedAt));
71+
6872
throw $e;
6973
}
7074

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine\Laravel\Events;
4+
5+
use Carbon\Carbon;
6+
use Exception;
7+
8+
class MailboxWatchAttemptsExceeded
9+
{
10+
/**
11+
* Constructor.
12+
*/
13+
public function __construct(
14+
public string $mailbox,
15+
public Exception $exception,
16+
public ?Carbon $lastReceivedAt = null,
17+
) {}
18+
}

0 commit comments

Comments
 (0)