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