1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\Mailbox\Routing; |
4
|
|
|
|
5
|
|
|
use BeyondCode\Mailbox\InboundEmail; |
6
|
|
|
use BeyondCode\Mailbox\MailboxManager; |
7
|
|
|
use Illuminate\Container\Container; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
10
|
|
|
|
11
|
|
|
class Router |
12
|
|
|
{ |
13
|
|
|
use ForwardsCalls; |
14
|
|
|
|
15
|
|
|
/** @var RouteCollection */ |
16
|
|
|
protected $routes; |
17
|
|
|
|
18
|
|
|
/** @var Route */ |
19
|
|
|
protected $fallbackRoute; |
20
|
|
|
|
21
|
|
|
/** @var Route */ |
22
|
|
|
protected $catchAllRoute; |
23
|
|
|
|
24
|
|
|
/** @var Container */ |
25
|
|
|
protected $container; |
26
|
|
|
|
27
|
|
|
public function __construct(Container $container = null) |
28
|
|
|
{ |
29
|
|
|
$this->container = $container ?: new Container; |
30
|
|
|
|
31
|
|
|
$this->routes = new RouteCollection; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function from(string $pattern, $action): Route |
35
|
|
|
{ |
36
|
|
|
return $this->addRoute(Route::FROM, $pattern, $action); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function to(string $pattern, $action): Route |
40
|
|
|
{ |
41
|
|
|
return $this->addRoute(Route::TO, $pattern, $action); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function cc(string $pattern, $action): Route |
45
|
|
|
{ |
46
|
|
|
return $this->addRoute(Route::CC, $pattern, $action); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function bcc(string $pattern, $action): Route |
50
|
|
|
{ |
51
|
|
|
return $this->addRoute(Route::BCC, $pattern, $action); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function subject(string $pattern, $action): Route |
55
|
|
|
{ |
56
|
|
|
return $this->addRoute(Route::SUBJECT, $pattern, $action); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function fallback($action) |
60
|
|
|
{ |
61
|
|
|
$this->fallbackRoute = $this->createRoute(Route::FALLBACK, '', $action); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function catchAll($action) |
65
|
|
|
{ |
66
|
|
|
$this->catchAllRoute = $this->createRoute(Route::CATCH_ALL, '', $action); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function addRoute(string $subject, string $pattern, $action): Route |
70
|
|
|
{ |
71
|
|
|
$route = $this->createRoute($subject, $pattern, $action); |
72
|
|
|
|
73
|
|
|
$this->routes->add($route); |
74
|
|
|
|
75
|
|
|
return $route; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function createRoute(string $subject, string $pattern, $action): Route |
79
|
|
|
{ |
80
|
|
|
return (new Route($subject, $pattern, $action)) |
81
|
|
|
->setContainer($this->container); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function callMailboxes(InboundEmail $email) |
85
|
|
|
{ |
86
|
|
|
if ($email->isValid()) { |
87
|
|
|
$matchedRoutes = $this->routes->match($email)->map(function (Route $route) use ($email) { |
88
|
|
|
$route->run($email); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
if ($matchedRoutes->isEmpty() && $this->fallbackRoute) { |
92
|
|
|
$matchedRoutes[] = $this->fallbackRoute; |
93
|
|
|
$this->fallbackRoute->run($email); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($this->catchAllRoute) { |
97
|
|
|
$matchedRoutes[] = $this->catchAllRoute; |
98
|
|
|
$this->catchAllRoute->run($email); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($this->shouldStoreInboundEmails() && $this->shouldStoreAllInboundEmails($matchedRoutes)) { |
102
|
|
|
$this->storeEmail($email); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function shouldStoreInboundEmails(): bool |
108
|
|
|
{ |
109
|
|
|
return config('mailbox.store_incoming_emails_for_days') > 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function shouldStoreAllInboundEmails(Collection $matchedRoutes): bool |
113
|
|
|
{ |
114
|
|
|
return $matchedRoutes->isNotEmpty() ? true : ! config('mailbox.only_store_matching_emails'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function storeEmail(InboundEmail $email) |
118
|
|
|
{ |
119
|
|
|
$email->save(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function __call($method, $parameters) |
123
|
|
|
{ |
124
|
|
|
return $this->forwardCallTo( |
125
|
|
|
$this->container->make(MailboxManager::class), $method, $parameters |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|