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