Completed
Push — master ( 52aff9...2d40fa )
by Darko
38s queued 10s
created

TvProcessor::displayProviderComplete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Settings;
6
use Blacklight\ColorCLI;
7
use Blacklight\processing\tv\LocalDB;
8
use Blacklight\processing\tv\TMDB;
9
use Blacklight\processing\tv\TraktTv;
10
use Blacklight\processing\tv\TVDB;
11
use Blacklight\processing\tv\TVMaze;
12
13
class TvProcessor
14
{
15
    // Processing modes
16
    public const MODE_PIPELINE = 'pipeline';  // Sequential processing (efficient, reduces API calls)
17
18
    public const MODE_PARALLEL = 'parallel';  // Parallel processing (faster, more API calls)
19
20
    private bool $echooutput;
21
22
    private ColorCLI $colorCli;
23
24
    private array $stats = [
0 ignored issues
show
introduced by
The private property $stats is not used, and could be removed.
Loading history...
25
        'total' => 0,
26
        'processed' => 0,
27
        'matched' => 0,
28
        'failed' => 0,
29
        'skipped' => 0,
30
        'byProvider' => [
31
            'Local DB' => ['processed' => 0, 'matched' => 0, 'failed' => 0],
32
            'TVDB' => ['processed' => 0, 'matched' => 0, 'failed' => 0],
33
            'TVMaze' => ['processed' => 0, 'matched' => 0, 'failed' => 0],
34
            'TMDB' => ['processed' => 0, 'matched' => 0, 'failed' => 0],
35
            'Trakt' => ['processed' => 0, 'matched' => 0, 'failed' => 0],
36
        ],
37
    ];
38
39
    public function __construct(bool $echooutput)
40
    {
41
        $this->echooutput = $echooutput;
42
        $this->colorCli = new ColorCLI;
43
    }
44
45
    /**
46
     * Process all TV related releases across supported providers.
47
     *
48
     * @param  string  $groupID  Group ID to process
49
     * @param  string  $guidChar  GUID character to process
50
     * @param  int|string|null  $processTV  0/1/2 or '' to read from settings
51
     * @param  string  $mode  Processing mode: 'pipeline' (sequential) or 'parallel' (simultaneous)
52
     */
53
    public function process(string $groupID = '', string $guidChar = '', int|string|null $processTV = '', string $mode = self::MODE_PIPELINE): void
54
    {
55
        $processTV = (is_numeric($processTV) ? $processTV : Settings::settingValue('lookuptv'));
56
        if ($processTV <= 0) {
57
            return;
58
        }
59
60
        if ($mode === self::MODE_PARALLEL) {
61
            $this->processParallel($groupID, $guidChar, $processTV);
62
        } else {
63
            $this->processPipeline($groupID, $guidChar, $processTV);
64
        }
65
    }
66
67
    /**
68
     * Process releases through providers in parallel (all providers process all releases).
69
     * This is faster but uses more API calls. Compatible with Forking class.
70
     */
71
    private function processParallel(string $groupID, string $guidChar, int|string $processTV): void
72
    {
73
        // $this->displayHeaderParallel($guidChar);
74
75
        $providers = $this->getProviderPipeline();
76
        $totalTime = 0;
77
78
        foreach ($providers as $index => $provider) {
79
            $this->displayProviderHeader($provider['name'], $index + 1, count($providers));
80
81
            $startTime = microtime(true);
82
            $provider['instance']->processSite($groupID, $guidChar, $processTV);
83
            $elapsedTime = microtime(true) - $startTime;
84
            $totalTime += $elapsedTime;
85
86
            $this->displayProviderComplete($provider['name'], $elapsedTime);
87
        }
88
89
        // $this->displaySummaryParallel($totalTime);
90
    }
91
92
    /**
93
     * Process releases through providers in pipeline (sequential, each processes failures from previous).
94
     * This is more efficient and reduces API calls significantly.
95
     */
96
    private function processPipeline(string $groupID, string $guidChar, int|string $processTV): void
97
    {
98
        // $this->displayHeader($guidChar);
99
100
        // Pipeline: Process releases through each provider in sequence
101
        // Each provider only processes releases that failed in previous steps
102
        $providers = $this->getProviderPipeline();
103
104
        foreach ($providers as $index => $provider) {
105
            $this->displayProviderHeader($provider['name'], $index + 1, count($providers));
106
107
            $startTime = microtime(true);
108
            $provider['instance']->processSite($groupID, $guidChar, $processTV);
109
            $elapsedTime = microtime(true) - $startTime;
110
111
            $this->displayProviderComplete($provider['name'], $elapsedTime);
112
        }
113
114
        // $this->displaySummary();
115
    }
116
117
    /**
118
     * Get the provider pipeline in order of preference.
119
     */
120
    private function getProviderPipeline(): array
121
    {
122
        return [
123
            ['name' => 'Local DB', 'instance' => new LocalDB],
124
            ['name' => 'TVDB', 'instance' => new TVDB],
125
            ['name' => 'TVMaze', 'instance' => new TVMaze],
126
            ['name' => 'TMDB', 'instance' => new TMDB],
127
            ['name' => 'Trakt', 'instance' => new TraktTv],
128
        ];
129
    }
130
131
    /**
132
     * Display the processing header.
133
     */
134
    private function displayHeader(string $guidChar = ''): void
0 ignored issues
show
Unused Code introduced by
The method displayHeader() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
135
    {
136
        if (! $this->echooutput) {
137
            return;
138
        }
139
140
        echo "\n";
141
        $this->colorCli->headerOver('▶ TV Processing');
142
        if ($guidChar !== '') {
143
            $this->colorCli->primaryOver(' → ');
144
            $this->colorCli->headerOver('PIPELINE Mode');
145
            $this->colorCli->primaryOver(' → ');
146
            $this->colorCli->warningOver('Bucket: ');
147
            $this->colorCli->header(strtoupper($guidChar));
148
        } else {
149
            $this->colorCli->primaryOver(' → ');
150
            $this->colorCli->header('PIPELINE Mode');
151
        }
152
        echo "\n";
153
    }
154
155
    /**
156
     * Display the processing header for parallel mode.
157
     */
158
    private function displayHeaderParallel(string $guidChar = ''): void
0 ignored issues
show
Unused Code introduced by
The method displayHeaderParallel() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
159
    {
160
        if (! $this->echooutput) {
161
            return;
162
        }
163
164
        echo "\n";
165
        $this->colorCli->headerOver('▶ TV Processing');
166
        if ($guidChar !== '') {
167
            $this->colorCli->primaryOver(' → ');
168
            $this->colorCli->headerOver('PARALLEL Mode');
169
            $this->colorCli->primaryOver(' → ');
170
            $this->colorCli->warningOver('Bucket: ');
171
            $this->colorCli->header(strtoupper($guidChar));
172
        } else {
173
            $this->colorCli->primaryOver(' → ');
174
            $this->colorCli->header('PARALLEL Mode');
175
        }
176
        echo "\n";
177
    }
178
179
    /**
180
     * Display provider processing header.
181
     */
182
    private function displayProviderHeader(string $providerName, int $step, int $total): void
183
    {
184
        if (! $this->echooutput) {
185
            return;
186
        }
187
188
        echo "\n";
189
        $this->colorCli->primaryOver('  [');
190
        $this->colorCli->warningOver($step);
191
        $this->colorCli->primaryOver('/');
192
        $this->colorCli->warningOver($total);
193
        $this->colorCli->primaryOver('] ');
194
        $this->colorCli->headerOver('→ ');
195
        $this->colorCli->header($providerName);
196
    }
197
198
    /**
199
     * Display provider completion message.
200
     */
201
    private function displayProviderComplete(string $providerName, float $elapsedTime): void
202
    {
203
        if (! $this->echooutput) {
204
            return;
205
        }
206
207
        echo "\n";
208
        $this->colorCli->primaryOver('  ✓ ');
209
        $this->colorCli->primaryOver($providerName);
210
        $this->colorCli->primaryOver(' → ');
211
        $this->colorCli->alternateOver('Completed in ');
212
        $this->colorCli->warning(sprintf('%.2fs', $elapsedTime));
213
        echo "\n";
214
    }
215
216
    /**
217
     * Display final processing summary.
218
     */
219
    private function displaySummary(): void
0 ignored issues
show
Unused Code introduced by
The method displaySummary() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
220
    {
221
        if (! $this->echooutput) {
222
            return;
223
        }
224
225
        echo "\n";
226
        $this->colorCli->primaryOver('✓ Pipeline Complete');
227
        $this->colorCli->primaryOver(' → ');
228
        $this->colorCli->primary('Local DB → TVDB → TVMaze → TMDB → Trakt');
229
        echo "\n";
230
    }
231
232
    /**
233
     * Display final processing summary for parallel mode.
234
     */
235
    private function displaySummaryParallel(float $totalTime): void
0 ignored issues
show
Unused Code introduced by
The method displaySummaryParallel() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
236
    {
237
        if (! $this->echooutput) {
238
            return;
239
        }
240
241
        echo "\n";
242
        $this->colorCli->primaryOver('✓ Parallel Processing Complete');
243
        $this->colorCli->primaryOver(' → ');
244
        $this->colorCli->warningOver('Total: ');
245
        $this->colorCli->warning(sprintf('%.2fs', $totalTime));
246
        echo "\n";
247
    }
248
}
249