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