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