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