Refactor Trakt configuration to use config file and dependency injection

This commit is contained in:
Dan Jones 2025-11-16 20:45:27 -06:00
commit caa1f0e442
5 changed files with 32 additions and 19 deletions

View file

@ -5,16 +5,16 @@ declare(strict_types=1);
namespace App\Commands; namespace App\Commands;
use App\Services\Trakt; use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository;
class TraktLogin extends Command class TraktLogin extends Command
{ {
protected $signature = 'trakt:login'; protected $signature = 'trakt:login';
protected $description = 'Login and get new token.'; protected $description = 'Login and get new token.';
public function handle(Trakt $trakt, Repository $config): int
public function handle(Trakt $trakt): int
{ {
$data = [ $data = [
'client_id' => env('TRAKT_APP_ID'), 'client_id' => $config->get('trakt.app_id'),
]; ];
$resp = $trakt->requestNoAuth()->post('/oauth/device/code', $data); $resp = $trakt->requestNoAuth()->post('/oauth/device/code', $data);
@ -33,8 +33,8 @@ class TraktLogin extends Command
$this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'"); $this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'");
$data = [ $data = [
'client_id' => env('TRAKT_APP_ID'), 'client_id' => $config->get('trakt.app_id'),
'client_secret' => env('TRAKT_APP_SECRET'), 'client_secret' => $config->get('trakt.app_secret'),
'code' => $body['device_code'], 'code' => $body['device_code'],
]; ];

View file

@ -5,25 +5,25 @@ declare(strict_types=1);
namespace App\Commands; namespace App\Commands;
use App\Services\Trakt; use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository;
class TraktRefreshLogin extends Command class TraktRefreshLogin extends Command
{ {
protected $signature = 'trakt:refresh'; protected $signature = 'trakt:refresh';
protected $description = 'Refresh access token.'; protected $description = 'Refresh access token.';
public function handle(Trakt $trakt, Repository $config): int
public function handle(Trakt $trakt): int
{ {
$oauth_resp = json_decode(env('TRAKT_OAUTH_RESP'), true); $refresh_token = $config->get('trakt.login.oauth.refresh_token');
if (empty($oauth_resp)) { if (empty($refresh_token)) {
$this->line("Failed to get OAuth response"); $this->line("Failed to get refresh token");
return static::FAILURE; return static::FAILURE;
} }
$data = [ $data = [
'refresh_token' => $oauth_resp['refresh_token'], 'refresh_token' => $refresh_token,
'client_id' => env('TRAKT_APP_ID'), 'client_id' => $config->get('trakt.app_id'),
'client_secret' => env('TRAKT_APP_SECRET'), 'client_secret' => $config->get('trakt.app_secret'),
'redirect_uri' => env('TRAKT_REDIRECT_URI'), 'redirect_uri' => $config->get('trakt.redirect_uri'),
'grant_type' => 'refresh_token', 'grant_type' => 'refresh_token',
]; ];

View file

@ -7,6 +7,7 @@ use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Queue\Factory as QueueFactoryContract; use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
use App\Queue\DatabaseConnector; use App\Queue\DatabaseConnector;
use App\Services\Trakt; use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -29,6 +30,6 @@ class AppServiceProvider extends ServiceProvider
public function register() public function register()
{ {
$this->app->instance(ProcessInput::class, new ProcessInput()); $this->app->instance(ProcessInput::class, new ProcessInput());
$this->app->instance(Trakt::class, new Trakt()); $this->app->singleton(Trakt::class, fn ($app) => new Trakt($app->make(Repository::class)));
} }
} }

View file

@ -6,18 +6,19 @@ use App\Data\WatchData;
use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response; use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Contracts\Config\Repository;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class Trakt class Trakt
{ {
protected $headers = []; protected array $headers = [];
public function __construct() public function __construct(Repository $config)
{ {
$this->headers = [ $this->headers = [
'trakt-api-version' => 2, 'trakt-api-version' => 2,
'trakt-api-key' => env('TRAKT_APP_ID'), 'trakt-api-key' => $config->get('trakt.app_id'),
'Authorization' => 'Bearer ' . env('TRAKT_OAUTH_TOKEN'), 'Authorization' => 'Bearer ' . $config->get('trakt.login.oauth.access_token'),
]; ];
} }

11
config/trakt.php Normal file
View file

@ -0,0 +1,11 @@
<?php
return [
'app_id' => env('TRAKT_APP_ID'),
'app_secret' => env('TRAKT_APP_secret'),
'redirect_uri' => env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob'),
'login' => [
'device' => json_decode(env('TRAKT_DEVICE_RESP', '{}}'), true),
'oauth' => json_decode(env('TRAKT_OAUTH_RESP', '{}}'), true),
],
];