/
Route.php
180 lines (146 loc) · 4.3 KB
/
Route.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace BeyondCode\Mailbox\Routing;
use BeyondCode\Mailbox\Concerns\HandlesParameters;
use BeyondCode\Mailbox\Concerns\HandlesRegularExpressions;
use BeyondCode\Mailbox\InboundEmail;
use Illuminate\Container\Container;
use Illuminate\Routing\RouteDependencyResolverTrait;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionFunction;
use ZBateson\MailMimeParser\Header\Part\AddressPart;
class Route
{
use HandlesParameters;
use HandlesRegularExpressions;
use RouteDependencyResolverTrait;
const FROM = 'from';
const TO = 'to';
const CC = 'cc';
const BCC = 'bcc';
const SUBJECT = 'subject';
const FALLBACK = 'fallback';
const CATCH_ALL = 'catch-all';
protected $mailbox;
protected $subject;
protected $pattern;
protected $action;
protected $container;
protected $matches = [];
protected $wheres = [];
public function __construct(string $subject, string $pattern, $action)
{
$this->subject = $subject;
$this->pattern = $pattern;
$this->action = $action;
}
public function setContainer(Container $container)
{
$this->container = $container;
return $this;
}
public function subject()
{
return $this->subject;
}
public function action()
{
return $this->action;
}
public function pattern()
{
return $this->pattern;
}
public function matches(InboundEmail $message): bool
{
$subjects = $this->gatherMatchSubjectsFromMessage($message);
return Collection::make($subjects)->first(function (string $subject) {
return $this->matchesRegularExpression($subject);
}) !== null;
}
protected function gatherMatchSubjectsFromMessage(InboundEmail $message)
{
switch ($this->subject) {
case self::FROM:
return [$message->from()];
break;
case self::TO:
return $this->convertMessageAddresses($message->to());
break;
case self::CC:
return $this->convertMessageAddresses($message->cc());
break;
case self::BCC:
return $this->convertMessageAddresses($message->bcc());
break;
case self::SUBJECT:
return [$message->subject()];
break;
}
}
/**
* @param $addresses AddressPart[]
* @return array
*/
protected function convertMessageAddresses($addresses): array
{
return Collection::make($addresses)
->map(function (AddressPart $address) {
return $address->getEmail();
})->toArray();
}
public function run(InboundEmail $email)
{
$this->container = $this->container ?: new Container;
if ($this->isMailboxAction()) {
$this->runMailbox($email);
} else {
$this->runCallable($email);
}
}
protected function isMailboxAction()
{
return is_string($this->action);
}
protected function runMailbox(InboundEmail $email)
{
$method = $this->getMailboxMethod();
$parameters = $this->resolveClassMethodDependencies(
[$email] + $this->parametersWithoutNulls(), $this->getMailbox(), $method
);
return $this->getMailbox()->{$method}(...array_values($parameters));
}
protected function runCallable(InboundEmail $email)
{
$callable = $this->action;
return $callable(...array_values($this->resolveMethodDependencies(
[$email] + $this->parametersWithoutNulls(), new ReflectionFunction($this->action)
)));
}
public function getMailbox()
{
if (! $this->mailbox) {
$class = $this->parseMailboxCallback()[0];
$this->mailbox = $this->container->make(ltrim($class, '\\'));
}
return $this->mailbox;
}
/**
* Get the controller method used for the route.
*
* @return string
*/
protected function getMailboxMethod()
{
return $this->parseMailboxCallback()[1] ?? '__invoke';
}
/**
* Parse the controller.
*
* @return array
*/
protected function parseMailboxCallback()
{
return Str::parseCallback($this->action);
}
}