|
13 | 13 |
|
14 | 14 | namespace Guanguans\LaravelExceptionNotify\Channels; |
15 | 15 |
|
| 16 | +use Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException; |
16 | 17 | use Guanguans\LaravelExceptionNotify\Mail\ReportExceptionMail; |
| 18 | +use Illuminate\Config\Repository; |
17 | 19 | use Illuminate\Mail\Mailer; |
18 | 20 | use Illuminate\Mail\PendingMail; |
19 | 21 | use Illuminate\Support\Facades\Mail; |
20 | 22 | use Illuminate\Support\Str; |
21 | 23 |
|
22 | 24 | class MailChannel extends Channel |
23 | 25 | { |
| 26 | + public function __construct(Repository $config) |
| 27 | + { |
| 28 | + $validator = validator($config->all(), [ |
| 29 | + 'mailer' => 'nullable|string', |
| 30 | + 'to' => 'required|array', |
| 31 | + 'extender' => static function (string $attribute, $value, \Closure $fail): void { |
| 32 | + if (\is_string($value) || \is_callable($value)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $fail("The $attribute must be a callable or string."); |
| 37 | + }, |
| 38 | + ]); |
| 39 | + |
| 40 | + if ($validator->fails()) { |
| 41 | + throw new InvalidArgumentException($validator->errors()->first()); |
| 42 | + } |
| 43 | + |
| 44 | + parent::__construct($config); |
| 45 | + } |
| 46 | + |
24 | 47 | public function report(string $report): void |
25 | 48 | { |
26 | 49 | /** @var Mailer|PendingMail $mailerOrPendingMail */ |
27 | 50 | $mailerOrPendingMail = collect($this->config->all()) |
28 | 51 | ->except(['driver', 'mailer', 'extender', 'pipes']) |
29 | 52 | ->reduce( |
30 | | - /** |
31 | | - * @param array|mixed $parameters |
32 | | - * |
33 | | - * @throws \ReflectionException |
34 | | - */ |
35 | | - static function (object $mailerOrPendingMail, $parameters, string $method): object { |
36 | | - $method = Str::camel($method); |
37 | | - |
38 | | - $object = 1 < (new \ReflectionObject($mailerOrPendingMail))->getMethod($method)->getNumberOfParameters() |
39 | | - ? $mailerOrPendingMail->{$method}(...$parameters) |
40 | | - : $mailerOrPendingMail->{$method}($parameters); |
| 53 | + static function (object $mailerOrPendingMail, array $parameters, string $method): object { |
| 54 | + $object = app()->call([$mailerOrPendingMail, Str::camel($method)], $parameters); |
41 | 55 |
|
42 | 56 | return \is_object($object) ? $object : $mailerOrPendingMail; |
43 | 57 | }, |
|
0 commit comments