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