Skip to content

Commit 4248e01

Browse files
committed
refactor(Music): change return type of search method in SequenceMusic class
- Change the return type of the search method in the SequenceMusic class to Collection - Use collect function to ensure with URLs - Filter songs with empty URLs - Return the filtered songs collection
1 parent 250a8de commit 4248e01

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

app/Commands/MusicCommand.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ public function handle(Timer $timer, ResourceUsageFormatter $resourceUsageFormat
7979
))->trim()->toString();
8080
})
8181
->pipe(function () use ($timer, $keyword, &$duration): Collection {
82-
$timer->start();
83-
$songs = spin(
84-
fn (): array => $this->music->search($keyword, $this->option('sources')),
82+
return spin(
83+
function () use ($timer, $keyword, &$duration): Collection {
84+
$timer->start();
85+
$songs = $this->music->search($keyword, $this->option('sources'));
86+
$duration = $timer->stop();
87+
88+
return $songs;
89+
},
8590
$this->config['searching_hint']
86-
);
87-
$duration = $timer->stop();
88-
89-
return collect($songs)->mapWithKeys(static fn ($song, $index): array => [$index + 1 => $song]);
91+
)->mapWithKeys(static fn ($song, $index): array => [$index + 1 => $song]);
9092
})
9193
->whenEmpty(function (): void {
9294
warning($this->config['empty_hint']);

app/Contracts/Music.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
namespace App\Contracts;
1414

15+
use Illuminate\Support\Collection;
16+
1517
interface Music
1618
{
17-
public function search(string $keyword, array $sources = []): array;
19+
public function search(string $keyword, array $sources = []): Collection;
1820

1921
public function download(string $url, string $savePath): void;
2022
}

app/Facades/Music.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @method static bool hasMacro(string $name)
3131
* @method static void flushMacros()
3232
* @method static \App\MusicManager|\Illuminate\Support\HigherOrderTapProxy tap(callable|null $callback = null)
33-
* @method static array search(string $keyword, array $sources = [])
33+
* @method static \Illuminate\Support\Collection search(string $keyword, array $sources = [])
3434
* @method static void download(string $url, string $savePath)
3535
*
3636
* @see \App\MusicManager

app/Music/SequenceMusic.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use App\Support\Meting;
1818
use GuzzleHttp\Exception\GuzzleException;
1919
use Illuminate\Console\OutputStyle;
20+
use Illuminate\Support\Collection;
2021
use Illuminate\Support\Traits\Macroable;
2122
use Laravel\Prompts\Progress;
2223

@@ -39,7 +40,7 @@ public function __construct(
3940
*
4041
* @throws \JsonException
4142
*/
42-
public function search(string $keyword, array $sources = []): array
43+
public function search(string $keyword, array $sources = []): Collection
4344
{
4445
$withoutUrlSongs = collect($sources)
4546
->map(fn (string $source): array => json_decode(
@@ -54,8 +55,7 @@ public function search(string $keyword, array $sources = []): array
5455
return collect($this->ensureWithUrls($withoutUrlSongs))
5556
->filter()
5657
->filter(static fn (array $song): bool => ! empty($song['url']))
57-
->values()
58-
->all();
58+
->values();
5959
}
6060

6161
/**

0 commit comments

Comments
 (0)