Use a service for iTunes

This commit is contained in:
Phan An 2018-08-19 16:40:25 +02:00
parent e634a199b5
commit 1c76ff6d76
9 changed files with 105 additions and 121 deletions

View file

@ -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')),

View file

@ -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);

View file

@ -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);
});
}
}

View file

@ -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');
}
}

View file

@ -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),

View file

@ -0,0 +1,35 @@
<?php
namespace Tests\Integration\Services;
use App\Services\iTunesService;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery;
use Tests\TestCase;
class iTunesServiceTest extends TestCase
{
/**
* @throws Exception
*/
public function testGetTrackUrl()
{
$term = 'Foo Bar';
/** @var Client $client */
$client = Mockery::mock(Client::class, [
'get' => 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'));
}
}

View file

@ -1,43 +0,0 @@
<?php
namespace Tests\Integration\Services;
use App\Services\iTunes;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery as m;
use Tests\TestCase;
class iTunesTest extends TestCase
{
public function tearDown()
{
m::close();
parent::tearDown();
}
/**
* @test
*
* @throws \Exception
*/
public function it_gets_itunes_track_url()
{
// Given there's a search term
$term = 'Foo Bar';
// When I request the iTunes track URL for the song
$client = m::mock(Client::class, [
'get' => 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'));
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\Services;
use App\Services\iTunesService;
use Tests\TestCase;
class iTunesServiceTest extends TestCase
{
public function testConfiguration()
{
config(['koel.itunes.enabled' => true]);
/** @var iTunesService $iTunes */
$iTunes = app()->make(iTunesService::class);
$this->assertTrue($iTunes->used());
config(['koel.itunes.enabled' => false]);
$this->assertFalse($iTunes->used());
}
}

View file

@ -1,38 +0,0 @@
<?php
namespace Tests\Unit\Services;
use App\Services\iTunes;
use Tests\TestCase;
class iTunesTest extends TestCase
{
/** @test */
public function it_can_be_instantiated()
{
$this->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);
}
}