| Total Complexity | 51 |
| Total Lines | 237 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TraktPipe often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TraktPipe, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class TraktPipe extends AbstractTvProviderPipe |
||
| 13 | { |
||
| 14 | // Video type and source constants (matching Videos class protected constants) |
||
| 15 | private const TYPE_TV = 0; |
||
| 16 | private const SOURCE_TRAKT = 5; |
||
| 17 | |||
| 18 | protected int $priority = 50; |
||
| 19 | private ?TraktProvider $trakt = null; |
||
| 20 | |||
| 21 | public function getName(): string |
||
| 24 | } |
||
| 25 | |||
| 26 | public function getStatusCode(): int |
||
| 27 | { |
||
| 28 | return -3; // PROCESS_TRAKT |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get or create the Trakt instance. |
||
| 33 | */ |
||
| 34 | private function getTrakt(): TraktProvider |
||
| 40 | } |
||
| 41 | |||
| 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 | ]); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Output full season match message. |
||
| 181 | */ |
||
| 182 | private function outputFullSeason(string $title): void |
||
| 183 | { |
||
| 184 | if (! $this->echoOutput) { |
||
| 185 | return; |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->colorCli->primaryOver(' → '); |
||
| 189 | $this->colorCli->headerOver($this->truncateTitle($title)); |
||
| 190 | $this->colorCli->primaryOver(' → '); |
||
| 191 | $this->colorCli->primary('Full Season matched'); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Process episode matching for a video that already exists in local DB. |
||
| 196 | * This is used when the show was added from another source (e.g., TMDB) and doesn't have a Trakt ID. |
||
| 197 | */ |
||
| 198 | private function processEpisodeForExistingVideo( |
||
| 249 | ]); |
||
| 250 | } |
||
| 251 | } |
||
| 252 |