Skip to content

Commit abeb40d

Browse files
committed
wip
1 parent e1038bd commit abeb40d

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

config/mailbox.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
'store_incoming_emails_for_days' => 7,
2828

29+
'only_store_matching_emails' => true,
30+
2931
/*
3032
* Some services do not have their own authentication methods to
3133
* verify the incoming request. For these services, you need

src/Routing/Route.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Route
2222
const TO = 'to';
2323
const CC = 'cc';
2424
const SUBJECT = 'subject';
25+
const FALLBACK = 'fallback';
26+
const CATCH_ALL = 'catch-all';
2527

2628
protected $mailbox;
2729

src/Routing/Router.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44

55
use BeyondCode\Mailbox\InboundEmail;
66
use Illuminate\Container\Container;
7+
use Illuminate\Support\Collection;
78

89
class Router
910
{
1011
/** @var RouteCollection */
1112
protected $routes;
1213

14+
/** @var Route */
15+
protected $fallbackRoute;
16+
17+
/** @var Route */
18+
protected $catchAllRoute;
19+
1320
/** @var Container */
1421
protected $container;
1522

@@ -40,6 +47,16 @@ public function subject(string $pattern, $action)
4047
$this->addRoute(Route::SUBJECT, $pattern, $action);
4148
}
4249

50+
public function fallback($action)
51+
{
52+
$this->fallbackRoute = $this->createRoute(Route::FALLBACK, '', $action);
53+
}
54+
55+
public function catchAll($action)
56+
{
57+
$this->catchAllRoute = $this->createRoute(Route::CATCH_ALL, '', $action);
58+
}
59+
4360
protected function addRoute(string $subject, string $pattern, $action)
4461
{
4562
$this->routes->add($this->createRoute($subject, $pattern, $action));
@@ -59,17 +76,42 @@ public function callMailboxes(InboundEmail $email)
5976
$route->run($email);
6077
});
6178

62-
if ($this->shouldStoreInboundEmails() && $matchedRoutes->isNotEmpty()) {
79+
if ($matchedRoutes->isEmpty()) {
80+
$this->callFallback($email);
81+
}
82+
83+
$this->callCatchAll($email);
84+
85+
if ($this->shouldStoreInboundEmails() && $this->shouldStoreAllInboundEmails($matchedRoutes)) {
6386
$this->storeEmail($email);
6487
}
6588
}
6689
}
6790

91+
protected function callFallback(InboundEmail $email)
92+
{
93+
if ($this->fallbackRoute) {
94+
$this->fallbackRoute->run($email);
95+
}
96+
}
97+
98+
protected function callCatchAll(InboundEmail $email)
99+
{
100+
if ($this->catchAllRoute) {
101+
$this->catchAllRoute->run($email);
102+
}
103+
}
104+
68105
protected function shouldStoreInboundEmails(): bool
69106
{
70107
return config('mailbox.store_incoming_emails_for_days') > 0;
71108
}
72109

110+
protected function shouldStoreAllInboundEmails(Collection $matchedRoutes): bool
111+
{
112+
return $matchedRoutes->isNotEmpty() ? true : ! config('mailbox.only_store_matching_emails');
113+
}
114+
73115
protected function storeEmail(InboundEmail $email)
74116
{
75117
$email->save();

tests/InboundEmailTest.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,40 @@ public function it_stores_inbound_emails()
3131
}
3232

3333
/** @test */
34-
public function it_does_not_store_inbound_emails_if_configured()
34+
public function it_stores_all_inbound_emails()
3535
{
36-
$this->app['config']['mailbox.store_incoming_emails_for_days'] = 0;
36+
$this->app['config']['mailbox.only_store_matching_emails'] = false;
3737

38-
Mailbox::from('example@beyondco.de', function($email) {
38+
Mailbox::to('someone@beyondco.de', function($email) {
3939
});
4040

4141
Mail::to('[email protected]')->send(new TestMail);
42+
Mail::to('[email protected]')->send(new TestMail);
43+
44+
$this->assertSame(2, InboundEmail::query()->count());
45+
}
46+
47+
/** @test */
48+
public function it_can_use_fallbacks()
49+
{
50+
Mailbox::fallback(function(InboundEmail $email) {
51+
Mail::fake();
52+
53+
$email->reply(new ReplyMail);
54+
});
55+
4256
Mail::to('[email protected]')->send(new TestMail);
4357

44-
$this->assertSame(0, InboundEmail::query()->count());
58+
Mail::assertSent(ReplyMail::class);
4559
}
4660

4761
/** @test */
48-
public function it_can_reply_to_mails()
62+
public function it_can_use_catchall()
4963
{
64+
Mailbox::to('[email protected]', function($email) {
65+
});
5066

51-
Mailbox::from('[email protected]', function(InboundEmail $email) {
67+
Mailbox::catchAll(function(InboundEmail $email) {
5268
Mail::fake();
5369

5470
$email->reply(new ReplyMail);
@@ -60,14 +76,32 @@ public function it_can_reply_to_mails()
6076
}
6177

6278
/** @test */
63-
public function it_can_forward_mails()
79+
public function it_does_not_store_inbound_emails_if_configured()
80+
{
81+
$this->app['config']['mailbox.store_incoming_emails_for_days'] = 0;
82+
83+
Mailbox::from('[email protected]', function($email) {
84+
});
85+
86+
Mail::to('[email protected]')->send(new TestMail);
87+
Mail::to('[email protected]')->send(new TestMail);
88+
89+
$this->assertSame(0, InboundEmail::query()->count());
90+
}
91+
92+
/** @test */
93+
public function it_can_reply_to_mails()
6494
{
6595

6696
Mailbox::from('[email protected]', function(InboundEmail $email) {
67-
$email->forward('[email protected]');
97+
Mail::fake();
98+
99+
$email->reply(new ReplyMail);
68100
});
69101

70102
Mail::to('[email protected]')->send(new TestMail);
103+
104+
Mail::assertSent(ReplyMail::class);
71105
}
72106

73107
}

0 commit comments

Comments
 (0)