| Total Complexity | 55 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Videos 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 Videos, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class Videos |
||
| 34 | { |
||
| 35 | // Video Type Identifiers |
||
| 36 | protected const TYPE_TV = 0; // Type of video is a TV Programme/Show |
||
| 37 | |||
| 38 | protected const TYPE_FILM = 1; // Type of video is a Film/Movie |
||
| 39 | |||
| 40 | protected const TYPE_ANIME = 2; // Type of video is a Anime |
||
| 41 | |||
| 42 | public bool $echooutput; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array sites The sites that we have an ID columns for in our video table. |
||
| 46 | */ |
||
| 47 | private static $sites = ['imdb', 'tmdb', 'trakt', 'tvdb', 'tvmaze', 'tvrage']; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array Temp Array of cached failed lookups |
||
| 51 | */ |
||
| 52 | public array $titleCache; |
||
| 53 | |||
| 54 | public function __construct() |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Main processing director function for scrapers |
||
| 62 | * Calls work query function and initiates processing. |
||
| 63 | */ |
||
| 64 | abstract protected function processSite(int $groupID, string $guidChar, int $process, bool $local = false): void; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get video info from a Video ID and column. |
||
| 68 | * |
||
| 69 | * @return array|false False if invalid site, or ID not found; Site id value otherwise. |
||
| 70 | */ |
||
| 71 | protected function getSiteIDFromVideoID(string $siteColumn, int $videoID): bool|array |
||
| 72 | { |
||
| 73 | if (\in_array($siteColumn, self::$sites, false)) { |
||
| 74 | $result = Video::query()->where('id', $videoID)->first([$siteColumn]); |
||
| 75 | |||
| 76 | return $result !== null ? $result[$siteColumn] : false; |
||
| 77 | } |
||
| 78 | |||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get TV show local timezone from a Video ID. |
||
| 84 | * |
||
| 85 | * @return string Empty string if no query return or tz style timezone |
||
| 86 | */ |
||
| 87 | protected function getLocalZoneFromVideoID(int $videoID): string |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get video info from a Site ID and column. |
||
| 96 | */ |
||
| 97 | protected function getVideoIDFromSiteID(string $siteColumn, int $siteID): bool|int |
||
| 98 | { |
||
| 99 | $result = false; |
||
| 100 | if (\in_array($siteColumn, self::$sites, false)) { |
||
| 101 | $result = Video::query()->where($siteColumn, $siteID)->first(); |
||
| 102 | } |
||
| 103 | if (! empty($result)) { |
||
| 104 | $query = $result->toArray(); |
||
| 105 | |||
| 106 | return $query['id']; |
||
| 107 | } |
||
| 108 | |||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function getByTitle(string $title, int $type, int $source = 0) |
||
| 113 | { |
||
| 114 | // Check if we already have an entry for this show. |
||
| 115 | $res = $this->getTitleExact($title, $type, $source); |
||
| 116 | if ($res !== 0) { |
||
| 117 | return $res; |
||
| 118 | } |
||
| 119 | |||
| 120 | // Check alt. title (Strip ' and :) Maybe strip more in the future. |
||
| 121 | $res = $this->getAlternativeTitleExact($title, $type, $source); |
||
| 122 | if ($res !== 0) { |
||
| 123 | return $res; |
||
| 124 | } |
||
| 125 | |||
| 126 | $title2 = str_ireplace(' and ', ' & ', $title); |
||
| 127 | if ((string) $title !== (string) $title2) { |
||
| 128 | $res = $this->getTitleExact($title2, $type, $source); |
||
|
|
|||
| 129 | if ($res !== 0) { |
||
| 130 | return $res; |
||
| 131 | } |
||
| 132 | $pieces = explode(' ', $title2); |
||
| 133 | $title2 = '%'; |
||
| 134 | foreach ($pieces as $piece) { |
||
| 135 | $title2 .= str_ireplace(["'", '!'], '', $piece).'%'; |
||
| 136 | } |
||
| 137 | $res = $this->getTitleLoose($title2, $type, $source); |
||
| 138 | if ($res !== 0) { |
||
| 139 | return $res; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // Some words are spelled correctly 2 ways |
||
| 144 | // example theatre and theater |
||
| 145 | $title2 = str_ireplace('er', 're', $title); |
||
| 146 | if ((string) $title !== (string) $title2) { |
||
| 147 | $res = $this->getTitleExact($title2, $type, $source); |
||
| 148 | if ($res !== 0) { |
||
| 149 | return $res; |
||
| 150 | } |
||
| 151 | $pieces = explode(' ', $title2); |
||
| 152 | $title2 = '%'; |
||
| 153 | foreach ($pieces as $piece) { |
||
| 154 | $title2 .= str_ireplace(["'", '!'], '', $piece).'%'; |
||
| 155 | } |
||
| 156 | $res = $this->getTitleLoose($title2, $type, $source); |
||
| 157 | if ($res !== 0) { |
||
| 158 | return $res; |
||
| 159 | } |
||
| 160 | } else { |
||
| 161 | // If there was not an exact title match, look for title with missing chars |
||
| 162 | // example release name :Zorro 1990, tvrage name Zorro (1990) |
||
| 163 | // Only search if the title contains more than one word to prevent incorrect matches |
||
| 164 | $pieces = explode(' ', $title); |
||
| 165 | if (\count($pieces) > 1) { |
||
| 166 | $title2 = '%'; |
||
| 167 | foreach ($pieces as $piece) { |
||
| 168 | $title2 .= str_ireplace(["'", '!'], '', $piece).'%'; |
||
| 169 | } |
||
| 170 | $res = $this->getTitleLoose($title2, $type, $source); |
||
| 171 | if ($res !== 0) { |
||
| 172 | return $res; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | return 0; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getTitleExact(string $title, int $type, int $source = 0): int |
||
| 181 | { |
||
| 182 | $return = 0; |
||
| 183 | if (! empty($title)) { |
||
| 184 | $sql = Video::query()->where(['title' => $title, 'type' => $type]); |
||
| 185 | if ($source > 0) { |
||
| 186 | $sql->where('source', $source); |
||
| 187 | } |
||
| 188 | $query = $sql->first(); |
||
| 189 | if (! empty($query)) { |
||
| 190 | $result = $query->toArray(); |
||
| 191 | $return = $result['id']; |
||
| 192 | } |
||
| 193 | // Try for an alias |
||
| 194 | if (empty($return)) { |
||
| 195 | $sql = Video::query() |
||
| 196 | ->join('videos_aliases', 'videos.id', '=', 'videos_aliases.videos_id') |
||
| 197 | ->where(['videos_aliases.title' => $title, 'videos.type' => $type]); |
||
| 198 | if ($source > 0) { |
||
| 199 | $sql->where('videos.source', $source); |
||
| 200 | } |
||
| 201 | $query = $sql->first(); |
||
| 202 | if (! empty($query)) { |
||
| 203 | $result = $query->toArray(); |
||
| 204 | $return = $result['id']; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $return; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return int|mixed |
||
| 214 | */ |
||
| 215 | public function getTitleLoose($title, $type, int $source = 0): mixed |
||
| 216 | { |
||
| 217 | $return = 0; |
||
| 218 | |||
| 219 | if (! empty($title)) { |
||
| 220 | $sql = Video::query() |
||
| 221 | ->where('title', 'like', rtrim($title, '%')) |
||
| 222 | ->where('type', $type); |
||
| 223 | if ($source > 0) { |
||
| 224 | $sql->where('source', $source); |
||
| 225 | } |
||
| 226 | $query = $sql->first(); |
||
| 227 | if (! empty($query)) { |
||
| 228 | $result = $query->toArray(); |
||
| 229 | $return = $result['id']; |
||
| 230 | } |
||
| 231 | // Try for an alias |
||
| 232 | if (empty($return)) { |
||
| 233 | $sql = Video::query() |
||
| 234 | ->join('videos_aliases', 'videos.id', '=', 'videos_aliases.videos_id') |
||
| 235 | ->where('videos_aliases.title', '=', rtrim($title, '%')) |
||
| 236 | ->where('type', $type); |
||
| 237 | if ($source > 0) { |
||
| 238 | $sql->where('videos.source', $source); |
||
| 239 | } |
||
| 240 | $query = $sql->first(); |
||
| 241 | if (! empty($query)) { |
||
| 242 | $result = $query->toArray(); |
||
| 243 | $return = $result['id']; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | return $return; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return int|mixed |
||
| 253 | */ |
||
| 254 | public function getAlternativeTitleExact(string $title, int $type, int $source = 0): mixed |
||
| 255 | { |
||
| 256 | $return = 0; |
||
| 257 | if (! empty($title)) { |
||
| 258 | if ($source > 0) { |
||
| 259 | $query = Video::query() |
||
| 260 | ->whereRaw("REPLACE(title,'\'','') = ?", $title) |
||
| 261 | ->orWhereRaw("REPLACE(title,':','') = ?", $title) |
||
| 262 | ->where('type', '=', $type) |
||
| 263 | ->where('source', '=', $source) |
||
| 264 | ->first(); |
||
| 265 | } else { |
||
| 266 | $query = Video::query() |
||
| 267 | ->whereRaw("REPLACE(title,'\'','') = ?", $title) |
||
| 268 | ->orWhereRaw("REPLACE(title,':','') = ?", $title) |
||
| 269 | ->where('type', '=', $type) |
||
| 270 | ->first(); |
||
| 271 | } |
||
| 272 | if (! empty($query)) { |
||
| 273 | $result = $query->toArray(); |
||
| 274 | |||
| 275 | return $result['id']; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | return $return; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Inserts aliases for videos. |
||
| 284 | */ |
||
| 285 | public function addAliases($videoId, array $aliases = []): void |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Retrieves all aliases for given VideoID or VideoID for a given alias. |
||
| 305 | * |
||
| 306 | * |
||
| 307 | * @return VideoAlias[]|bool|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|mixed |
||
| 308 | */ |
||
| 309 | public function getAliases(int $videoId, string $alias = ''): mixed |
||
| 333 |