| Total Complexity | 98 |
| Total Lines | 561 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DeleteReleases 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 DeleteReleases, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class DeleteReleases extends Command |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The name and signature of the console command. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $signature = 'nntmux:delete-releases |
||
| 17 | {criteria?* : Advanced criteria in original format} |
||
| 18 | {--group= : Group name to filter by} |
||
| 19 | {--group-like= : Group name pattern to filter by (partial match)} |
||
| 20 | {--poster= : Poster name (fromname) to filter by} |
||
| 21 | {--poster-like= : Poster name pattern to filter by (partial match)} |
||
| 22 | {--name= : Release name to filter by} |
||
| 23 | {--name-like= : Release name pattern to filter by (partial match)} |
||
| 24 | {--search= : Search name to filter by} |
||
| 25 | {--search-like= : Search name pattern to filter by (partial match)} |
||
| 26 | {--category= : Category ID to filter by} |
||
| 27 | {--guid= : Specific GUID to filter by} |
||
| 28 | {--size-min= : Minimum size in bytes} |
||
| 29 | {--size-max= : Maximum size in bytes} |
||
| 30 | {--size= : Exact size in bytes} |
||
| 31 | {--hours-old= : Delete releases older than X hours} |
||
| 32 | {--hours-new= : Delete releases newer than X hours} |
||
| 33 | {--parts-min= : Minimum number of parts} |
||
| 34 | {--parts-max= : Maximum number of parts} |
||
| 35 | {--parts= : Exact number of parts} |
||
| 36 | {--completion-max= : Maximum completion percentage} |
||
| 37 | {--nzb-status= : NZB status to filter by} |
||
| 38 | {--imdb= : IMDB ID to filter by (use NULL for no IMDB)} |
||
| 39 | {--rage= : Rage ID to filter by} |
||
| 40 | {--dry-run : Show what would be deleted without actually deleting} |
||
| 41 | {--force : Skip confirmation prompt}'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The console command description. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $description = 'Delete releases based on specified criteria'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Create a new command instance. |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 54 | { |
||
| 55 | parent::__construct(); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Execute the console command. |
||
| 60 | */ |
||
| 61 | public function handle(): int |
||
| 62 | { |
||
| 63 | $dryRun = $this->option('dry-run'); |
||
| 64 | $force = $this->option('force'); |
||
| 65 | |||
| 66 | // Build criteria array from simple options |
||
| 67 | $criteria = $this->buildCriteriaFromOptions(); |
||
| 68 | |||
| 69 | // Add any advanced criteria |
||
| 70 | $advancedCriteria = $this->argument('criteria'); |
||
| 71 | if (! empty($advancedCriteria)) { |
||
| 72 | $criteria = array_merge($criteria, $advancedCriteria); |
||
|
|
|||
| 73 | } |
||
| 74 | |||
| 75 | if (empty($criteria)) { |
||
| 76 | $this->showUsage(); |
||
| 77 | |||
| 78 | return 1; |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->info('Delete Releases Command'); |
||
| 82 | $this->newLine(); |
||
| 83 | |||
| 84 | if ($dryRun) { |
||
| 85 | $this->warn('DRY RUN MODE - No releases will actually be deleted'); |
||
| 86 | $this->newLine(); |
||
| 87 | |||
| 88 | return $this->performDryRun($criteria); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Show confirmation unless force flag is set |
||
| 92 | if (! $force) { |
||
| 93 | $this->warn('You are about to delete releases matching the following criteria:'); |
||
| 94 | foreach ($criteria as $criterion) { |
||
| 95 | $this->line(" - {$criterion}"); |
||
| 96 | } |
||
| 97 | $this->newLine(); |
||
| 98 | |||
| 99 | if (! $this->confirm('Are you sure you want to proceed with the deletion?')) { |
||
| 100 | $this->info('Operation cancelled.'); |
||
| 101 | |||
| 102 | return 0; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | try { |
||
| 107 | $this->info('Starting release deletion process...'); |
||
| 108 | |||
| 109 | // Build the query to find releases to delete |
||
| 110 | $query = $this->buildQueryFromCriteria($criteria); |
||
| 111 | if (! $query) { |
||
| 112 | $this->error('Could not build query from criteria.'); |
||
| 113 | |||
| 114 | return 1; |
||
| 115 | } |
||
| 116 | |||
| 117 | // Get releases to delete |
||
| 118 | $releases = DB::select($query); |
||
| 119 | if (empty($releases)) { |
||
| 120 | $this->info('No releases found matching the specified criteria.'); |
||
| 121 | |||
| 122 | return 0; |
||
| 123 | } |
||
| 124 | |||
| 125 | $count = count($releases); |
||
| 126 | $this->info("Found {$count} release(s) to delete..."); |
||
| 127 | |||
| 128 | $deleted = 0; |
||
| 129 | foreach ($releases as $release) { |
||
| 130 | try { |
||
| 131 | // Delete the release using direct database operations |
||
| 132 | $releaseId = $release->id; |
||
| 133 | |||
| 134 | $releaseName = $release->searchname ?: ($release->name ?: 'Unknown'); |
||
| 135 | $this->line("Deleting: {$releaseName}"); |
||
| 136 | |||
| 137 | // Delete related data first (to maintain referential integrity) |
||
| 138 | DB::delete('DELETE FROM user_downloads WHERE releases_id = ?', [$releaseId]); |
||
| 139 | DB::delete('DELETE FROM users_releases WHERE releases_id = ?', [$releaseId]); |
||
| 140 | DB::delete('DELETE FROM release_files WHERE releases_id = ?', [$releaseId]); |
||
| 141 | DB::delete('DELETE FROM release_comments WHERE releases_id = ?', [$releaseId]); |
||
| 142 | DB::delete('DELETE FROM release_nfos WHERE releases_id = ?', [$releaseId]); |
||
| 143 | DB::delete('DELETE FROM release_subtitles WHERE releases_id = ?', [$releaseId]); |
||
| 144 | |||
| 145 | // Delete the main release record |
||
| 146 | DB::delete('DELETE FROM releases WHERE id = ?', [$releaseId]); |
||
| 147 | |||
| 148 | // Delete from search indexes |
||
| 149 | Search::deleteRelease($releaseId); |
||
| 150 | |||
| 151 | $deleted++; |
||
| 152 | |||
| 153 | // Show progress for large deletions |
||
| 154 | if ($deleted % 100 == 0) { |
||
| 155 | $this->info("Deleted {$deleted}/{$count} releases..."); |
||
| 156 | } |
||
| 157 | |||
| 158 | } catch (\Exception $e) { |
||
| 159 | $releaseName = $release->searchname ?? 'Unknown'; |
||
| 160 | $this->warn("Failed to delete release {$releaseName}: ".$e->getMessage()); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->info("Successfully deleted {$deleted} release(s)."); |
||
| 165 | |||
| 166 | return 0; |
||
| 167 | |||
| 168 | } catch (\Exception $e) { |
||
| 169 | $this->error('An error occurred: '.$e->getMessage()); |
||
| 170 | |||
| 171 | return 1; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Perform a dry run to show what releases would be deleted. |
||
| 177 | */ |
||
| 178 | protected function performDryRun(array $criteria): int |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Build the SQL query from criteria array. |
||
| 237 | */ |
||
| 238 | protected function buildQueryFromCriteria(array $criteria): ?string |
||
| 239 | { |
||
| 240 | // Start with base query |
||
| 241 | $query = 'SELECT id, guid, searchname, name FROM releases WHERE 1=1'; |
||
| 242 | |||
| 243 | foreach ($criteria as $criterion) { |
||
| 244 | if ($criterion === 'ignore') { |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | |||
| 248 | $queryPart = $this->formatCriterionToSql($criterion); |
||
| 249 | if ($queryPart) { |
||
| 250 | $query .= $queryPart; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | return $this->cleanSpaces($query); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Convert a single criterion to SQL WHERE clause. |
||
| 259 | */ |
||
| 260 | protected function formatCriterionToSql(string $criterion): ?string |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Clean multiple spaces from a string. |
||
| 391 | */ |
||
| 392 | protected function cleanSpaces(string $string): string |
||
| 393 | { |
||
| 394 | return preg_replace('/\s+/', ' ', trim($string)); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Build criteria array from simple command options. |
||
| 399 | */ |
||
| 400 | protected function buildCriteriaFromOptions(): array |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Show usage information for the command. |
||
| 501 | */ |
||
| 502 | protected function showUsage(): void |
||
| 572 |