| Conditions | 31 |
| Paths | 396 |
| Total Lines | 166 |
| Code Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 50 | public function processSite($groupID, $guidChar, int $process, bool $local = false): void |
||
| 51 | { |
||
| 52 | $res = $this->getTvReleases($groupID, $guidChar, $process, parent::PROCESS_TRAKT); |
||
| 53 | |||
| 54 | $tvcount = \count($res); |
||
| 55 | |||
| 56 | if ($tvcount === 0) { |
||
| 57 | |||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($res instanceof \Traversable) { |
||
|
|
|||
| 62 | $processed = 0; |
||
| 63 | $matched = 0; |
||
| 64 | $skipped = 0; |
||
| 65 | |||
| 66 | foreach ($res as $row) { |
||
| 67 | $processed++; |
||
| 68 | $traktid = false; |
||
| 69 | $this->posterUrl = $this->fanartUrl = $this->localizedTZ = ''; |
||
| 70 | |||
| 71 | // Clean the show name for better match probability |
||
| 72 | $release = $this->parseInfo($row['searchname']); |
||
| 73 | if (\is_array($release) && $release['name'] !== '') { |
||
| 74 | if (\in_array($release['cleanname'], $this->titleCache, false)) { |
||
| 75 | if ($this->echooutput) { |
||
| 76 | cli()->primaryOver(' → '); |
||
| 77 | cli()->alternateOver($this->truncateTitle($release['cleanname'])); |
||
| 78 | cli()->primaryOver(' → '); |
||
| 79 | cli()->alternate('Skipped (previously failed)'); |
||
| 80 | } |
||
| 81 | $this->setVideoNotFound(parent::PROCESS_IMDB, $row['id']); |
||
| 82 | $skipped++; |
||
| 83 | |||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | // Find the Video ID if it already exists by checking the title. |
||
| 88 | $videoId = $this->getByTitle($release['cleanname'], parent::TYPE_TV, parent::SOURCE_TRAKT); |
||
| 89 | |||
| 90 | // Force local lookup only |
||
| 91 | if ($local === true) { |
||
| 92 | $lookupSetting = false; |
||
| 93 | } else { |
||
| 94 | $lookupSetting = true; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($videoId === 0 && $lookupSetting) { |
||
| 98 | // If it doesn't exist locally and lookups are allowed lets try to get it. |
||
| 99 | if ($this->echooutput) { |
||
| 100 | cli()->primaryOver(' → '); |
||
| 101 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 102 | cli()->primaryOver(' → '); |
||
| 103 | cli()->info('Searching Trakt...'); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Get the show from TRAKT |
||
| 107 | $traktShow = $this->getShowInfo((string) $release['cleanname']); |
||
| 108 | |||
| 109 | if (\is_array($traktShow)) { |
||
| 110 | $videoId = $this->add($traktShow); |
||
| 111 | $traktid = (int) $traktShow['trakt']; |
||
| 112 | } |
||
| 113 | } else { |
||
| 114 | if ($this->echooutput && $videoId > 0) { |
||
| 115 | cli()->primaryOver(' → '); |
||
| 116 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 117 | cli()->primaryOver(' → '); |
||
| 118 | cli()->info('Found in DB'); |
||
| 119 | } |
||
| 120 | $traktid = $this->getSiteIDFromVideoID('trakt', $videoId); |
||
| 121 | $this->localizedTZ = $this->getLocalZoneFromVideoID($videoId); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ((int) $videoId > 0 && (int) $traktid > 0) { |
||
| 125 | // Now that we have valid video and trakt ids, try to get the poster |
||
| 126 | // $this->getPoster($videoId, $traktid); |
||
| 127 | |||
| 128 | $seasonNo = preg_replace('/^S0*/i', '', $release['season']); |
||
| 129 | $episodeNo = preg_replace('/^E0*/i', '', $release['episode']); |
||
| 130 | |||
| 131 | if ($episodeNo === 'all') { |
||
| 132 | // Set the video ID and leave episode 0 |
||
| 133 | $this->setVideoIdFound($videoId, $row['id'], 0); |
||
| 134 | if ($this->echooutput) { |
||
| 135 | cli()->primaryOver(' → '); |
||
| 136 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 137 | cli()->primaryOver(' → '); |
||
| 138 | cli()->primary('Full Season matched'); |
||
| 139 | } |
||
| 140 | $matched++; |
||
| 141 | |||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | // Check if we have the episode for this video ID |
||
| 146 | $episode = $this->getBySeasonEp($videoId, $seasonNo, $episodeNo, $release['airdate']); |
||
| 147 | |||
| 148 | if ($episode === false && $lookupSetting) { |
||
| 149 | // Send the request for the episode to TRAKT with fallback to other IDs |
||
| 150 | $traktEpisode = $this->getEpisodeInfo( |
||
| 151 | $traktid, |
||
| 152 | $seasonNo, |
||
| 153 | $episodeNo, |
||
| 154 | $videoId |
||
| 155 | ); |
||
| 156 | |||
| 157 | if ($traktEpisode) { |
||
| 158 | $episode = $this->addEpisode($videoId, $traktEpisode); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($episode !== false && is_numeric($episode) && $episode > 0) { |
||
| 163 | // Mark the releases video and episode IDs |
||
| 164 | $this->setVideoIdFound($videoId, $row['id'], $episode); |
||
| 165 | if ($this->echooutput) { |
||
| 166 | cli()->primaryOver(' → '); |
||
| 167 | cli()->headerOver($this->truncateTitle($release['cleanname'])); |
||
| 168 | cli()->primaryOver(' S'); |
||
| 169 | cli()->warningOver(sprintf('%02d', $seasonNo)); |
||
| 170 | cli()->primaryOver('E'); |
||
| 171 | cli()->warningOver(sprintf('%02d', $episodeNo)); |
||
| 172 | cli()->primaryOver(' ✓ '); |
||
| 173 | cli()->primary('MATCHED (Trakt)'); |
||
| 174 | } |
||
| 175 | $matched++; |
||
| 176 | } else { |
||
| 177 | // Processing failed, set the episode ID to the next processing group |
||
| 178 | $this->setVideoIdFound($videoId, $row['id'], 0); |
||
| 179 | $this->setVideoNotFound(parent::PROCESS_IMDB, $row['id']); |
||
| 180 | if ($this->echooutput) { |
||
| 181 | cli()->primaryOver(' → '); |
||
| 182 | cli()->alternateOver($this->truncateTitle($release['cleanname'])); |
||
| 183 | cli()->primaryOver(' → '); |
||
| 184 | cli()->warning('Episode not found'); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | // Processing failed, set the episode ID to the next processing group |
||
| 189 | $this->setVideoNotFound(parent::PROCESS_IMDB, $row['id']); |
||
| 190 | $this->titleCache[] = $release['cleanname'] ?? null; |
||
| 191 | if ($this->echooutput) { |
||
| 192 | cli()->primaryOver(' → '); |
||
| 193 | cli()->alternateOver($this->truncateTitle($release['cleanname'])); |
||
| 194 | cli()->primaryOver(' → '); |
||
| 195 | cli()->warning('Not found'); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } else { |
||
| 199 | // Processing failed, set the episode ID to the next processing group |
||
| 200 | $this->setVideoNotFound(parent::PROCESS_IMDB, $row['id']); |
||
| 201 | $this->titleCache[] = $release['cleanname'] ?? null; |
||
| 202 | if ($this->echooutput) { |
||
| 203 | cli()->primaryOver(' → '); |
||
| 204 | cli()->alternateOver(mb_substr($row['searchname'], 0, 50)); |
||
| 205 | cli()->primaryOver(' → '); |
||
| 206 | cli()->error('Parse failed'); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | // Display summary |
||
| 212 | if ($this->echooutput && $matched > 0) { |
||
| 213 | echo "\n"; |
||
| 214 | cli()->primaryOver(' ✓ Trakt: '); |
||
| 215 | cli()->primary(sprintf('%d matched, %d skipped', $matched, $skipped)); |
||
| 216 | } |
||
| 468 |