Skip to content

Commit 00a1269

Browse files
committed
Provide number of attempts in event
1 parent 726446c commit 00a1269

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

src/Commands/HandleMessageReceived.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HandleMessageReceived
1515
*/
1616
public function __construct(
1717
protected WatchMailbox $command,
18-
protected int &$attempts,
18+
protected int &$attempts = 0,
1919
protected ?Carbon &$lastReceivedAt = null,
2020
) {}
2121

src/Commands/WatchMailbox.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public function handle(LoopInterface $loop): void
6767
if ($attempts >= $this->option('attempts')) {
6868
$this->info("Exception: {$e->getMessage()}");
6969

70-
Event::dispatch(new MailboxWatchAttemptsExceeded($name, $e, $lastReceivedAt));
70+
Event::dispatch(
71+
new MailboxWatchAttemptsExceeded($name, $attempts, $e, $lastReceivedAt)
72+
);
7173

7274
throw $e;
7375
}

src/Events/MailboxWatchAttemptsExceeded.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class MailboxWatchAttemptsExceeded
1212
*/
1313
public function __construct(
1414
public string $mailbox,
15+
public int $attempts,
1516
public Exception $exception,
1617
public ?Carbon $lastReceivedAt = null,
1718
) {}

tests/Commands/WatchMailboxTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use RuntimeException;
1818

1919
use function Pest\Laravel\artisan;
20-
use function Pest\Laravel\freezeTime;
2120

2221
it('throws exception when mailbox is not configured', function () {
2322
artisan(WatchMailbox::class, ['mailbox' => 'invalid']);
@@ -53,8 +52,6 @@
5352
});
5453

5554
it('dispatches event when failure attempts have been reached', function () {
56-
$datetime = freezeTime();
57-
5855
Config::set('imap.mailboxes.test', [
5956
'host' => 'localhost',
6057
'port' => 993,
@@ -88,7 +85,8 @@ public function idle(
8885
}
8986

9087
Event::assertDispatched(function (MailboxWatchAttemptsExceeded $event) {
91-
return $event->mailbox === 'test'
88+
return $event->attempts === 5
89+
&& $event->mailbox === 'test'
9290
&& is_null($event->lastReceivedAt)
9391
&& $event->exception->getMessage() === 'Simulated exception';
9492
});

0 commit comments

Comments
 (0)