Skip to content

Commit 1c7f576

Browse files
committed
chore: add a few refactors
1 parent b57732d commit 1c7f576

17 files changed

+80
-17
lines changed

Diff for: .env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TRAKT_API_TOKEN=

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/build
22
/torrents
33
/vendor
4+
/.env
45
/.phpunit.result.cache
56
/.php-cs-fixer.cache
67
/composer.lock

Diff for: app/Commands/DownloadCommand.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class DownloadCommand extends Command
1616
{
1717
/** {@inheritdoc} */
18-
protected $signature = 'download { trakt-user= : Trakt username for the list }
18+
protected $signature = 'download { trakt-user? : Trakt username for the list }
1919
{ --l|list=watchlist : A custom list id or stub }
2020
{ --o|output=./torrents : The directory to output data to }
2121
{ --quality=1080p : The quality to download (720p, 1080p or 3D) }
@@ -33,12 +33,11 @@ class DownloadCommand extends Command
3333

3434
private YTSClient $yts;
3535

36-
public function handle(TraktClient $traktClient): void
36+
public function handle(TraktClient $traktClient, YTSClient $ytsClient): void
3737
{
3838
$this->trakt = $traktClient;
3939
$this->yts = $ytsClient;
4040

41-
$
4241
$this->quality = Quality::tryFrom($this->option('quality')) ?? Quality::Q_1080P;
4342

4443
try {

Diff for: app/Exceptions/Handler.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Exceptions;
6+
7+
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8+
use function Termwind\render;
9+
use Throwable;
10+
11+
class Handler extends ExceptionHandler
12+
{
13+
public function renderForConsole($output, Throwable $e): void
14+
{
15+
if ($e instanceof UnauthorizedException) {
16+
render(view('unauthorised')->render());
17+
18+
return;
19+
}
20+
21+
if ($e instanceof TraktToYTSException) {
22+
render(view('error', ['message' => $e->getMessage()])->render());
23+
24+
return;
25+
}
26+
27+
parent::renderForConsole($output, $e);
28+
}
29+
}

Diff for: app/Exceptions/NoMovieDataFound.php renamed to app/Exceptions/NoMovieDataFoundException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use RuntimeException;
66

7-
class NoMovieDataFound extends RuntimeException
7+
class NoMovieDataFoundException extends RuntimeException implements TraktToYTSException
88
{
99
public static function forImdbIdOnYts(string $imdbId): self
1010
{

Diff for: app/Exceptions/TraktToYTSException.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Exceptions;
6+
7+
interface TraktToYTSException
8+
{
9+
}

Diff for: app/Exceptions/ApiKeyNotSpecified.php renamed to app/Exceptions/UnauthorizedException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use RuntimeException;
66

7-
class ApiKeyNotSpecified extends RuntimeException
7+
class UnauthorizedException extends RuntimeException implements TraktToYTSException
88
{
99
public static function forServiceWithId(string $serviceId): self
1010
{

Diff for: app/Providers/AppServiceProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Providers;
44

5-
use App\Exceptions\ApiKeyNotSpecified;
5+
use App\Exceptions\UnauthorizedException;
66
use App\Services\Trakt\Client;
77
use Illuminate\Config\Repository;
88
use Illuminate\Support\ServiceProvider;
@@ -13,7 +13,7 @@ public function register(): void
1313
{
1414
$this->app->singleton(Client::class, function (): Client {
1515
if (! $traktApiKey = $this->app->get(Repository::class)->get('services.trakt.token')) {
16-
throw ApiKeyNotSpecified::forServiceWithId(Client::SERVICE_ID);
16+
throw UnauthorizedException::forServiceWithId(Client::SERVICE_ID);
1717
}
1818

1919
return new Client($traktApiKey);

Diff for: app/Services/YTS/Client.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Services\YTS;
44

5-
use App\Exceptions\NoMovieDataFound;
5+
use App\Exceptions\NoMovieDataFoundException;
66
use App\Services\YTS\Enums\Quality;
77
use App\Services\YTS\ValueObjects\Movie;
88
use App\Services\YTS\ValueObjects\Torrent;
@@ -11,15 +11,8 @@
1111

1212
class Client
1313
{
14-
public const SERVICE_ID = 'YTS';
15-
1614
public const BASE_URI = 'https://yts.mx';
1715

18-
/**
19-
* @param string $imdbId
20-
* @param Quality|null $quality
21-
* @return Movie
22-
*/
2316
public function getMovieByImdbId(string|null $imdbId, Quality|null $quality = null): Movie
2417
{
2518
$response = Http::baseUrl(self::BASE_URI)->get('/api/v2/list_movies.json', [
@@ -28,7 +21,7 @@ public function getMovieByImdbId(string|null $imdbId, Quality|null $quality = nu
2821
])->json();
2922

3023
if (! Arr::has($response, 'data.movies.0')) {
31-
throw NoMovieDataFound::forImdbIdOnYts($imdbId);
24+
throw NoMovieDataFoundException::forImdbIdOnYts($imdbId);
3225
}
3326

3427
return Movie::fromResponse(

Diff for: bootstrap/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
$app->singleton(
3535
Illuminate\Contracts\Debug\ExceptionHandler::class,
36-
Illuminate\Foundation\Exceptions\Handler::class
36+
App\Exceptions\Handler::class
3737
);
3838

3939
/*

Diff for: box.json

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"app",
44
"bootstrap",
55
"config",
6+
"resources",
67
"vendor"
78
],
89
"files": [
910
"composer.json"
1011
],
12+
"exclude-dev-files": false,
1113
"exclude-composer-files": false,
1214
"compression": "GZ",
1315
"compactors": [

Diff for: builds/trakt-to-yts

-11 MB
Binary file not shown.

Diff for: config/view.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'paths' => [
7+
resource_path('views'),
8+
],
9+
10+
'compiled' => sys_get_temp_dir(),
11+
];

Diff for: resources/views/components/layouts/app.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div {{ $attributes->merge(['class' => 'ml-2 my-1']) }}>
2+
{{ $slot }}
3+
</div>

Diff for: resources/views/error.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-layouts.app>
2+
<span class="text-red">{{ $notice }}</span>
3+
</x-layouts.app>

Diff for: resources/views/notice.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-layouts.app>
2+
<span>{{ $notice }}</span>
3+
</x-layouts.app>

Diff for: resources/views/unauthorised.blade.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<x-layouts.app>
2+
<div class="text-red-500">
3+
<span>Unauthorised</span>
4+
</div>
5+
6+
<p class="mt-1">
7+
Please set the <span class="font-bold">TRAKT_API_TOKEN</span> environment variable to your <a href="https://trakt.tv/oauth/applications">Trakt API token</a>.
8+
</p>
9+
</x-layouts.app>

0 commit comments

Comments
 (0)