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