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