koel/app/Services/YouTubeService.php

37 lines
913 B
PHP
Raw Normal View History

2016-07-14 08:47:50 +00:00
<?php
namespace App\Services;
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
{
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
}
public function searchVideosRelatedToSong(Song $song, string $pageToken = ''): ?object
2016-07-30 15:32:17 +00:00
{
if (!self::enabled()) {
return null;
2016-07-30 15:32:17 +00:00
}
$request = new SearchVideosRequest($song, $pageToken);
$hash = md5(serialize($request->query()->all()));
2022-08-08 16:00:59 +00:00
return $this->cache->remember(
"youtube.$hash",
now()->addWeek(),
fn () => $this->connector->send($request)->object()
);
}
2016-07-14 08:47:50 +00:00
}