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