ProcessAdultMovies::handle()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 81
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 81
rs 6.9666
cc 12
nc 11
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\Settings;
6
use App\Services\AdultProcessing\AdultProcessingPipeline;
7
use Illuminate\Console\Command;
8
9
class ProcessAdultMovies extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'nntmux:process-adult
17
                            {--title= : Process a specific movie title}
18
                            {--debug : Enable debug output}
19
                            {--limit= : Limit number of releases to process}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Process adult movie releases using the pipeline-based scraper';
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle(): int
32
    {
33
        if ((int) Settings::settingValue('lookupxxx') !== 1) {
34
            $this->error('Adult movie lookup is disabled in settings. Enable "lookupxxx" to use this command.');
35
            return Command::FAILURE;
36
        }
37
38
        $debug = $this->option('debug');
39
        $title = $this->option('title');
40
        $limit = $this->option('limit');
41
42
        $pipeline = new AdultProcessingPipeline([], true);
43
44
        if ($title) {
45
            // Process a single title
46
            $this->info("Looking up: {$title}");
47
48
            $result = $pipeline->processMovie($title, $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\AdultProces...ipeline::processMovie(). ( Ignorable by Annotation )

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

48
            $result = $pipeline->processMovie($title, /** @scrutinizer ignore-type */ $debug);
Loading history...
49
50
            if ($result['status'] === 'matched') {
51
                $this->info("Match found on {$result['provider']}!");
52
                $title_display = $result['movieData']['title'] ?? 'N/A';
53
                $synopsis_display = substr($result['movieData']['synopsis'] ?? 'N/A', 0, 200);
54
                $this->line("Title: {$title_display}");
55
                $this->line("Synopsis: {$synopsis_display}...");
56
57
                if (!empty($result['movieData']['boxcover'])) {
58
                    $cover_url = $result['movieData']['boxcover'];
59
                    $this->line("Cover: {$cover_url}");
60
                }
61
62
                if ($debug && !empty($result['debug'])) {
63
                    $this->newLine();
64
                    $this->line('Debug Info:');
65
                    $this->line(json_encode($result['debug'], JSON_PRETTY_PRINT));
66
                }
67
            } else {
68
                $this->warn("No match found for: {$title}");
69
70
                if ($debug && !empty($result['debug'])) {
71
                    $this->newLine();
72
                    $this->line('Debug Info:');
73
                    $this->line(json_encode($result['debug'], JSON_PRETTY_PRINT));
74
                }
75
            }
76
        } else {
77
            // Process all pending releases
78
            $this->info('Processing adult movie releases using pipeline...');
79
80
            if ($limit) {
81
                $this->info("Limited to {$limit} releases");
82
            }
83
84
            $pipeline->processXXXReleases();
85
86
            $stats = $pipeline->getStats();
87
88
            $this->newLine();
89
            $this->table(
90
                ['Metric', 'Value'],
91
                [
92
                    ['Processed', $stats['processed']],
93
                    ['Matched', $stats['matched']],
94
                    ['Failed', $stats['failed']],
95
                    ['Skipped', $stats['skipped']],
96
                    ['Duration', sprintf('%.2f seconds', $stats['duration'])],
97
                ]
98
            );
99
100
            if (!empty($stats['providers'])) {
101
                $this->newLine();
102
                $this->info('Provider Statistics:');
103
                $providerData = [];
104
                foreach ($stats['providers'] as $provider => $count) {
105
                    $providerData[] = [$provider, $count];
106
                }
107
                $this->table(['Provider', 'Matches'], $providerData);
108
            }
109
        }
110
111
        return Command::SUCCESS;
112
    }
113
}
114
115