ReprocessTvRelease::handle()   C
last analyzed

Complexity

Conditions 10
Paths 205

Size

Total Lines 81
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 81
rs 6.3984
cc 10
nc 205
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Release;
6
use App\Services\TvProcessing\TvProcessingPipeline;
7
use Blacklight\ColorCLI;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\Log;
10
11
class ReprocessTvRelease extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'tv:reprocess
19
                            {guid : The release GUID to reprocess}
20
                            {--reset : Reset the release video/episode IDs before reprocessing}
21
                            {--debug : Show detailed debug information}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Reprocess a specific TV release by GUID to rematch it against TV databases';
29
30
    protected ColorCLI $colorCli;
31
32
    public function __construct()
33
    {
34
        parent::__construct();
35
        $this->colorCli = new ColorCLI();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     */
41
    public function handle(): int
42
    {
43
        $guid = $this->argument('guid');
44
        $reset = $this->option('reset');
45
        $debug = $this->option('debug');
46
47
        // Find the release by GUID
48
        $release = Release::query()->where('guid', $guid)->first();
49
50
        if ($release === null) {
51
            $this->error("Release with GUID '{$guid}' not found.");
52
            return self::FAILURE;
53
        }
54
55
        $this->info('Found release:');
56
        $this->table(
57
            ['ID', 'GUID', 'Search Name', 'Videos ID', 'Episode ID', 'Category'],
58
            [[
59
                $release->id,
60
                $release->guid,
0 ignored issues
show
Bug introduced by
The property guid does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
61
                mb_substr($release->searchname, 0, 60) . (strlen($release->searchname) > 60 ? '...' : ''),
0 ignored issues
show
Bug introduced by
The property searchname does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
62
                $release->videos_id,
0 ignored issues
show
Bug introduced by
The property videos_id does not exist on App\Models\Release. Did you mean video?
Loading history...
63
                $release->tv_episodes_id,
0 ignored issues
show
Bug introduced by
The property tv_episodes_id does not exist on App\Models\Release. Did you mean episode?
Loading history...
64
                $release->categories_id,
0 ignored issues
show
Bug introduced by
The property categories_id does not exist on App\Models\Release. Did you mean category_ids?
Loading history...
65
            ]]
66
        );
67
68
        if ($reset) {
69
            $this->warn('Resetting videos_id and tv_episodes_id to 0...');
70
            $release->update([
71
                'videos_id' => 0,
72
                'tv_episodes_id' => 0,
73
            ]);
74
            $release->refresh();
75
        }
76
77
        $this->info('Processing release through TV pipeline...');
78
        $this->newLine();
79
80
        try {
81
            $pipeline = TvProcessingPipeline::createDefault(echoOutput: true);
82
            $result = $pipeline->processRelease($release, $debug);
0 ignored issues
show
Bug introduced by
$debug of type string is incompatible with the type boolean expected by parameter $debug of App\Services\TvProcessin...eline::processRelease(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
            $result = $pipeline->processRelease($release, /** @scrutinizer ignore-type */ $debug);
Loading history...
83
84
            $this->newLine();
85
86
            if ($result['matched']) {
87
                $this->colorCli->primary('Successfully matched!');
88
                $this->info('Provider: ' . ($result['provider'] ?? 'Unknown'));
89
                $this->info('Video ID: ' . ($result['video_id'] ?? 'N/A'));
90
                $this->info('Episode ID: ' . ($result['episode_id'] ?? 'N/A'));
91
            } else {
92
                $this->colorCli->warning('No match found.');
93
                $this->info('Status: ' . ($result['status'] ?? 'Unknown'));
94
                if (isset($result['provider'])) {
95
                    $this->info('Last provider tried: ' . $result['provider']);
96
                }
97
            }
98
99
            if ($debug && isset($result['debug'])) {
100
                $this->newLine();
101
                $this->info('Debug Information:');
102
                $this->line(json_encode($result['debug'], JSON_PRETTY_PRINT));
103
            }
104
105
            // Show final state of the release
106
            $release->refresh();
107
            $this->newLine();
108
            $this->info('Final release state:');
109
            $this->table(
110
                ['Videos ID', 'Episode ID'],
111
                [[$release->videos_id, $release->tv_episodes_id]]
112
            );
113
114
            return self::SUCCESS;
115
        } catch (\Throwable $e) {
116
            Log::error($e->getTraceAsString());
117
            $this->error('Error processing release: ' . $e->getMessage());
118
            if ($debug) {
119
                $this->error($e->getTraceAsString());
120
            }
121
            return self::FAILURE;
122
        }
123
    }
124
}
125