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