Passed
Push — master ( f9d86d...09d1e9 )
by Darko
10:04
created

FetchMovieByImdb::handle()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 38
rs 8.6026
cc 7
nc 4
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Blacklight\Movie;
6
use Illuminate\Console\Command;
7
8
class FetchMovieByImdb extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * Accepts either a numeric IMDb id (eg: 1234567) or prefixed (tt1234567). Will strip 'tt'.
14
     */
15
    protected $signature = 'nntmux:fetch-movie {imdbid : IMDb id with or without tt prefix}';
16
17
    /**
18
     * The console command description.
19
     */
20
    protected $description = 'Force fetch/update movie data for a specific IMDb id from external providers (TMDB/IMDB/Trakt/OMDB/iTunes/Fanart), always updating local data.';
21
22
    public function handle(): int
23
    {
24
        $raw = (string) $this->argument('imdbid');
25
        $normalized = strtolower(trim($raw));
26
        $normalized = preg_replace('/^tt/i', '', $normalized); // remove leading tt if present
27
        $imdbId = preg_replace('/\D/', '', $normalized); // keep digits only
28
29
        if ($imdbId === '' || ! ctype_digit($imdbId) || strlen($imdbId) < 5) {
30
            $this->error('Invalid IMDb id provided: '.$raw.' (parsed: '.$imdbId.')');
31
32
            return self::FAILURE;
33
        }
34
35
        $movie = app(Movie::class);
36
37
        $this->info('Force fetching movie data for IMDb id: tt'.$imdbId.' ...');
38
        $ok = $movie->updateMovieInfo($imdbId);
39
        if (! $ok) {
40
            $this->error('Failed to fetch/update movie data for tt'.$imdbId.'.');
41
42
            return self::FAILURE;
43
        }
44
45
        $updated = $movie->getMovieInfo($imdbId);
46
        if ($updated === null) {
47
            $this->error('Movie info not found after update for tt'.$imdbId.'.');
48
49
            return self::FAILURE;
50
        }
51
52
        $this->line('');
53
        $this->info('Updated movie: '.($updated->title ?? 'Unknown Title').' ('.$updated->year.')');
54
        $this->line('IMDb: tt'.$imdbId);
55
        $this->line('Rating: '.($updated->rating ?? 'N/A'));
56
        $this->line('Genre: '.($updated->genre ?? 'N/A'));
57
        $this->line('Cover: '.(($updated->cover ?? 0) == 1 ? 'Yes' : 'No'));
58
59
        return self::SUCCESS;
60
    }
61
}
62