| Conditions | 37 |
| Paths | > 20000 |
| Total Lines | 156 |
| Code Lines | 83 |
| 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 |
||
| 46 | protected function process(TvProcessingPassable $passable): TvProcessingResult |
||
| 47 | { |
||
| 48 | $parsedInfo = $passable->getParsedInfo(); |
||
| 49 | $context = $passable->context; |
||
| 50 | |||
| 51 | if ($parsedInfo === null || empty($parsedInfo['cleanname'])) { |
||
| 52 | return TvProcessingResult::notFound($this->getName()); |
||
| 53 | } |
||
| 54 | |||
| 55 | $cleanName = $parsedInfo['cleanname']; |
||
| 56 | |||
| 57 | // Check if we've already failed this title |
||
| 58 | if ($this->isInTitleCache($cleanName)) { |
||
| 59 | $this->outputSkipped($cleanName); |
||
| 60 | |||
| 61 | return TvProcessingResult::skipped('previously failed', $this->getName()); |
||
| 62 | } |
||
| 63 | |||
| 64 | $tvdb = $this->getTvdb(); |
||
| 65 | $siteId = false; |
||
| 66 | $posterUrl = ''; |
||
| 67 | |||
| 68 | // Find the Video ID if it already exists by checking the title |
||
| 69 | $videoId = $tvdb->getByTitle($cleanName, self::TYPE_TV); |
||
| 70 | |||
| 71 | // If not found and cleanName contains a year in parentheses, try without the year |
||
| 72 | if ($videoId === 0 && preg_match('/^(.+?)\s*\(\d{4}\)$/', $cleanName, $yearMatch)) { |
||
| 73 | $nameWithoutYear = trim($yearMatch[1]); |
||
| 74 | $videoId = $tvdb->getByTitle($nameWithoutYear, self::TYPE_TV); |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($videoId !== 0) { |
||
| 78 | $siteId = $tvdb->getSiteByID('tvdb', $videoId); |
||
| 79 | // If show exists in local DB but doesn't have a TVDB ID, use the existing video |
||
| 80 | // and process episode matching without trying to search TVDB API |
||
| 81 | if ($siteId === false || $siteId === 0) { |
||
| 82 | // Show exists in our DB (likely from another source like TMDB) |
||
| 83 | // Skip TVDB API search and proceed to episode matching |
||
| 84 | $this->outputFoundInDb($cleanName); |
||
| 85 | |||
| 86 | return $this->processEpisodeForExistingVideo($passable, $tvdb, $videoId, $parsedInfo); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | // Check if we have a valid country |
||
| 91 | $country = ( |
||
| 92 | isset($parsedInfo['country']) && strlen($parsedInfo['country']) === 2 |
||
| 93 | ? (string) $parsedInfo['country'] |
||
| 94 | : '' |
||
| 95 | ); |
||
| 96 | |||
| 97 | if ($siteId === false || $siteId === 0) { |
||
| 98 | // Not in local DB, search TVDB |
||
| 99 | $this->outputSearching($cleanName); |
||
| 100 | |||
| 101 | $tvdbShow = $tvdb->getShowInfo((string) $cleanName); |
||
| 102 | |||
| 103 | // If not found and cleanName contains a year in parentheses, try without the year |
||
| 104 | if ($tvdbShow === false && preg_match('/^(.+?)\s*\(\d{4}\)$/', $cleanName, $yearMatch)) { |
||
| 105 | $nameWithoutYear = trim($yearMatch[1]); |
||
| 106 | $tvdbShow = $tvdb->getShowInfo($nameWithoutYear); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (is_array($tvdbShow)) { |
||
| 110 | $tvdbShow['country'] = $country; |
||
| 111 | $videoId = $tvdb->add($tvdbShow); |
||
| 112 | $siteId = (int) $tvdbShow['tvdb']; |
||
| 113 | $posterUrl = $tvdbShow['poster'] ?? ''; |
||
| 114 | } |
||
| 115 | } else { |
||
| 116 | $this->outputFoundInDb($cleanName); |
||
| 117 | } |
||
| 118 | |||
| 119 | if ((int) $videoId === 0 || (int) $siteId === 0) { |
||
| 120 | // Show not found |
||
| 121 | $this->addToTitleCache($cleanName); |
||
| 122 | $this->outputNotFound($cleanName); |
||
| 123 | |||
| 124 | return TvProcessingResult::notFound($this->getName(), ['title' => $cleanName]); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Fetch poster if available |
||
| 128 | if (! empty($posterUrl)) { |
||
| 129 | $tvdb->getPoster($videoId); |
||
| 130 | } else { |
||
| 131 | $this->fetchFanartPoster($videoId, $siteId); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Process episode |
||
| 135 | $seriesNo = ! empty($parsedInfo['season']) ? preg_replace('/^S0*/i', '', (string) $parsedInfo['season']) : ''; |
||
| 136 | $episodeNo = ! empty($parsedInfo['episode']) ? preg_replace('/^E0*/i', '', (string) $parsedInfo['episode']) : ''; |
||
| 137 | $hasAirdate = ! empty($parsedInfo['airdate']); |
||
| 138 | |||
| 139 | if ($episodeNo === 'all') { |
||
| 140 | // Full season release |
||
| 141 | $tvdb->setVideoIdFound($videoId, $context->releaseId, 0); |
||
| 142 | $this->outputFullSeason($cleanName); |
||
| 143 | |||
| 144 | return TvProcessingResult::matched($videoId, 0, $this->getName(), ['full_season' => true]); |
||
| 145 | } |
||
| 146 | |||
| 147 | // Download all episodes if new show to reduce API/bandwidth usage |
||
| 148 | if (! $tvdb->countEpsByVideoID($videoId)) { |
||
| 149 | $tvdb->getEpisodeInfo($siteId, -1, -1, $videoId); |
||
| 150 | } |
||
| 151 | |||
| 152 | // Check if we have the episode for this video ID |
||
| 153 | $episode = $tvdb->getBySeasonEp($videoId, $seriesNo, $episodeNo, $parsedInfo['airdate'] ?? ''); |
||
| 154 | |||
| 155 | if ($episode === false) { |
||
| 156 | if ($seriesNo !== '' && $episodeNo !== '') { |
||
| 157 | // Try to get episode from TVDB |
||
| 158 | $tvdbEpisode = $tvdb->getEpisodeInfo($siteId, (int) $seriesNo, (int) $episodeNo, $videoId); |
||
| 159 | |||
| 160 | if ($tvdbEpisode) { |
||
| 161 | $episode = $tvdb->addEpisode($videoId, $tvdbEpisode); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($episode === false && $hasAirdate) { |
||
| 166 | // Refresh episode cache and attempt airdate match |
||
| 167 | $tvdb->getEpisodeInfo($siteId, -1, -1, $videoId); |
||
| 168 | $episode = $tvdb->getBySeasonEp($videoId, 0, 0, $parsedInfo['airdate']); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($episode !== false && is_numeric($episode) && $episode > 0) { |
||
| 173 | // Success! |
||
| 174 | $tvdb->setVideoIdFound($videoId, $context->releaseId, $episode); |
||
| 175 | $this->outputMatch( |
||
| 176 | $cleanName, |
||
| 177 | $seriesNo !== '' ? (int) $seriesNo : null, |
||
| 178 | $episodeNo !== '' ? (int) $episodeNo : null, |
||
| 179 | $hasAirdate ? $parsedInfo['airdate'] : null |
||
| 180 | ); |
||
| 181 | |||
| 182 | return TvProcessingResult::matched($videoId, (int) $episode, $this->getName()); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Episode not found |
||
| 186 | $tvdb->setVideoIdFound($videoId, $context->releaseId, 0); |
||
| 187 | |||
| 188 | if ($this->echoOutput) { |
||
| 189 | cli()->primaryOver(' → '); |
||
| 190 | cli()->alternateOver($this->truncateTitle($cleanName)); |
||
| 191 | if ($hasAirdate) { |
||
| 192 | cli()->primaryOver(' | '); |
||
| 193 | cli()->warningOver($parsedInfo['airdate']); |
||
| 194 | } |
||
| 195 | cli()->primaryOver(' → '); |
||
| 196 | cli()->warning('Episode not found'); |
||
| 197 | } |
||
| 198 | |||
| 199 | return TvProcessingResult::notFound($this->getName(), [ |
||
| 200 | 'video_id' => $videoId, |
||
| 201 | 'episode_not_found' => true, |
||
| 202 | ]); |
||
| 300 |