Skip to content

Commit 48bd031

Browse files
committed
refactor(Channels): Improve readability of MailChannel and NotifyChannel
- Refactored the reduce method in MailChannel to improve readability - Simplified the logic in NotifyChannel for extender check - Removed unnecessary return statements in NotifyChannel
1 parent 14ad648 commit 48bd031

15 files changed

+57
-92
lines changed

src/Channels/MailChannel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function report(string $report): void
2727
$mailerOrPendingMail = collect($this->config->all())
2828
->except(['driver', 'mailer', 'extender', 'pipes'])
2929
->reduce(
30+
/**
31+
* @param array|mixed $parameters
32+
*
33+
* @throws \ReflectionException
34+
*/
3035
static function (object $mailerOrPendingMail, $parameters, string $method): object {
3136
$method = Str::camel($method);
3237

src/Channels/NotifyChannel.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public function __construct(Repository $config)
4040
'client.class' => 'required|string',
4141
'client.http_options' => 'array',
4242
'client.extender' => static function (string $attribute, $value, \Closure $fail): void {
43-
if (\is_string($value)) {
44-
return;
45-
}
46-
47-
if (\is_callable($value)) {
43+
if (\is_string($value) || \is_callable($value)) {
4844
return;
4945
}
5046

@@ -85,11 +81,9 @@ private function createClient(): Client
8581
$client->setHttpOptions($this->config->get('client.http_options'));
8682
}
8783

88-
if ($this->config->has('client.extender')) {
89-
return app()->call($this->config->get('client.extender'), ['client' => $client]);
90-
}
91-
92-
return $client;
84+
return $this->config->has('client.extender')
85+
? app()->call($this->config->get('client.extender'), ['client' => $client])
86+
: $client;
9387
}
9488

9589
/**
@@ -98,7 +92,6 @@ private function createClient(): Client
9892
private function createMessage(string $report): Message
9993
{
10094
$replace = [config('exception-notify.title'), $report];
101-
10295
$options = Arr::except($this->config->get('message'), 'class');
10396

10497
array_walk_recursive($options, static function (&$value) use ($replace): void {

src/CollectorManager.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ static function (Collector $collector) use ($throwable): array {
4141
->all();
4242
}
4343

44-
/**
45-
* @psalm-suppress InvalidReturnType
46-
* @psalm-suppress InvalidReturnStatement
47-
*/
4844
private function mapToReport(string $channel, Collection $collectors): string
4945
{
5046
return (string) (new Pipeline(app()))

src/Collectors/RequestBasicCollector.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ public function __construct(Request $request)
2424
$this->request = $request;
2525
}
2626

27-
/**
28-
* @psalm-suppress InvalidScalarArgument
29-
* @psalm-suppress InvalidArgument
30-
*/
3127
public function collect(): array
3228
{
3329
return [

src/Collectors/RequestCookieCollector.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ public function __construct(Request $request)
2424
$this->request = $request;
2525
}
2626

27-
/**
28-
* @psalm-suppress InvalidReturnType
29-
* @psalm-suppress InvalidReturnStatement
30-
* @psalm-suppress NullableReturnStatement
31-
* @psalm-suppress InvalidNullableReturnType
32-
*/
3327
public function collect(): array
3428
{
3529
return $this->request->cookie();

src/Collectors/RequestHeaderCollector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
class RequestHeaderCollector extends Collector
1919
{
20-
protected Request $request;
20+
private const REJECTED_HEADERS = ['Cookie'];
21+
private Request $request;
2122

2223
public function __construct(Request $request)
2324
{
@@ -29,6 +30,8 @@ public function __construct(Request $request)
2930
*/
3031
public function collect(): array
3132
{
32-
return $this->request->headers();
33+
return collect($this->request->headers())
34+
->reject(static fn (string $value, string $key): bool => \in_array($key, self::REJECTED_HEADERS, true))
35+
->all();
3336
}
3437
}

src/Collectors/RequestQueryCollector.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ public function __construct(Request $request)
2424
$this->request = $request;
2525
}
2626

27-
/**
28-
* @psalm-suppress InvalidReturnType
29-
* @psalm-suppress InvalidReturnStatement
30-
* @psalm-suppress NullableReturnStatement
31-
* @psalm-suppress InvalidNullableReturnType
32-
*/
3327
public function collect(): array
3428
{
3529
return $this->request->query();

src/Collectors/RequestServerCollector.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ public function __construct(Request $request)
2424
$this->request = $request;
2525
}
2626

27-
/**
28-
* @psalm-suppress InvalidReturnType
29-
* @psalm-suppress InvalidReturnStatement
30-
* @psalm-suppress NullableReturnStatement
31-
* @psalm-suppress InvalidNullableReturnType
32-
*/
3327
public function collect(): array
3428
{
3529
return $this->request->server();

src/ExceptionNotifyManager.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function report(\Throwable $throwable, $channels = null): void
8282
if (
8383
!$this->container->runningInConsole()
8484
&& 'sync' === config('exception-notify.job.connection')
85-
&& method_exists($dispatch, 'afterResponse')
8685
) {
8786
$dispatch->afterResponse();
8887
}
@@ -103,13 +102,34 @@ public function shouldReport(\Throwable $throwable): bool
103102
return !$this->shouldntReport($throwable);
104103
}
105104

106-
protected function shouldntReport(\Throwable $throwable): bool
105+
protected function createDriver($driver): Channel
106+
{
107+
if (isset($this->customCreators[$driver])) {
108+
return $this->callCustomCreator($driver);
109+
}
110+
111+
$configRepository = new Repository($this->config->get("exception-notify.channels.$driver", []));
112+
113+
$studlyName = Str::studly($configRepository->get('driver', $driver));
114+
115+
if (method_exists($this, $method = "create{$studlyName}Driver")) {
116+
return $this->{$method}($configRepository);
117+
}
118+
119+
if (class_exists($class = "Guanguans\\LaravelExceptionNotify\\Channels\\{$studlyName}Channel")) {
120+
return new $class($configRepository);
121+
}
122+
123+
throw new InvalidArgumentException("Driver [$driver] not supported.");
124+
}
125+
126+
private function shouldntReport(\Throwable $throwable): bool
107127
{
108128
if (!config('exception-notify.enabled')) {
109129
return true;
110130
}
111131

112-
if (!Str::is(config('exception-notify.env'), (string) $this->container->environment())) {
132+
if (!$this->container->environment(config('exception-notify.env'))) {
113133
return true;
114134
}
115135

@@ -124,7 +144,7 @@ protected function shouldntReport(\Throwable $throwable): bool
124144
);
125145
}
126146

127-
protected function toFingerprint(\Throwable $throwable): string
147+
private function toFingerprint(\Throwable $throwable): string
128148
{
129149
return config('exception-notify.rate_limit.key_prefix').sha1(implode('|', [
130150
$throwable->getFile(),
@@ -137,7 +157,7 @@ protected function toFingerprint(\Throwable $throwable): string
137157
/**
138158
* @see RateLimiter::attempt
139159
*/
140-
protected function attempt(string $key, int $maxAttempts, int $decaySeconds = 60): bool
160+
private function attempt(string $key, int $maxAttempts, int $decaySeconds = 60): bool
141161
{
142162
if (app(RateLimiter::class)->tooManyAttempts($key, $maxAttempts)) {
143163
return false;
@@ -148,27 +168,6 @@ protected function attempt(string $key, int $maxAttempts, int $decaySeconds = 60
148168
});
149169
}
150170

151-
protected function createDriver($driver): Channel
152-
{
153-
if (isset($this->customCreators[$driver])) {
154-
return $this->callCustomCreator($driver);
155-
}
156-
157-
$configRepository = new Repository($this->config->get("exception-notify.channels.$driver", []));
158-
159-
$studlyName = Str::studly($configRepository->get('driver', $driver));
160-
161-
if (method_exists($this, $method = "create{$studlyName}Driver")) {
162-
return $this->{$method}($configRepository);
163-
}
164-
165-
if (class_exists($class = "Guanguans\\LaravelExceptionNotify\\Channels\\{$studlyName}Channel")) {
166-
return new $class($configRepository);
167-
}
168-
169-
throw new InvalidArgumentException("Driver [$driver] not supported.");
170-
}
171-
172171
private function createDumpDriver(): Channel
173172
{
174173
return new class implements Channel {

src/ExceptionNotifyServiceProvider.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@
2121
use Illuminate\Container\Container;
2222
use Illuminate\Contracts\Container\BindingResolutionException;
2323
use Illuminate\Contracts\Debug\ExceptionHandler;
24+
use Illuminate\Contracts\Support\DeferrableProvider;
2425
use Illuminate\Foundation\Exceptions\Handler;
2526
use Illuminate\Http\Request;
2627
use Illuminate\Support\ServiceProvider;
2728
use Illuminate\Support\Str;
2829
use Illuminate\Support\Stringable;
2930

30-
class ExceptionNotifyServiceProvider extends ServiceProvider
31+
class ExceptionNotifyServiceProvider extends ServiceProvider implements DeferrableProvider
3132
{
3233
public array $singletons = [
3334
RequestMacro::class => RequestMacro::class,
3435
StringableMacro::class => StringableMacro::class,
3536
StrMacro::class => StrMacro::class,
3637
];
37-
protected bool $defer = false;
3838

3939
/**
4040
* @throws \ReflectionException
@@ -67,7 +67,7 @@ public function provides(): array
6767
];
6868
}
6969

70-
protected function setupConfig(): self
70+
private function setupConfig(): self
7171
{
7272
/** @noinspection RealpathInStreamContextInspection */
7373
$source = realpath($raw = __DIR__.'/../config/exception-notify.php') ?: $raw;
@@ -85,7 +85,7 @@ protected function setupConfig(): self
8585
* @throws \ReflectionException
8686
* @throws BindingResolutionException
8787
*/
88-
protected function registerMacros(): self
88+
private function registerMacros(): self
8989
{
9090
Request::mixin($this->app->make(RequestMacro::class));
9191
Str::mixin($this->app->make(StrMacro::class));
@@ -94,7 +94,7 @@ protected function registerMacros(): self
9494
return $this;
9595
}
9696

97-
protected function registerExceptionNotifyManager(): self
97+
private function registerExceptionNotifyManager(): self
9898
{
9999
$this->app->singleton(
100100
ExceptionNotifyManager::class,
@@ -106,7 +106,7 @@ protected function registerExceptionNotifyManager(): self
106106
return $this;
107107
}
108108

109-
protected function registerCollectorManager(): self
109+
private function registerCollectorManager(): self
110110
{
111111
$this->app->singleton(
112112
CollectorManager::class,
@@ -128,15 +128,15 @@ protected function registerCollectorManager(): self
128128
return $this;
129129
}
130130

131-
protected function registerTestCommand(): self
131+
private function registerTestCommand(): self
132132
{
133133
$this->app->singleton(TestCommand::class);
134134
$this->alias(TestCommand::class);
135135

136136
return $this;
137137
}
138138

139-
protected function extendExceptionHandler(): self
139+
private function extendExceptionHandler(): self
140140
{
141141
$this->app->extend(ExceptionHandler::class, function (ExceptionHandler $exceptionHandler): ExceptionHandler {
142142
if ($reportUsingCreator = config('exception-notify.report_using_creator')) {
@@ -162,7 +162,7 @@ protected function extendExceptionHandler(): self
162162
return $this;
163163
}
164164

165-
protected function registerCommands(): self
165+
private function registerCommands(): self
166166
{
167167
if ($this->app->runningInConsole()) {
168168
$this->commands([
@@ -176,7 +176,7 @@ protected function registerCommands(): self
176176
/**
177177
* @param class-string $class
178178
*/
179-
protected function alias(string $class): self
179+
private function alias(string $class): self
180180
{
181181
$this->app->alias($class, $this->toAlias($class));
182182

@@ -186,7 +186,7 @@ protected function alias(string $class): self
186186
/**
187187
* @param class-string $class
188188
*/
189-
protected function toAlias(string $class): string
189+
private function toAlias(string $class): string
190190
{
191191
return str($class)
192192
->replaceFirst(__NAMESPACE__, '')

0 commit comments

Comments
 (0)