Skip to content

Commit

Permalink
refactor(command): update TestCommand signature and handle method
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
guanguans committed May 9, 2024
1 parent b07385e commit c6c72ca
Showing 1 changed file with 24 additions and 56 deletions.
80 changes: 24 additions & 56 deletions src/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,45 @@
namespace Guanguans\LaravelExceptionNotify\Commands;

use Guanguans\LaravelExceptionNotify\DefaultNotifyClientExtender;
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;

class TestCommand extends Command
{
protected $signature = 'exception-notify:test';
protected $signature = 'exception-notify:test {--c|channels=* : Specify channels to test}';
protected $description = 'Test for exception-notify';

public function handle(ExceptionNotifyManager $exceptionNotifyManager): void
public function handle(): void
{
collect(config('exception-notify.channels'))->each(static function (array $config, string $name): void {
if ('notify' === $config['driver']) {
config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class);
}
});
if ($channels = $this->option('channels')) {
config()->set('exception-notify.defaults', $channels);
}

collect(config('exception-notify.channels'))
->only($defaults = config('exception-notify.defaults'))
->each(static function (array $config, string $name): void {
if ('notify' === $config['driver']) {
config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class);
}
});

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

try {
$runtimeException = new RuntimeException('Test for exception-notify.');

if ($exceptionNotifyManager->shouldReport($runtimeException)) {
throw $runtimeException;
}

$warning = sprintf(
'The exception [%s] should not be reported. Please check the configuration.',
\get_class($runtimeException)
);
} finally {
$this->output->warning(
$warning ?? <<<'warning'
Please check whether your channels received the exception notification report.
$this->output->warning(sprintf(
<<<'warning'
Let's throw an exception to trigger exception notification monitoring.
Please check whether your channels(%s) received the exception notification reports.
If not, please find reason in the default log.
warning
);
warning,
implode('', $defaults)
));

throw new RuntimeException('Test for exception-notify.');
} finally {
$this->output->note('Test for exception-notify done.');
}
}

private function getMainConfigurations(): array
{
$mainConfigurations = collect(config('exception-notify'))
->except(['collectors', 'title', 'channels'])
->transform(static function ($item) {
if (\is_array($item)) {
if ([] === $item) {
return '[]';
}

if (array_is_list($item)) {
return implode(',', $item);
}

return $item;
}

/** @noinspection DebugFunctionUsageInspection */
return var_export($item, true);
});

return collect(Arr::dot($mainConfigurations))
->transform(static fn (?string $item, string $key): string => sprintf(
"$key: <fg=yellow>%s</>",
$item ?? 'null'
))
->all();
}
}

0 comments on commit c6c72ca

Please sign in to comment.