Skip to content

Commit 6f08361

Browse files
committed
refactor(commands): simplify source option selection in MusicCommand handle method
- Move interactive source selection logic from interact and afterPromptingForMissingArguments methods into the handle method - Use unless condition to prompt user for sources only if not provided as an option - Remove redundant method overrides to streamline command execution flow - Update Music facade phpdoc to reflect 'options' parameter name instead of 'sources' in search method This change improves code clarity by consolidating source option handling into a single place and reduces complexity of the command class. Signed-off-by: guanguans <ityaozm@gmail.com>
1 parent abf7f0e commit 6f08361

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

app/Commands/MusicCommand.php

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,13 @@ final class MusicCommand extends Command implements Isolatable, PromptsForMissin
7171
public function handle(): void
7272
{
7373
collect()
74+
->unless($this->option('sources'), fn () => $this->input->setOption('sources', (array) select(
75+
__('select_source_label'),
76+
array_combine($sources = config('app.sources'), array_map(ucfirst(...), $sources))
77+
)))
7478
->when(windows_os(), static fn () => warning(__('windows_hint')))
75-
->tap(static function () use (&$stdinKeyword): void {
76-
/** @noinspection OffsetOperationsInspection */
77-
if (($fstat = fstat(\STDIN)) && 0 < $fstat['size']) {
78-
$stdinKeyword = trim(stream_get_contents(\STDIN)); // @codeCoverageIgnore
79-
fclose(\STDIN); // @codeCoverageIgnore
80-
}
81-
})
82-
->tap(function () use (&$keyword, $stdinKeyword): void {
83-
$keyword = str($stdinKeyword ?: $this->argument('keyword') ?? text(
79+
->tap(function () use (&$keyword): void {
80+
$keyword = str($this->argument('keyword') ?? text(
8481
__('keyword_label'),
8582
__('keyword_placeholder'),
8683
$this->laravel->has('keyword') ? '' : __('keyword_default'),
@@ -93,7 +90,7 @@ public function handle(): void
9390
function () use ($keyword, &$duration): Collection {
9491
/** @noinspection PhpVoidFunctionResultUsedInspection */
9592
$timer = tap(new Timer)->start();
96-
$songs = $this->music->search($keyword, $this->option('sources'));
93+
$songs = $this->music->search($keyword, ['sources' => $this->option('sources')]);
9794
$duration = $timer->stop();
9895

9996
return $songs;
@@ -103,13 +100,13 @@ function () use ($keyword, &$duration): Collection {
103100
})
104101
->whenEmpty(function (): void {
105102
warning(__('empty_hint')); // @codeCoverageIgnore
106-
$this->handle(); // @codeCoverageIgnore
103+
$this->reHandle(); // @codeCoverageIgnore
107104
})
108105
->tap(function (Collection $songs) use ($keyword, $duration): void {
109106
table(__('table_header'), $this->sanitizes($songs, $keyword));
110107
info((new ResourceUsageFormatter)->resourceUsage($duration));
111108
})
112-
->tap(fn (): bool => confirm(__('confirm_label')) or $this->handle())
109+
->tap(fn (): bool => confirm(__('confirm_label')) or $this->reHandle())
113110
->tap(function (Collection $songs) use (&$selectedKeys, $keyword): void {
114111
$selectedKeys = $this
115112
->hydrates($songs, $keyword)
@@ -135,7 +132,7 @@ function () use ($keyword, &$duration): Collection {
135132
$this->option('directory'),
136133
resource_path('images/notify-icon.png')
137134
)))
138-
->unless($this->option('break'), fn (): null => $this->handle());
135+
->unless($this->option('break'), fn (): null => $this->reHandle());
139136
}
140137

141138
/**
@@ -190,21 +187,12 @@ protected function rules(): array
190187
];
191188
}
192189

193-
#[\Override]
194-
protected function interact(InputInterface $input, OutputInterface $output): void
195-
{
196-
parent::interact($input, $output);
197-
$this->afterPromptingForMissingArguments($input, $output);
198-
}
199-
200-
#[\Override]
201-
protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void
190+
/**
191+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
192+
*/
193+
private function reHandle(): void
202194
{
203-
if (empty($this->option('sources'))) {
204-
$input->setOption('sources', (array) select(
205-
__('select_source_label'),
206-
array_combine($sources = config('app.sources'), array_map(ucfirst(...), $sources))
207-
));
208-
}
195+
$this->input->setArgument('keyword', null);
196+
$this->handle();
209197
}
210198
}

app/Facades/Music.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Illuminate\Support\Facades\Facade;
1919

2020
/**
21-
* @method static \Illuminate\Support\Collection search(string $keyword, array $sources)
21+
* @method static \Illuminate\Support\Collection search(string $keyword, array $options)
2222
* @method static void download(string $url, string $savedPath)
2323
* @method static \App\Music setMeting(\App\Support\Meting $meting)
2424
* @method static \App\Music setDriver(\Illuminate\Contracts\Concurrency\Driver $driver)

0 commit comments

Comments
 (0)