| Total Complexity | 48 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ReleaseUpdateService 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 ReleaseUpdateService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ReleaseUpdateService |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * PreDB regex pattern for scene release names. |
||
| 28 | */ |
||
| 29 | public const PREDB_REGEX = '/([\w.\'()\[\]-]+(?:[\s._-]+[\w.\'()\[\]-]+)+[-.][\w]+)/ui'; |
||
| 30 | |||
| 31 | // Constants for name fixing status |
||
| 32 | public const PROC_NFO_NONE = 0; |
||
| 33 | public const PROC_NFO_DONE = 1; |
||
| 34 | public const PROC_FILES_NONE = 0; |
||
| 35 | public const PROC_FILES_DONE = 1; |
||
| 36 | public const PROC_PAR2_NONE = 0; |
||
| 37 | public const PROC_PAR2_DONE = 1; |
||
| 38 | public const PROC_UID_NONE = 0; |
||
| 39 | public const PROC_UID_DONE = 1; |
||
| 40 | public const PROC_HASH16K_NONE = 0; |
||
| 41 | public const PROC_HASH16K_DONE = 1; |
||
| 42 | public const PROC_SRR_NONE = 0; |
||
| 43 | public const PROC_SRR_DONE = 1; |
||
| 44 | public const PROC_CRC_NONE = 0; |
||
| 45 | public const PROC_CRC_DONE = 1; |
||
| 46 | |||
| 47 | // Constants for overall rename status |
||
| 48 | public const IS_RENAMED_NONE = 0; |
||
| 49 | public const IS_RENAMED_DONE = 1; |
||
| 50 | |||
| 51 | protected CategorizationService $category; |
||
| 52 | protected ManticoreSearchService $manticore; |
||
| 53 | protected ElasticSearchService $elasticsearch; |
||
| 54 | protected FileNameCleaner $fileNameCleaner; |
||
| 55 | protected ColorCLI $colorCLI; |
||
| 56 | protected bool $echoOutput; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The release ID we are trying to rename. |
||
| 60 | */ |
||
| 61 | protected int $relid = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Has the current release found a new name? |
||
| 65 | */ |
||
| 66 | public bool $matched = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Was the check completed? |
||
| 70 | */ |
||
| 71 | public bool $done = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * How many releases have got a new name? |
||
| 75 | */ |
||
| 76 | public int $fixed = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * How many releases were checked. |
||
| 80 | */ |
||
| 81 | public int $checked = 0; |
||
| 82 | |||
| 83 | public function __construct( |
||
| 84 | ?CategorizationService $category = null, |
||
| 85 | ?ManticoreSearchService $manticore = null, |
||
| 86 | ?ElasticSearchService $elasticsearch = null, |
||
| 87 | ?FileNameCleaner $fileNameCleaner = null, |
||
| 88 | ?ColorCLI $colorCLI = null |
||
| 89 | ) { |
||
| 90 | $this->category = $category ?? new CategorizationService(); |
||
| 91 | $this->manticore = $manticore ?? app(ManticoreSearchService::class); |
||
| 92 | $this->elasticsearch = $elasticsearch ?? app(ElasticSearchService::class); |
||
| 93 | $this->fileNameCleaner = $fileNameCleaner ?? new FileNameCleaner(); |
||
| 94 | $this->colorCLI = $colorCLI ?? new ColorCLI(); |
||
| 95 | $this->echoOutput = config('nntmux.echocli'); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Update the release with the new information. |
||
| 100 | * |
||
| 101 | * @param object|array $release The release to update |
||
| 102 | * @param string $name The new name |
||
| 103 | * @param string $method The method that found the name |
||
| 104 | * @param bool $echo Whether to actually update the database |
||
| 105 | * @param string $type The type string for logging |
||
| 106 | * @param bool $nameStatus Whether to update status columns |
||
| 107 | * @param bool $show Whether to show output |
||
| 108 | * @param int|null $preId PreDB ID if matched |
||
| 109 | * @throws \Exception |
||
| 110 | */ |
||
| 111 | public function updateRelease( |
||
| 112 | object|array $release, |
||
| 113 | string $name, |
||
| 114 | string $method, |
||
| 115 | bool $echo, |
||
| 116 | string $type, |
||
| 117 | bool $nameStatus, |
||
| 118 | bool $show, |
||
| 119 | ?int $preId = 0 |
||
| 120 | ): void { |
||
| 121 | $preId = $preId ?? 0; |
||
| 122 | if (is_array($release)) { |
||
|
|
|||
| 123 | $release = (object) $release; |
||
| 124 | } |
||
| 125 | |||
| 126 | // If $release does not have a releases_id, we should add it. |
||
| 127 | if (!isset($release->releases_id)) { |
||
| 128 | $release->releases_id = $release->id; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($this->relid !== $release->releases_id) { |
||
| 132 | $newName = (new ReleaseCleaning())->fixerCleaner($name); |
||
| 133 | // Normalize and sanity-check candidate for non-trusted sources |
||
| 134 | $newName = $this->fileNameCleaner->normalizeCandidateTitle($newName); |
||
| 135 | |||
| 136 | // Determine if the source is trusted enough to bypass plausibility checks |
||
| 137 | $trustedSource = $this->isTrustedSource($type, $method, $preId); |
||
| 138 | |||
| 139 | if (!$trustedSource && !$this->fileNameCleaner->isPlausibleReleaseTitle($newName)) { |
||
| 140 | // Skip low-quality rename candidates for untrusted sources |
||
| 141 | $this->done = true; |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (strtolower($newName) !== strtolower($release->searchname)) { |
||
| 146 | $this->matched = true; |
||
| 147 | $this->relid = (int) $release->releases_id; |
||
| 148 | |||
| 149 | $determinedCategory = $this->category->determineCategory( |
||
| 150 | $release->groups_id, |
||
| 151 | $newName, |
||
| 152 | !empty($release->fromname) ? $release->fromname : '' |
||
| 153 | ); |
||
| 154 | |||
| 155 | if ($type === 'PAR2, ') { |
||
| 156 | $newName = ucwords($newName); |
||
| 157 | if (preg_match('/(.+?)\.[a-z0-9]{2,3}(PAR2)?$/i', $name, $hit)) { |
||
| 158 | $newName = $hit[1]; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | $this->fixed++; |
||
| 163 | |||
| 164 | // Split on path separator backslash to strip any path |
||
| 165 | $newName = explode('\\', $newName); |
||
| 166 | $newName = preg_replace(['/^[=_.:\s-]+/', '/[=_.:\s-]+$/'], '', $newName[0]); |
||
| 167 | |||
| 168 | if ($this->echoOutput && $show) { |
||
| 169 | $this->echoReleaseInfo($release, $newName, $determinedCategory, $type, $method); |
||
| 170 | } |
||
| 171 | |||
| 172 | $newTitle = substr($newName, 0, 299); |
||
| 173 | |||
| 174 | if ($echo === true) { |
||
| 175 | $this->performDatabaseUpdate($release, $newTitle, $determinedCategory, $type, $nameStatus, $preId); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | $this->done = true; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if the source is trusted enough to bypass plausibility checks. |
||
| 184 | */ |
||
| 185 | protected function isTrustedSource(string $type, string $method, int $preId): bool |
||
| 186 | { |
||
| 187 | return ( |
||
| 188 | (!empty($preId) && $preId > 0) || |
||
| 189 | str_starts_with($type, 'PreDB') || |
||
| 190 | str_starts_with($type, 'PreDb') || |
||
| 191 | $type === 'UID, ' || |
||
| 192 | $type === 'PAR2 hash, ' || |
||
| 193 | $type === 'CRC32, ' || |
||
| 194 | $type === 'SRR, ' || |
||
| 195 | stripos($method, 'Title Match') !== false || |
||
| 196 | stripos($method, 'file matched source') !== false || |
||
| 197 | stripos($method, 'PreDb') !== false || |
||
| 198 | stripos($method, 'preDB') !== false |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Echo release information to CLI. |
||
| 204 | */ |
||
| 205 | public function echoReleaseInfo( |
||
| 206 | object $release, |
||
| 207 | string $newName, |
||
| 208 | array $determinedCategory, |
||
| 209 | string $type, |
||
| 210 | string $method |
||
| 211 | ): void { |
||
| 212 | $groupName = UsenetGroup::getNameByID($release->groups_id); |
||
| 213 | $oldCatName = Category::getNameByID($release->categories_id); |
||
| 214 | $newCatName = Category::getNameByID($determinedCategory['categories_id']); |
||
| 215 | |||
| 216 | if ($type === 'PAR2, ') { |
||
| 217 | echo PHP_EOL; |
||
| 218 | } |
||
| 219 | |||
| 220 | echo PHP_EOL; |
||
| 221 | |||
| 222 | $this->colorCLI->primary('Release Information:'); |
||
| 223 | |||
| 224 | echo ' ' . $this->colorCLI->headerOver('New name: ') . $this->colorCLI->primary(substr($newName, 0, 100)) . PHP_EOL; |
||
| 225 | echo ' ' . $this->colorCLI->headerOver('Old name: ') . $this->colorCLI->primary(substr((string) $release->searchname, 0, 100)) . PHP_EOL; |
||
| 226 | echo ' ' . $this->colorCLI->headerOver('Use name: ') . $this->colorCLI->primary(substr((string) $release->name, 0, 100)) . PHP_EOL; |
||
| 227 | echo PHP_EOL; |
||
| 228 | |||
| 229 | echo ' ' . $this->colorCLI->headerOver('New cat: ') . $this->colorCLI->primary($newCatName) . PHP_EOL; |
||
| 230 | echo ' ' . $this->colorCLI->headerOver('Old cat: ') . $this->colorCLI->primary($oldCatName) . PHP_EOL; |
||
| 231 | echo ' ' . $this->colorCLI->headerOver('Group: ') . $this->colorCLI->primary($groupName) . PHP_EOL; |
||
| 232 | echo PHP_EOL; |
||
| 233 | |||
| 234 | echo ' ' . $this->colorCLI->headerOver('Method: ') . $this->colorCLI->primary($type . $method) . PHP_EOL; |
||
| 235 | echo ' ' . $this->colorCLI->headerOver('Release ID: ') . $this->colorCLI->primary((string) $release->releases_id) . PHP_EOL; |
||
| 236 | |||
| 237 | if (!empty($release->filename)) { |
||
| 238 | echo ' ' . $this->colorCLI->headerOver('Filename: ') . $this->colorCLI->primary(substr((string) $release->filename, 0, 100)) . PHP_EOL; |
||
| 239 | } |
||
| 240 | |||
| 241 | if ($type !== 'PAR2, ') { |
||
| 242 | echo PHP_EOL; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Perform the actual database update. |
||
| 248 | */ |
||
| 249 | protected function performDatabaseUpdate( |
||
| 250 | object $release, |
||
| 251 | string $newTitle, |
||
| 252 | array $determinedCategory, |
||
| 253 | string $type, |
||
| 254 | bool $nameStatus, |
||
| 255 | int $preId |
||
| 256 | ): void { |
||
| 257 | if ($nameStatus === true) { |
||
| 258 | $status = $this->getStatusColumnsForType($type); |
||
| 259 | |||
| 260 | $updateColumns = [ |
||
| 261 | 'videos_id' => 0, |
||
| 262 | 'tv_episodes_id' => 0, |
||
| 263 | 'imdbid' => '', |
||
| 264 | 'musicinfo_id' => '', |
||
| 265 | 'consoleinfo_id' => '', |
||
| 266 | 'bookinfo_id' => '', |
||
| 267 | 'anidbid' => '', |
||
| 268 | 'predb_id' => $preId, |
||
| 269 | 'searchname' => $newTitle, |
||
| 270 | 'categories_id' => $determinedCategory['categories_id'], |
||
| 271 | ]; |
||
| 272 | |||
| 273 | if (!empty($status)) { |
||
| 274 | foreach ($status as $key => $stat) { |
||
| 275 | $updateColumns = Arr::add($updateColumns, $key, $stat); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | Release::query() |
||
| 280 | ->where('id', $release->releases_id) |
||
| 281 | ->update($updateColumns); |
||
| 282 | } else { |
||
| 283 | Release::query() |
||
| 284 | ->where('id', $release->releases_id) |
||
| 285 | ->update([ |
||
| 286 | 'videos_id' => 0, |
||
| 287 | 'tv_episodes_id' => 0, |
||
| 288 | 'imdbid' => null, |
||
| 289 | 'musicinfo_id' => null, |
||
| 290 | 'consoleinfo_id' => null, |
||
| 291 | 'bookinfo_id' => null, |
||
| 292 | 'anidbid' => null, |
||
| 293 | 'predb_id' => $preId, |
||
| 294 | 'searchname' => $newTitle, |
||
| 295 | 'categories_id' => $determinedCategory['categories_id'], |
||
| 296 | 'iscategorized' => 1, |
||
| 297 | ]); |
||
| 298 | } |
||
| 299 | |||
| 300 | // Update search index |
||
| 301 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
| 302 | $this->elasticsearch->updateRelease($release->releases_id); |
||
| 303 | } else { |
||
| 304 | $this->manticore->updateRelease($release->releases_id); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the status columns to update for a given type. |
||
| 310 | */ |
||
| 311 | protected function getStatusColumnsForType(string $type): array |
||
| 312 | { |
||
| 313 | return match ($type) { |
||
| 314 | 'NFO, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_nfo' => 1], |
||
| 315 | 'PAR2, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_par2' => 1], |
||
| 316 | 'Filenames, ', 'file matched source: ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_files' => 1], |
||
| 317 | 'SHA1, ', 'MD5, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'dehashstatus' => 1], |
||
| 318 | 'PreDB FT Exact, ' => ['isrenamed' => 1, 'iscategorized' => 1], |
||
| 319 | 'sorter, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_sorter' => 1], |
||
| 320 | 'UID, ', 'Mediainfo, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_uid' => 1], |
||
| 321 | 'PAR2 hash, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_hash16k' => 1], |
||
| 322 | 'SRR, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_srr' => 1], |
||
| 323 | 'CRC32, ' => ['isrenamed' => 1, 'iscategorized' => 1, 'proc_crc32' => 1], |
||
| 324 | default => [], |
||
| 325 | }; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Update a single column in releases. |
||
| 330 | */ |
||
| 331 | public function updateSingleColumn(string $column, int $status, int $id): void |
||
| 332 | { |
||
| 333 | if ($column !== '' && $id !== 0) { |
||
| 334 | Release::query()->where('id', $id)->update([$column => $status]); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Check if a release matches a PreDB entry. |
||
| 340 | */ |
||
| 341 | public function checkPreDbMatch(object $release, string $textstring): ?array |
||
| 342 | { |
||
| 343 | if (preg_match_all(self::PREDB_REGEX, $textstring, $hits) && !preg_match('/Source\s\:/i', $textstring)) { |
||
| 344 | foreach ($hits as $hit) { |
||
| 345 | foreach ($hit as $val) { |
||
| 346 | $title = Predb::query()->where('title', trim($val))->select(['title', 'id'])->first(); |
||
| 347 | if ($title !== null) { |
||
| 348 | return ['title' => $title['title'], 'id' => $title['id']]; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | return null; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Reset status variables for new processing. |
||
| 358 | */ |
||
| 359 | public function reset(): void |
||
| 360 | { |
||
| 361 | $this->done = $this->matched = false; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Increment the checked counter. |
||
| 366 | */ |
||
| 367 | public function incrementChecked(): void |
||
| 368 | { |
||
| 369 | $this->checked++; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the current statistics. |
||
| 374 | */ |
||
| 375 | public function getStats(): array |
||
| 382 | ]; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 |