koel/app/Services/YouTubeService.php

38 lines
947 B
PHP
Raw Normal View History

2016-07-14 16:47:50 +08:00
<?php
namespace App\Services;
use App\Http\Integrations\YouTube\Requests\SearchVideosRequest;
use App\Http\Integrations\YouTube\YouTubeConnector;
2016-07-30 23:32:17 +08:00
use App\Models\Song;
2022-08-08 18:00:59 +02:00
use Illuminate\Cache\Repository as Cache;
2016-07-14 16:47:50 +08:00
2022-08-08 18:00:59 +02:00
class YouTubeService
2016-07-14 16:47:50 +08:00
{
public function __construct(private YouTubeConnector $connector, private Cache $cache)
2022-08-08 18:00:59 +02:00
{
}
public static function enabled(): bool
2016-07-14 16:47:50 +08:00
{
2022-08-08 18:00:59 +02:00
return (bool) config('koel.youtube.key');
2016-07-14 16:47:50 +08:00
}
/** @return array<mixed>|null */
public function searchVideosRelatedToSong(Song $song, string $pageToken = ''): ?array
2016-07-30 23:32:17 +08:00
{
if (!self::enabled()) {
return null;
2016-07-30 23:32:17 +08:00
}
$request = new SearchVideosRequest($song, $pageToken);
$hash = md5(serialize($request->query()->all()));
2022-08-08 18:00:59 +02:00
return $this->cache->remember(
"youtube:$hash",
now()->addWeek(),
fn () => $this->connector->send($request)->json()
);
}
2016-07-14 16:47:50 +08:00
}