Total Complexity | 98 |
Total Lines | 562 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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() |
||
55 | { |
||
56 | parent::__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 | ManticoreSearch::deleteRelease($releaseId); |
||
151 | ElasticSearchSiteSearch::deleteRelease($releaseId); |
||
152 | |||
153 | $deleted++; |
||
154 | |||
155 | // Show progress for large deletions |
||
156 | if ($deleted % 100 == 0) { |
||
157 | $this->info("Deleted {$deleted}/{$count} releases..."); |
||
158 | } |
||
159 | |||
160 | } catch (\Exception $e) { |
||
161 | $releaseName = $release->searchname ?? 'Unknown'; |
||
162 | $this->warn("Failed to delete release {$releaseName}: ".$e->getMessage()); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | $this->info("Successfully deleted {$deleted} release(s)."); |
||
167 | |||
168 | return 0; |
||
169 | |||
170 | } catch (\Exception $e) { |
||
171 | $this->error('An error occurred: '.$e->getMessage()); |
||
172 | |||
173 | return 1; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Perform a dry run to show what releases would be deleted. |
||
179 | */ |
||
180 | protected function performDryRun(array $criteria): int |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Build the SQL query from criteria array. |
||
239 | */ |
||
240 | protected function buildQueryFromCriteria(array $criteria): ?string |
||
241 | { |
||
242 | // Start with base query |
||
243 | $query = 'SELECT id, guid, searchname, name FROM releases WHERE 1=1'; |
||
244 | |||
245 | foreach ($criteria as $criterion) { |
||
246 | if ($criterion === 'ignore') { |
||
247 | continue; |
||
248 | } |
||
249 | |||
250 | $queryPart = $this->formatCriterionToSql($criterion); |
||
251 | if ($queryPart) { |
||
252 | $query .= $queryPart; |
||
253 | } |
||
254 | } |
||
255 | |||
256 | return $this->cleanSpaces($query); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Convert a single criterion to SQL WHERE clause. |
||
261 | */ |
||
262 | protected function formatCriterionToSql(string $criterion): ?string |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Clean multiple spaces from a string. |
||
393 | */ |
||
394 | protected function cleanSpaces(string $string): string |
||
395 | { |
||
396 | return preg_replace('/\s+/', ' ', trim($string)); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Build criteria array from simple command options. |
||
401 | */ |
||
402 | protected function buildCriteriaFromOptions(): array |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Show usage information for the command. |
||
503 | */ |
||
504 | protected function showUsage(): void |
||
574 |