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