| Conditions | 23 |
| Paths | 1386 |
| Total Lines | 201 |
| Code Lines | 125 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 48 | public function handle(): int |
||
| 49 | { |
||
| 50 | $limit = (int) $this->option('limit'); |
||
| 51 | $chunkSize = (int) $this->option('chunk'); |
||
| 52 | $missingOnly = $this->option('missing-only'); |
||
| 53 | $force = $this->option('force'); |
||
| 54 | |||
| 55 | $this->info('Starting AniList data refresh for anime releases...'); |
||
| 56 | $this->info('Matching releases by searchname to AniList API...'); |
||
| 57 | $this->newLine(); |
||
| 58 | |||
| 59 | // Build query for releases in TV_ANIME category |
||
| 60 | $query = Release::query() |
||
| 61 | ->select(['releases.id', 'releases.anidbid', 'releases.searchname']) |
||
| 62 | ->where('categories_id', Category::TV_ANIME); |
||
| 63 | |||
| 64 | // If missing-only, only get releases without anilist_id |
||
| 65 | if ($missingOnly) { |
||
| 66 | $query->leftJoin('anidb_info as ai', 'ai.anidbid', '=', 'releases.anidbid') |
||
| 67 | ->whereNull('ai.anilist_id'); |
||
| 68 | } |
||
| 69 | |||
| 70 | // Get releases (not distinct anidbids, since we're matching by searchname) |
||
| 71 | $releases = $query->orderBy('releases.id') |
||
|
|
|||
| 72 | ->get(); |
||
| 73 | |||
| 74 | $totalCount = $releases->count(); |
||
| 75 | |||
| 76 | if ($totalCount === 0) { |
||
| 77 | $this->warn('No anime releases found to process.'); |
||
| 78 | return self::SUCCESS; |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->info("Found {$totalCount} anime releases to process."); |
||
| 82 | |||
| 83 | if ($limit > 0) { |
||
| 84 | $releases = $releases->take($limit); |
||
| 85 | $totalCount = $releases->count(); |
||
| 86 | $this->info("Processing {$totalCount} releases (limited)."); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->newLine(); |
||
| 90 | |||
| 91 | $populateAniList = new PopulateAniList; |
||
| 92 | $processed = 0; |
||
| 93 | $successful = 0; |
||
| 94 | $failed = 0; |
||
| 95 | $skipped = 0; |
||
| 96 | $notFound = 0; |
||
| 97 | |||
| 98 | // Process in chunks |
||
| 99 | $chunks = $releases->chunk($chunkSize); |
||
| 100 | $progressBar = $this->output->createProgressBar($totalCount); |
||
| 101 | $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s% -- %message%'); |
||
| 102 | $progressBar->setMessage('Starting...'); |
||
| 103 | $progressBar->start(); |
||
| 104 | |||
| 105 | foreach ($chunks as $chunk) { |
||
| 106 | foreach ($chunk as $release) { |
||
| 107 | $searchname = $release->searchname ?? ''; |
||
| 108 | $progressBar->setMessage("Processing: " . substr($searchname, 0, 50) . "..."); |
||
| 109 | |||
| 110 | try { |
||
| 111 | // Extract clean title from searchname |
||
| 112 | $titleData = $this->extractTitleFromSearchname($searchname); |
||
| 113 | |||
| 114 | if (empty($titleData) || empty($titleData['title'])) { |
||
| 115 | $notFound++; |
||
| 116 | $processed++; |
||
| 117 | $progressBar->advance(); |
||
| 118 | continue; |
||
| 119 | } |
||
| 120 | |||
| 121 | $cleanTitle = $titleData['title']; |
||
| 122 | |||
| 123 | // Check if we should skip (if not forcing and data exists) |
||
| 124 | if (! $force && ! $missingOnly) { |
||
| 125 | // Check if release already has complete AniList data |
||
| 126 | if ($release->anidbid > 0) { |
||
| 127 | $anidbInfo = DB::table('anidb_info') |
||
| 128 | ->where('anidbid', $release->anidbid) |
||
| 129 | ->whereNotNull('anilist_id') |
||
| 130 | ->whereNotNull('country') |
||
| 131 | ->whereNotNull('media_type') |
||
| 132 | ->first(); |
||
| 133 | |||
| 134 | if ($anidbInfo) { |
||
| 135 | $skipped++; |
||
| 136 | $processed++; |
||
| 137 | $progressBar->advance(); |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // Search AniList for this title (with rate limiting) |
||
| 144 | $this->enforceRateLimit(); |
||
| 145 | $searchResults = $populateAniList->searchAnime($cleanTitle, 1); |
||
| 146 | |||
| 147 | if (! $searchResults || empty($searchResults)) { |
||
| 148 | // Try with spaces replaced for broader matching |
||
| 149 | $altTitle = preg_replace('/\s+/', ' ', $cleanTitle); |
||
| 150 | if ($altTitle !== $cleanTitle) { |
||
| 151 | $this->enforceRateLimit(); |
||
| 152 | $searchResults = $populateAniList->searchAnime($altTitle, 1); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | if (! $searchResults || empty($searchResults)) { |
||
| 157 | $notFound++; |
||
| 158 | $processed++; |
||
| 159 | $progressBar->advance(); |
||
| 160 | continue; |
||
| 161 | } |
||
| 162 | |||
| 163 | $anilistData = $searchResults[0]; |
||
| 164 | $anilistId = $anilistData['id'] ?? null; |
||
| 165 | |||
| 166 | if (! $anilistId) { |
||
| 167 | $notFound++; |
||
| 168 | $processed++; |
||
| 169 | $progressBar->advance(); |
||
| 170 | continue; |
||
| 171 | } |
||
| 172 | |||
| 173 | // Fetch full data from AniList and insert/update (with rate limiting) |
||
| 174 | // This will create/update anidb_info entry using anilist_id as anidbid if needed |
||
| 175 | $this->enforceRateLimit(); |
||
| 176 | $populateAniList->populateTable('info', $anilistId); |
||
| 177 | |||
| 178 | // Get the anidbid that was created/updated (it uses anilist_id as anidbid) |
||
| 179 | $anidbid = AnidbInfo::query() |
||
| 180 | ->where('anilist_id', $anilistId) |
||
| 181 | ->value('anidbid'); |
||
| 182 | |||
| 183 | if (! $anidbid) { |
||
| 184 | // Fallback: use anilist_id as anidbid |
||
| 185 | $anidbid = (int) $anilistId; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Update release with the anidbid |
||
| 189 | Release::query() |
||
| 190 | ->where('id', $release->id) |
||
| 191 | ->update(['anidbid' => $anidbid]); |
||
| 192 | |||
| 193 | $successful++; |
||
| 194 | } catch (\Exception $e) { |
||
| 195 | // Check if this is a 429 rate limit error |
||
| 196 | if (str_contains($e->getMessage(), '429') || str_contains($e->getMessage(), 'rate limit exceeded')) { |
||
| 197 | $this->newLine(); |
||
| 198 | $this->error('AniList API rate limit exceeded (429). Stopping processing for 15 minutes.'); |
||
| 199 | $this->warn('Please wait 15 minutes before running this command again.'); |
||
| 200 | $progressBar->finish(); |
||
| 201 | $this->newLine(); |
||
| 202 | |||
| 203 | // Show summary of what was processed before the error |
||
| 204 | $this->info('Summary (before rate limit error):'); |
||
| 205 | $this->table( |
||
| 206 | ['Status', 'Count'], |
||
| 207 | [ |
||
| 208 | ['Total Processed', $processed], |
||
| 209 | ['Successful', $successful], |
||
| 210 | ['Failed', $failed], |
||
| 211 | ['Not Found', $notFound], |
||
| 212 | ['Skipped', $skipped], |
||
| 213 | ] |
||
| 214 | ); |
||
| 215 | |||
| 216 | return self::FAILURE; |
||
| 217 | } |
||
| 218 | |||
| 219 | $failed++; |
||
| 220 | if ($this->getOutput()->isVerbose()) { |
||
| 221 | $this->newLine(); |
||
| 222 | $this->error("Error processing release ID {$release->id}: " . $e->getMessage()); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | $processed++; |
||
| 227 | $progressBar->advance(); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | $progressBar->setMessage('Complete!'); |
||
| 232 | $progressBar->finish(); |
||
| 233 | $this->newLine(2); |
||
| 234 | |||
| 235 | // Summary |
||
| 236 | $this->info('Summary:'); |
||
| 237 | $this->table( |
||
| 238 | ['Status', 'Count'], |
||
| 239 | [ |
||
| 240 | ['Total Processed', $processed], |
||
| 241 | ['Successful', $successful], |
||
| 242 | ['Failed', $failed], |
||
| 243 | ['Not Found', $notFound], |
||
| 244 | ['Skipped', $skipped], |
||
| 245 | ] |
||
| 246 | ); |
||
| 247 | |||
| 248 | return self::SUCCESS; |
||
| 249 | } |
||
| 413 |