Skip to content

Commit f54cd79

Browse files
committed
refactor(mail): Improve method call in MailChannel
- Refactored method call in MailChannel to improve readability and maintainability
1 parent f379e4d commit f54cd79

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

config/exception-notify.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,23 @@
7373
*/
7474
'collectors' => [
7575
Guanguans\LaravelExceptionNotify\Collectors\ApplicationCollector::class,
76-
Guanguans\LaravelExceptionNotify\Collectors\PhpInfoCollector::class,
7776
Guanguans\LaravelExceptionNotify\Collectors\ChoreCollector::class,
77+
Guanguans\LaravelExceptionNotify\Collectors\PhpInfoCollector::class,
78+
Guanguans\LaravelExceptionNotify\Collectors\RequestBasicCollector::class,
7879
Guanguans\LaravelExceptionNotify\Collectors\ExceptionBasicCollector::class,
7980
Guanguans\LaravelExceptionNotify\Collectors\ExceptionContextCollector::class,
8081
Guanguans\LaravelExceptionNotify\Collectors\ExceptionTraceCollector::class,
81-
Guanguans\LaravelExceptionNotify\Collectors\RequestBasicCollector::class,
82+
83+
// Guanguans\LaravelExceptionNotify\Collectors\RequestCookieCollector::class,
84+
// Guanguans\LaravelExceptionNotify\Collectors\RequestSessionCollector::class,
85+
// Guanguans\LaravelExceptionNotify\Collectors\RequestMiddlewareCollector::class,
86+
// Guanguans\LaravelExceptionNotify\Collectors\RequestServerCollector::class,
87+
8288
// Guanguans\LaravelExceptionNotify\Collectors\RequestHeaderCollector::class,
8389
// Guanguans\LaravelExceptionNotify\Collectors\RequestQueryCollector::class,
8490
// Guanguans\LaravelExceptionNotify\Collectors\RequestPostCollector::class,
85-
// Guanguans\LaravelExceptionNotify\Collectors\RequestRawFileCollector::class,
8691
// Guanguans\LaravelExceptionNotify\Collectors\RequestFileCollector::class,
87-
// \Guanguans\LaravelExceptionNotify\Collectors\RequestMiddlewareCollector::class,
88-
// \Guanguans\LaravelExceptionNotify\Collectors\RequestServerCollector::class,
89-
// \Guanguans\LaravelExceptionNotify\Collectors\RequestCookieCollector::class,
90-
// \Guanguans\LaravelExceptionNotify\Collectors\RequestSessionCollector::class,
92+
// Guanguans\LaravelExceptionNotify\Collectors\RequestRawFileCollector::class,
9193
],
9294

9395
/**
@@ -117,7 +119,7 @@
117119
'mail' => [
118120
'driver' => 'mail',
119121
'mailer' => null,
120-
'to' => env('EXCEPTION_NOTIFY_MAIL_TO', 'your@example.mail'),
122+
'to' => ['users' => env('EXCEPTION_NOTIFY_MAIL_TO', 'your@example.mail')],
121123
'pipes' => [
122124
SprintfHtmlPipe::class,
123125
],

src/Channels/MailChannel.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,45 @@
1313

1414
namespace Guanguans\LaravelExceptionNotify\Channels;
1515

16+
use Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException;
1617
use Guanguans\LaravelExceptionNotify\Mail\ReportExceptionMail;
18+
use Illuminate\Config\Repository;
1719
use Illuminate\Mail\Mailer;
1820
use Illuminate\Mail\PendingMail;
1921
use Illuminate\Support\Facades\Mail;
2022
use Illuminate\Support\Str;
2123

2224
class MailChannel extends Channel
2325
{
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+
2447
public function report(string $report): void
2548
{
2649
/** @var Mailer|PendingMail $mailerOrPendingMail */
2750
$mailerOrPendingMail = collect($this->config->all())
2851
->except(['driver', 'mailer', 'extender', 'pipes'])
2952
->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);
4155

4256
return \is_object($object) ? $object : $mailerOrPendingMail;
4357
},

0 commit comments

Comments
 (0)