| Conditions | 19 |
| Paths | 1974 |
| Total Lines | 146 |
| Code Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | public function handle(): int |
||
| 45 | { |
||
| 46 | $limit = (int) $this->option('limit'); |
||
| 47 | $videoId = $this->option('video-id'); |
||
| 48 | $dryRun = (bool) $this->option('dry-run'); |
||
| 49 | $debug = (bool) $this->option('debug'); |
||
| 50 | $sleep = (int) $this->option('sleep'); |
||
| 51 | |||
| 52 | // Build query for TV releases with matched show but no episode match |
||
| 53 | $query = Release::query() |
||
| 54 | ->select(['id', 'guid', 'searchname', 'videos_id', 'tv_episodes_id', 'categories_id', 'adddate', 'postdate']) |
||
| 55 | ->where('videos_id', '>', 0) |
||
| 56 | ->where('tv_episodes_id', 0) |
||
| 57 | ->whereBetween('categories_id', [Category::TV_ROOT, Category::TV_OTHER]) |
||
| 58 | ->orderBy('adddate', 'desc') |
||
|
|
|||
| 59 | ->orderBy('postdate', 'desc'); |
||
| 60 | |||
| 61 | if ($videoId !== null) { |
||
| 62 | $query->where('videos_id', (int) $videoId); |
||
| 63 | $this->info("Filtering by video ID: {$videoId}"); |
||
| 64 | } |
||
| 65 | |||
| 66 | $totalCount = (clone $query)->count(); |
||
| 67 | |||
| 68 | if ($totalCount === 0) { |
||
| 69 | $this->info('No TV releases with missing episode matches found.'); |
||
| 70 | return self::SUCCESS; |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->info("Found {$totalCount} TV release(s) with missing episode matches."); |
||
| 74 | |||
| 75 | if ($limit > 0) { |
||
| 76 | $query->limit($limit); |
||
| 77 | $processCount = min($limit, $totalCount); |
||
| 78 | $this->info("Processing limited to {$processCount} release(s)."); |
||
| 79 | } else { |
||
| 80 | $processCount = $totalCount; |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($dryRun) { |
||
| 84 | $this->line("[Dry Run] Would process {$processCount} TV release(s) with missing episode matches."); |
||
| 85 | |||
| 86 | // Show sample of releases that would be processed |
||
| 87 | $sample = (clone $query)->limit(15)->get(); |
||
| 88 | if ($sample->isNotEmpty()) { |
||
| 89 | $this->newLine(); |
||
| 90 | $this->info('Sample of releases to be processed:'); |
||
| 91 | $rows = $sample->map(fn ($release) => [ |
||
| 92 | $release->id, |
||
| 93 | $release->videos_id, |
||
| 94 | mb_substr($release->searchname, 0, 55) . (strlen($release->searchname) > 55 ? '...' : ''), |
||
| 95 | ])->toArray(); |
||
| 96 | $this->table(['ID', 'Video ID', 'Search Name'], $rows); |
||
| 97 | } |
||
| 98 | |||
| 99 | return self::SUCCESS; |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->info("Starting to process {$processCount} TV release(s) with missing episode matches..."); |
||
| 103 | $this->newLine(); |
||
| 104 | |||
| 105 | $bar = $this->output->createProgressBar($processCount); |
||
| 106 | $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%% | Matched: %matched% | Failed: %failed% | Elapsed: %elapsed:6s%'); |
||
| 107 | $bar->setMessage('0', 'matched'); |
||
| 108 | $bar->setMessage('0', 'failed'); |
||
| 109 | $bar->start(); |
||
| 110 | |||
| 111 | $matched = 0; |
||
| 112 | $failed = 0; |
||
| 113 | $processed = 0; |
||
| 114 | |||
| 115 | try { |
||
| 116 | $pipeline = TvProcessingPipeline::createDefault(echoOutput: false); |
||
| 117 | |||
| 118 | // Use cursor to iterate without chunking issues when ordering by non-id columns |
||
| 119 | $cursor = $query->cursor(); |
||
| 120 | |||
| 121 | foreach ($cursor as $release) { |
||
| 122 | if ($limit > 0 && $processed >= $limit) { |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | |||
| 126 | try { |
||
| 127 | $result = $pipeline->processRelease($release, $debug); |
||
| 128 | |||
| 129 | // Check if episode was matched (episode_id > 0) |
||
| 130 | $episodeMatched = isset($result['episode_id']) && $result['episode_id'] > 0; |
||
| 131 | |||
| 132 | if ($episodeMatched) { |
||
| 133 | $matched++; |
||
| 134 | if ($debug) { |
||
| 135 | $this->newLine(); |
||
| 136 | cli()->primary("Episode matched: {$release->searchname}"); |
||
| 137 | $this->info(' Provider: ' . ($result['provider'] ?? 'Unknown')); |
||
| 138 | $this->info(' Video ID: ' . ($result['video_id'] ?? 'N/A')); |
||
| 139 | $this->info(' Episode ID: ' . ($result['episode_id'] ?? 'N/A')); |
||
| 140 | } |
||
| 141 | } else { |
||
| 142 | $failed++; |
||
| 143 | if ($debug) { |
||
| 144 | $this->newLine(); |
||
| 145 | cli()->warning("Episode not found: {$release->searchname}"); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } catch (\Throwable $e) { |
||
| 149 | $failed++; |
||
| 150 | Log::error("Error processing release {$release->guid}: " . $e->getMessage()); |
||
| 151 | if ($debug) { |
||
| 152 | $this->newLine(); |
||
| 153 | $this->error("Error processing {$release->searchname}: " . $e->getMessage()); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $processed++; |
||
| 158 | $bar->setMessage((string) $matched, 'matched'); |
||
| 159 | $bar->setMessage((string) $failed, 'failed'); |
||
| 160 | $bar->advance(); |
||
| 161 | |||
| 162 | if ($sleep > 0) { |
||
| 163 | usleep($sleep * 1000); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | } catch (\Throwable $e) { |
||
| 168 | $bar->finish(); |
||
| 169 | $this->newLine(); |
||
| 170 | $this->error('Fatal error during processing: ' . $e->getMessage()); |
||
| 171 | Log::error('Fatal error in tv:reprocess-missing-episodes: ' . $e->getMessage() . "\n" . $e->getTraceAsString()); |
||
| 172 | return self::FAILURE; |
||
| 173 | } |
||
| 174 | |||
| 175 | $bar->finish(); |
||
| 176 | $this->newLine(2); |
||
| 177 | |||
| 178 | $this->info('Processing complete!'); |
||
| 179 | $this->table( |
||
| 180 | ['Total Processed', 'Episodes Matched', 'Not Matched', 'Match Rate'], |
||
| 181 | [[ |
||
| 182 | number_format($processed), |
||
| 183 | number_format($matched), |
||
| 184 | number_format($failed), |
||
| 185 | $processed > 0 ? round(($matched / $processed) * 100, 2) . '%' : '0%', |
||
| 186 | ]] |
||
| 187 | ); |
||
| 188 | |||
| 189 | return self::SUCCESS; |
||
| 190 | } |
||
| 193 |