| Total Complexity | 44 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ReleasesFixNamesGroup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReleasesFixNamesGroup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ReleasesFixNamesGroup extends Command |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The name and signature of the console command. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $signature = 'releases:fix-names-group |
||
| 24 | {type : Type of fix (standard|predbft)} |
||
| 25 | {--guid-char= : GUID character to process (for standard type)} |
||
| 26 | {--limit=1000 : Maximum releases to process} |
||
| 27 | {--thread=1 : Thread number (for predbft type)}'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The console command description. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $description = 'Fix release names using various methods (group-based processing)'; |
||
| 35 | |||
| 36 | private NameFixer $nameFixer; |
||
| 37 | |||
| 38 | private int $checked = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Execute the console command. |
||
| 42 | */ |
||
| 43 | public function handle(): int |
||
| 44 | { |
||
| 45 | $type = $this->argument('type'); |
||
| 46 | $maxPerRun = (int) $this->option('limit'); |
||
| 47 | |||
| 48 | $this->nameFixer = new NameFixer; |
||
| 49 | |||
| 50 | switch ($type) { |
||
| 51 | case 'standard': |
||
| 52 | return $this->processStandard($maxPerRun); |
||
| 53 | |||
| 54 | case 'predbft': |
||
| 55 | return $this->processPredbFulltext($maxPerRun); |
||
| 56 | |||
| 57 | default: |
||
| 58 | $this->error("Invalid type: {$type}. Use 'standard' or 'predbft'"); |
||
| 59 | |||
| 60 | return Command::FAILURE; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Process standard name fixing |
||
| 66 | */ |
||
| 67 | protected function processStandard(int $maxPerRun): int |
||
| 68 | { |
||
| 69 | $guidChar = $this->option('guid-char'); |
||
| 70 | |||
| 71 | if ($guidChar === null) { |
||
|
|
|||
| 72 | $this->error('--guid-char is required for standard type'); |
||
| 73 | |||
| 74 | return Command::FAILURE; |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->info("Processing releases with GUID starting with: {$guidChar}"); |
||
| 78 | $this->info("Maximum per run: {$maxPerRun}"); |
||
| 79 | |||
| 80 | // Allow for larger filename return sets |
||
| 81 | DB::statement('SET SESSION group_concat_max_len = 65536'); |
||
| 82 | |||
| 83 | // Find releases to process |
||
| 84 | $releases = $this->fetchReleases($guidChar, $maxPerRun); |
||
| 85 | |||
| 86 | if ($releases->isEmpty()) { |
||
| 87 | $this->info('No releases to process'); |
||
| 88 | |||
| 89 | return Command::SUCCESS; |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->info("Found {$releases->count()} releases to process"); |
||
| 93 | $bar = $this->output->createProgressBar($releases->count()); |
||
| 94 | $bar->start(); |
||
| 95 | |||
| 96 | $nntp = null; |
||
| 97 | $nzbcontents = null; |
||
| 98 | |||
| 99 | foreach ($releases as $release) { |
||
| 100 | $this->checked++; |
||
| 101 | $this->nameFixer->reset(); |
||
| 102 | |||
| 103 | // Process UID |
||
| 104 | if ((int) $release->proc_uid === NameFixer::PROC_UID_NONE && |
||
| 105 | (! empty($release->uid) || ! empty($release->mediainfo))) { |
||
| 106 | |||
| 107 | if (! empty($release->uid)) { |
||
| 108 | $this->nameFixer->checkName($release, true, 'UID, ', 1, true); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (empty($this->nameFixer->matched) && ! empty($release->mediainfo)) { |
||
| 112 | $this->nameFixer->checkName($release, true, 'Mediainfo, ', 1, true); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->nameFixer->_updateSingleColumn('proc_uid', NameFixer::PROC_UID_DONE, $release->releases_id); |
||
| 117 | |||
| 118 | if ($this->nameFixer->matched) { |
||
| 119 | $bar->advance(); |
||
| 120 | |||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | // Process CRC32 |
||
| 125 | if ((int) $release->proc_crc32 === NameFixer::PROC_CRC_NONE && ! empty($release->crc)) { |
||
| 126 | $this->nameFixer->reset(); |
||
| 127 | $this->nameFixer->checkName($release, true, 'CRC32, ', 1, true); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->nameFixer->_updateSingleColumn('proc_crc32', NameFixer::PROC_CRC_DONE, $release->releases_id); |
||
| 131 | |||
| 132 | if ($this->nameFixer->matched) { |
||
| 133 | $bar->advance(); |
||
| 134 | |||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | // Process SRR |
||
| 139 | if ((int) $release->proc_srr === NameFixer::PROC_SRR_NONE) { |
||
| 140 | $this->nameFixer->reset(); |
||
| 141 | $this->nameFixer->checkName($release, true, 'SRR, ', 1, true); |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->nameFixer->_updateSingleColumn('proc_srr', NameFixer::PROC_SRR_DONE, $release->releases_id); |
||
| 145 | |||
| 146 | if ($this->nameFixer->matched) { |
||
| 147 | $bar->advance(); |
||
| 148 | |||
| 149 | continue; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Process PAR2 hash |
||
| 153 | if ((int) $release->proc_hash16k === NameFixer::PROC_HASH16K_NONE && ! empty($release->hash)) { |
||
| 154 | $this->nameFixer->reset(); |
||
| 155 | $this->nameFixer->checkName($release, true, 'PAR2 hash, ', 1, true); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->nameFixer->_updateSingleColumn('proc_hash16k', NameFixer::PROC_HASH16K_DONE, $release->releases_id); |
||
| 159 | |||
| 160 | if ($this->nameFixer->matched) { |
||
| 161 | $bar->advance(); |
||
| 162 | |||
| 163 | continue; |
||
| 164 | } |
||
| 165 | |||
| 166 | // Process NFO |
||
| 167 | if ((int) $release->nfostatus === Nfo::NFO_FOUND && |
||
| 168 | (int) $release->proc_nfo === NameFixer::PROC_NFO_NONE && |
||
| 169 | ! empty($release->textstring) && |
||
| 170 | ! preg_match('/^=newz\[NZB\]=\w+/', $release->textstring)) { |
||
| 171 | |||
| 172 | $this->nameFixer->reset(); |
||
| 173 | $this->nameFixer->checkName($release, true, 'NFO, ', 1, true); |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->nameFixer->_updateSingleColumn('proc_nfo', NameFixer::PROC_NFO_DONE, $release->releases_id); |
||
| 177 | |||
| 178 | if ($this->nameFixer->matched) { |
||
| 179 | $bar->advance(); |
||
| 180 | |||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Process filenames |
||
| 185 | if ((int) $release->fileid > 0 && (int) $release->proc_files === NameFixer::PROC_FILES_NONE) { |
||
| 186 | $this->nameFixer->reset(); |
||
| 187 | $fileNames = explode('|', $release->filestring); |
||
| 188 | |||
| 189 | if (is_array($fileNames)) { |
||
| 190 | $releaseFile = $release; |
||
| 191 | foreach ($fileNames as $fileName) { |
||
| 192 | if ($this->nameFixer->matched === false) { |
||
| 193 | $releaseFile->textstring = $fileName; |
||
| 194 | $this->nameFixer->checkName($releaseFile, true, 'Filenames, ', 1, true); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | $this->nameFixer->_updateSingleColumn('proc_files', NameFixer::PROC_FILES_DONE, $release->releases_id); |
||
| 201 | |||
| 202 | if ($this->nameFixer->matched) { |
||
| 203 | $bar->advance(); |
||
| 204 | |||
| 205 | continue; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Process PAR2 |
||
| 209 | if ((int) $release->proc_par2 === NameFixer::PROC_PAR2_NONE) { |
||
| 210 | // Initialize NZB contents if needed |
||
| 211 | if (! isset($nzbcontents)) { |
||
| 212 | $nntp = new NNTP; |
||
| 213 | $compressedHeaders = config('nntmux_nntp.compressed_headers'); |
||
| 214 | |||
| 215 | if ((config('nntmux_nntp.use_alternate_nntp_server') === true |
||
| 216 | ? $nntp->doConnect($compressedHeaders, true) |
||
| 217 | : $nntp->doConnect()) !== true) { |
||
| 218 | $this->warn('Unable to connect to usenet for PAR2 processing'); |
||
| 219 | } else { |
||
| 220 | $Nfo = new Nfo; |
||
| 221 | $nzbcontents = new NZBContents([ |
||
| 222 | 'Echo' => false, |
||
| 223 | 'NNTP' => $nntp, |
||
| 224 | 'Nfo' => $Nfo, |
||
| 225 | 'PostProcess' => new PostProcess(['Nfo' => $Nfo, 'NameFixer' => $this->nameFixer]), |
||
| 226 | ]); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | if (isset($nzbcontents)) { |
||
| 231 | $nzbcontents->checkPAR2($release->guid, $release->releases_id, $release->groups_id, 1, true); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | $this->nameFixer->_updateSingleColumn('proc_par2', NameFixer::PROC_PAR2_DONE, $release->releases_id); |
||
| 236 | |||
| 237 | $bar->advance(); |
||
| 238 | } |
||
| 239 | |||
| 240 | $bar->finish(); |
||
| 241 | $this->newLine(2); |
||
| 242 | |||
| 243 | $this->info("✅ Processed {$this->checked} releases"); |
||
| 244 | $this->info("✅ Fixed {$this->nameFixer->fixed} release names"); |
||
| 245 | |||
| 246 | return Command::SUCCESS; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Process PreDB fulltext matching |
||
| 251 | */ |
||
| 252 | protected function processPredbFulltext(int $maxPerRun): int |
||
| 253 | { |
||
| 254 | $thread = (int) $this->option('thread'); |
||
| 255 | $offset = $thread * $maxPerRun - $maxPerRun; |
||
| 256 | |||
| 257 | $this->info('Processing PreDB fulltext matching'); |
||
| 258 | $this->info("Thread: {$thread}, Limit: {$maxPerRun}, Offset: {$offset}"); |
||
| 259 | |||
| 260 | $pres = Predb::fromQuery( |
||
| 261 | sprintf( |
||
| 262 | ' |
||
| 263 | SELECT p.id AS predb_id, p.title, p.source, p.searched |
||
| 264 | FROM predb p |
||
| 265 | WHERE LENGTH(p.title) >= 15 AND p.title NOT REGEXP "[\"\<\> ]" |
||
| 266 | AND p.searched = 0 |
||
| 267 | AND p.predate < (NOW() - INTERVAL 1 DAY) |
||
| 268 | ORDER BY p.predate ASC |
||
| 269 | LIMIT %s |
||
| 270 | OFFSET %s', |
||
| 271 | $maxPerRun, |
||
| 272 | $offset |
||
| 273 | ) |
||
| 274 | ); |
||
| 275 | |||
| 276 | if ($pres->isEmpty()) { |
||
| 277 | $this->info('No PreDB entries to process'); |
||
| 278 | |||
| 279 | return Command::SUCCESS; |
||
| 280 | } |
||
| 281 | |||
| 282 | $this->info("Found {$pres->count()} PreDB entries to process"); |
||
| 283 | $bar = $this->output->createProgressBar($pres->count()); |
||
| 284 | $bar->start(); |
||
| 285 | |||
| 286 | foreach ($pres as $pre) { |
||
| 287 | $this->nameFixer->done = $this->nameFixer->matched = false; |
||
| 288 | $searched = 0; |
||
| 289 | |||
| 290 | $ftmatched = $this->nameFixer->matchPredbFT($pre, true, 1, true); |
||
| 291 | |||
| 292 | if ($ftmatched > 0) { |
||
| 293 | $searched = 1; |
||
| 294 | } elseif ($ftmatched < 0) { |
||
| 295 | $searched = -6; |
||
| 296 | } else { |
||
| 297 | $searched = $pre['searched'] - 1; |
||
| 298 | } |
||
| 299 | |||
| 300 | Predb::query()->where('id', $pre['predb_id'])->update(['searched' => $searched]); |
||
| 301 | $this->checked++; |
||
| 302 | |||
| 303 | $bar->advance(); |
||
| 304 | } |
||
| 305 | |||
| 306 | $bar->finish(); |
||
| 307 | $this->newLine(2); |
||
| 308 | |||
| 309 | $this->info("✅ Processed {$this->checked} PreDB entries"); |
||
| 310 | |||
| 311 | return Command::SUCCESS; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Fetch releases for processing |
||
| 316 | */ |
||
| 317 | protected function fetchReleases(string $guidChar, int $maxPerRun) |
||
| 367 | )); |
||
| 368 | } |
||
| 369 | } |
||
| 370 |