diff --git a/app/Http/Controllers/API/DataController.php b/app/Http/Controllers/API/DataController.php index 802be2b3..6f5e5d03 100644 --- a/app/Http/Controllers/API/DataController.php +++ b/app/Http/Controllers/API/DataController.php @@ -7,6 +7,9 @@ use App\Models\Interaction; use App\Models\Playlist; use App\Models\Setting; use App\Models\User; +use App\Services\iTunesService; +use App\Services\LastfmService; +use App\Services\YouTubeService; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use iTunes; @@ -16,6 +19,21 @@ use YouTube; class DataController extends Controller { + private $lastfmService; + private $youTubeService; + private $iTunesService; + + public function __construct( + LastfmService $lastfmService, + YouTubeService $youTubeService, + iTunesService $iTunesService + ) + { + $this->lastfmService = $lastfmService; + $this->youTubeService = $youTubeService; + $this->iTunesService = $iTunesService; + } + /** * Get a set of application data. * @@ -31,9 +49,9 @@ class DataController extends Controller 'interactions' => Interaction::byCurrentUser()->get(), 'users' => $request->user()->is_admin ? User::all() : [], 'currentUser' => $request->user(), - 'useLastfm' => Lastfm::used(), - 'useYouTube' => YouTube::enabled(), - 'useiTunes' => iTunes::used(), + 'useLastfm' => $this->lastfmService->used(), + 'useYouTube' => $this->youTubeService->enabled(), + 'useiTunes' => $this->iTunesService->used(), 'allowDownload' => config('koel.download.allow'), 'supportsTranscoding' => config('koel.streaming.ffmpeg_path') && is_executable(config('koel.streaming.ffmpeg_path')), diff --git a/app/Http/Controllers/API/iTunesController.php b/app/Http/Controllers/API/iTunesController.php index de53772e..bca8f4ee 100644 --- a/app/Http/Controllers/API/iTunesController.php +++ b/app/Http/Controllers/API/iTunesController.php @@ -4,11 +4,18 @@ namespace App\Http\Controllers\API; use App\Http\Requests\API\ViewSongOnITunesRequest; use App\Models\Album; +use App\Services\iTunesService; use Illuminate\Http\RedirectResponse; -use iTunes; class iTunesController extends Controller { + private $iTunesService; + + public function __construct(iTunesService $iTunesService) + { + $this->iTunesService = $iTunesService; + } + /** * View a song on iTunes store. * @@ -19,7 +26,7 @@ class iTunesController extends Controller */ public function viewSong(ViewSongOnITunesRequest $request, Album $album) { - $url = iTunes::getTrackUrl($request->q, $album->name, $album->artist->name); + $url = $this->iTunesService->getTrackUrl($request->q, $album->name, $album->artist->name); abort_unless($url, 404, "Koel can't find such a song on iTunes Store."); return redirect($url); diff --git a/app/Providers/iTunesServiceProvider.php b/app/Providers/iTunesServiceProvider.php index 71692af9..ea3d2513 100644 --- a/app/Providers/iTunesServiceProvider.php +++ b/app/Providers/iTunesServiceProvider.php @@ -2,21 +2,11 @@ namespace App\Providers; -use App\Services\iTunes; +use App\Services\iTunesService; use Illuminate\Support\ServiceProvider; class iTunesServiceProvider extends ServiceProvider { - /** - * Bootstrap the application services. - * - * @return void - */ - public function boot() - { - // - } - /** * Register the application services. * @@ -25,7 +15,7 @@ class iTunesServiceProvider extends ServiceProvider public function register() { app()->singleton('iTunes', function () { - return new iTunes(); + return app()->make(iTunesService::class); }); } } diff --git a/app/Services/iTunes.php b/app/Services/iTunesService.php similarity index 80% rename from app/Services/iTunes.php rename to app/Services/iTunesService.php index 44304fa2..e6a767c6 100644 --- a/app/Services/iTunes.php +++ b/app/Services/iTunesService.php @@ -4,31 +4,10 @@ namespace App\Services; use Cache; use Exception; -use GuzzleHttp\Client; use Log; -class iTunes +class iTunesService extends ApiClient implements ApiConsumerInterface { - /** - * @var Client - */ - protected $client; - - /** - * @var string - */ - protected $endPoint = 'https://itunes.apple.com/search'; - - /** - * iTunes constructor. - * - * @param Client|null $client - */ - public function __construct(Client $client = null) - { - $this->client = $client ?: new Client(); - } - /** * Determines whether to use iTunes services. * @@ -60,7 +39,7 @@ class iTunes 'limit' => 1, ]; - $response = (string) $this->client->get($this->endPoint, ['query' => $params])->getBody(); + $response = (string) $this->client->get($this->getEndpoint(), ['query' => $params])->getBody(); $response = json_decode($response); if (!$response->resultCount) { @@ -80,4 +59,19 @@ class iTunes return false; } } + + public function getKey() + { + return null; + } + + public function getSecret() + { + return null; + } + + public function getEndpoint() + { + return config('koel.itunes.endpoint'); + } } diff --git a/config/koel.php b/config/koel.php index 3b13c561..9b2670b4 100644 --- a/config/koel.php +++ b/config/koel.php @@ -115,6 +115,7 @@ return [ 'itunes' => [ 'enabled' => env('USE_ITUNES', true), 'affiliate_id' => '1000lsGu', + 'endpoint' => 'https://itunes.apple.com/search', ], 'cache_media' => env('CACHE_MEDIA', true), diff --git a/tests/Integration/Services/iTunesServiceTest.php b/tests/Integration/Services/iTunesServiceTest.php new file mode 100644 index 00000000..e8d4dd18 --- /dev/null +++ b/tests/Integration/Services/iTunesServiceTest.php @@ -0,0 +1,35 @@ + new Response(200, [], file_get_contents(__DIR__.'../../../blobs/itunes/track.json')), + ]); + + $url = (new iTunesService($client))->getTrackUrl($term); + + self::assertEquals( + 'https://itunes.apple.com/us/album/i-remember-you/id265611220?i=265611396&uo=4&at=1000lsGu', + $url + ); + + self::assertNotNull(cache('b57a14784d80c58a856e0df34ff0c8e2')); + } +} diff --git a/tests/Integration/Services/iTunesTest.php b/tests/Integration/Services/iTunesTest.php deleted file mode 100644 index 2e8331b1..00000000 --- a/tests/Integration/Services/iTunesTest.php +++ /dev/null @@ -1,43 +0,0 @@ - new Response(200, [], file_get_contents(__DIR__.'../../../blobs/itunes/track.json')), - ]); - - $url = (new iTunes($client))->getTrackUrl($term); - - // Then I retrieve the track URL - $this->assertEquals('https://itunes.apple.com/us/album/i-remember-you/id265611220?i=265611396&uo=4&at=1000lsGu', - $url); - - // And the track url is cached - $this->assertNotNull(cache('b57a14784d80c58a856e0df34ff0c8e2')); - } -} diff --git a/tests/Unit/Services/iTunesServiceTest.php b/tests/Unit/Services/iTunesServiceTest.php new file mode 100644 index 00000000..ba18dc40 --- /dev/null +++ b/tests/Unit/Services/iTunesServiceTest.php @@ -0,0 +1,20 @@ + true]); + /** @var iTunesService $iTunes */ + $iTunes = app()->make(iTunesService::class); + $this->assertTrue($iTunes->used()); + + config(['koel.itunes.enabled' => false]); + $this->assertFalse($iTunes->used()); + } +} diff --git a/tests/Unit/Services/iTunesTest.php b/tests/Unit/Services/iTunesTest.php deleted file mode 100644 index d7c79732..00000000 --- a/tests/Unit/Services/iTunesTest.php +++ /dev/null @@ -1,38 +0,0 @@ -assertInstanceOf(iTunes::class, new iTunes()); - } - - /** @test */ - public function its_usage_status_is_determined_by_configuration() - { - // Given the configuration to use the iTunes service is set to TRUE - config(['koel.itunes.enabled' => true]); - $iTunes = new iTunes(); - - // When I check if the iTunes service should be used - $used = $iTunes->used(); - - // Then I see TRUE - $this->assertTrue($used); - - // If the configuration is set to FALSE - config(['koel.itunes.enabled' => false]); - - // When I check if the iTunes service should be used - $used = $iTunes->used(); - - // Then I see FALSE - $this->assertFalse($used); - } -}