Skip to content

Commit c6c72ca

Browse files
committed
refactor(command): update TestCommand signature and handle method
- Updated TestCommand signature to include optional channels parameter - Updated handle method to set default channels and configure notify client extender - Improved exception handling and logging in handle method
1 parent b07385e commit c6c72ca

File tree

1 file changed

+24
-56
lines changed

1 file changed

+24
-56
lines changed

src/Commands/TestCommand.php

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,45 @@
1414
namespace Guanguans\LaravelExceptionNotify\Commands;
1515

1616
use Guanguans\LaravelExceptionNotify\DefaultNotifyClientExtender;
17-
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
1817
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
1918
use Illuminate\Console\Command;
20-
use Illuminate\Support\Arr;
2119

2220
class TestCommand extends Command
2321
{
24-
protected $signature = 'exception-notify:test';
22+
protected $signature = 'exception-notify:test {--c|channels=* : Specify channels to test}';
2523
protected $description = 'Test for exception-notify';
2624

27-
public function handle(ExceptionNotifyManager $exceptionNotifyManager): void
25+
public function handle(): void
2826
{
29-
collect(config('exception-notify.channels'))->each(static function (array $config, string $name): void {
30-
if ('notify' === $config['driver']) {
31-
config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class);
32-
}
33-
});
27+
if ($channels = $this->option('channels')) {
28+
config()->set('exception-notify.defaults', $channels);
29+
}
30+
31+
collect(config('exception-notify.channels'))
32+
->only($defaults = config('exception-notify.defaults'))
33+
->each(static function (array $config, string $name): void {
34+
if ('notify' === $config['driver']) {
35+
config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class);
36+
}
37+
});
3438

3539
$this->output->note('Test for exception-notify start.');
36-
$this->output->section('The main configuration is as follows.');
37-
$this->output->listing($this->getMainConfigurations());
40+
$this->output->section('Current default channels:');
41+
$this->output->listing($defaults);
3842

3943
try {
40-
$runtimeException = new RuntimeException('Test for exception-notify.');
41-
42-
if ($exceptionNotifyManager->shouldReport($runtimeException)) {
43-
throw $runtimeException;
44-
}
45-
46-
$warning = sprintf(
47-
'The exception [%s] should not be reported. Please check the configuration.',
48-
\get_class($runtimeException)
49-
);
50-
} finally {
51-
$this->output->warning(
52-
$warning ?? <<<'warning'
53-
Please check whether your channels received the exception notification report.
44+
$this->output->warning(sprintf(
45+
<<<'warning'
46+
Let's throw an exception to trigger exception notification monitoring.
47+
Please check whether your channels(%s) received the exception notification reports.
5448
If not, please find reason in the default log.
55-
warning
56-
);
49+
warning,
50+
implode('', $defaults)
51+
));
5752

53+
throw new RuntimeException('Test for exception-notify.');
54+
} finally {
5855
$this->output->note('Test for exception-notify done.');
5956
}
6057
}
61-
62-
private function getMainConfigurations(): array
63-
{
64-
$mainConfigurations = collect(config('exception-notify'))
65-
->except(['collectors', 'title', 'channels'])
66-
->transform(static function ($item) {
67-
if (\is_array($item)) {
68-
if ([] === $item) {
69-
return '[]';
70-
}
71-
72-
if (array_is_list($item)) {
73-
return implode(',', $item);
74-
}
75-
76-
return $item;
77-
}
78-
79-
/** @noinspection DebugFunctionUsageInspection */
80-
return var_export($item, true);
81-
});
82-
83-
return collect(Arr::dot($mainConfigurations))
84-
->transform(static fn (?string $item, string $key): string => sprintf(
85-
"$key: <fg=yellow>%s</>",
86-
$item ?? 'null'
87-
))
88-
->all();
89-
}
9058
}

0 commit comments

Comments
 (0)