| Total Complexity | 57 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Nfo 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 Nfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Nfo |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var \Blacklight\db\DB |
||
| 22 | */ |
||
| 23 | public $pdo; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $nzbs; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | protected $maxSize; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $maxRetries; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $minSize; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $tmpPath; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $echo; |
||
| 54 | |||
| 55 | public const NFO_FAILED = -9; // We failed to get a NFO after admin set max retries. |
||
| 56 | public const NFO_UNPROC = -1; // Release has not been processed yet. |
||
| 57 | public const NFO_NONFO = 0; // Release has no NFO. |
||
| 58 | public const NFO_FOUND = 1; // Release has an NFO. |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Default constructor. |
||
| 62 | * |
||
| 63 | * @throws \Exception |
||
| 64 | */ |
||
| 65 | public function __construct() |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Look for a TV Show ID in a string. |
||
| 82 | * |
||
| 83 | * @param string $str The string with a Show ID. |
||
| 84 | * |
||
| 85 | * @return array|bool Return array with show ID and site source or false on failure. |
||
| 86 | */ |
||
| 87 | public function parseShowId($str) |
||
| 88 | { |
||
| 89 | $return = false; |
||
| 90 | |||
| 91 | if (preg_match('/tvmaze\.com\/shows\/(\d{1,6})/i', $str, $matches)) { |
||
| 92 | $return = |
||
| 93 | [ |
||
| 94 | 'showid' => trim($matches[1]), |
||
| 95 | 'site' => 'tvmaze', |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (preg_match('/imdb\.com\/title\/(tt\d{1,8})/i', $str, $matches)) { |
||
| 100 | $return = |
||
| 101 | [ |
||
| 102 | 'showid' => trim($matches[1]), |
||
| 103 | 'site' => 'imdb', |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | |||
| 107 | if (preg_match('/thetvdb\.com\/\?tab=series&id=(\d{1,8})/i', $str, $matches)) { |
||
| 108 | $return = |
||
| 109 | [ |
||
| 110 | 'showid' => trim($matches[1]), |
||
| 111 | 'site' => 'thetvdb', |
||
| 112 | ]; |
||
| 113 | } |
||
| 114 | |||
| 115 | return $return; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Confirm this is an NFO file. |
||
| 120 | * |
||
| 121 | * @param string $possibleNFO The nfo. |
||
| 122 | * @param string $guid The guid of the release. |
||
| 123 | * |
||
| 124 | * @return bool True on success, False on failure. |
||
| 125 | * @throws \Exception |
||
| 126 | */ |
||
| 127 | public function isNFO(&$possibleNFO, $guid): bool |
||
| 128 | { |
||
| 129 | if ($possibleNFO === false) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Make sure it's not too big or small, size needs to be at least 12 bytes for header checking. Ignore common file types. |
||
| 134 | $size = \strlen($possibleNFO); |
||
| 135 | if ($size < 65535 && |
||
| 136 | $size > 11 && |
||
| 137 | ! preg_match( |
||
| 138 | '/\A(\s*<\?xml|=newz\[NZB\]=|RIFF|\s*[RP]AR|.{0,10}(JFIF|matroska|ftyp|ID3))|;\s*Generated\s*by.*SF\w/i', |
||
| 139 | $possibleNFO |
||
| 140 | )) { |
||
| 141 | // File/GetId3 work with files, so save to disk. |
||
| 142 | $tmpPath = $this->tmpPath.$guid.'.nfo'; |
||
| 143 | file_put_contents($tmpPath, $possibleNFO); |
||
| 144 | |||
| 145 | // Linux boxes have 'file' (so should Macs), Windows *can* have it too: see GNUWIN.txt in docs. |
||
| 146 | $result = Utility::fileInfo($tmpPath); |
||
| 147 | if (! empty($result)) { |
||
| 148 | |||
| 149 | // Check if it's text. |
||
| 150 | if (preg_match('/(ASCII|ISO-8859|UTF-(8|16|32).*?)\s*text/', $result)) { |
||
| 151 | @unlink($tmpPath); |
||
| 152 | |||
| 153 | return true; |
||
| 154 | |||
| 155 | // Or binary. |
||
| 156 | } |
||
| 157 | |||
| 158 | if (preg_match('/^(JPE?G|Parity|PNG|RAR|XML|(7-)?[Zz]ip)/', $result) || preg_match('/[\x00-\x08\x12-\x1F\x0B\x0E\x0F]/', $possibleNFO)) { |
||
| 159 | @unlink($tmpPath); |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // If above checks couldn't make a categorical identification, Use GetId3 to check if it's an image/video/rar/zip etc.. |
||
| 166 | $check = (new \getID3())->analyze($tmpPath); |
||
| 167 | @unlink($tmpPath); |
||
| 168 | if (isset($check['error'])) { |
||
| 169 | |||
| 170 | // Check if it's a par2. |
||
| 171 | $par2info = new Par2Info(); |
||
| 172 | $par2info->setData($possibleNFO); |
||
| 173 | if ($par2info->error) { |
||
| 174 | // Check if it's an SFV. |
||
| 175 | $sfv = new SfvInfo(); |
||
| 176 | $sfv->setData($possibleNFO); |
||
| 177 | if ($sfv->error) { |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Add an NFO from alternate sources. ex.: PreDB, rar, zip, etc... |
||
| 189 | * |
||
| 190 | * @param string $nfo The nfo. |
||
| 191 | * @param array $release The SQL row for this release. |
||
| 192 | * @param NNTP $nntp Instance of class NNTP. |
||
| 193 | * |
||
| 194 | * @return bool True on success, False on failure. |
||
| 195 | * @throws \Exception |
||
| 196 | */ |
||
| 197 | public function addAlternateNfo(&$nfo, $release, $nntp): bool |
||
| 198 | { |
||
| 199 | if ($release['id'] > 0 && $this->isNFO($nfo, $release['guid'])) { |
||
| 200 | $check = ReleaseNfo::query()->where('releases_id', $release['id'])->first(['releases_id']); |
||
| 201 | |||
| 202 | if ($check === null) { |
||
| 203 | ReleaseNfo::query()->insert(['releases_id' => $release['id'], 'nfo' => "\x1f\x8b\x08\x00".gzcompress($nfo)]); |
||
| 204 | } |
||
| 205 | |||
| 206 | Release::query()->where('id', $release['id'])->update(['nfostatus' => self::NFO_FOUND]); |
||
| 207 | |||
| 208 | if (! isset($release['completion'])) { |
||
| 209 | $release['completion'] = 0; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($release['completion'] === 0) { |
||
| 213 | $nzbContents = new NZBContents( |
||
| 214 | [ |
||
| 215 | 'Echo' => $this->echo, |
||
| 216 | 'NNTP' => $nntp, |
||
| 217 | 'Nfo' => $this, |
||
| 218 | 'Settings' => null, |
||
| 219 | 'PostProcess' => new PostProcess(['Echo' => $this->echo, 'Nfo' => $this]), |
||
| 220 | ] |
||
| 221 | ); |
||
| 222 | $nzbContents->parseNZB($release['guid'], $release['id'], $release['groups_id']); |
||
| 223 | } |
||
| 224 | |||
| 225 | return true; |
||
| 226 | } |
||
| 227 | |||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Attempt to find NFO files inside the NZB's of releases. |
||
| 233 | * |
||
| 234 | * @param $nntp |
||
| 235 | * @param string $groupID (optional) Group ID. |
||
| 236 | * @param string $guidChar (optional) First character of the release GUID (used for multi-processing). |
||
| 237 | * @param int $processImdb (optional) Attempt to find IMDB id's in the NZB? |
||
| 238 | * @param int $processTv (optional) Attempt to find Tv id's in the NZB? |
||
| 239 | * |
||
| 240 | * @return int How many NFO's were processed? |
||
| 241 | * @throws \Exception |
||
| 242 | */ |
||
| 243 | public function processNfoFiles($nntp, $groupID = '', $guidChar = '', $processImdb = 1, $processTv = 1): int |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get a string like this: |
||
| 391 | * "AND r.nzbstatus = 1 AND r.nfostatus BETWEEN -8 AND -1 AND r.size < 1073741824 AND r.size > 1048576" |
||
| 392 | * To use in a query. |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | * @throws \Exception |
||
| 396 | * @static |
||
| 397 | */ |
||
| 398 | public static function NfoQueryString(): string |
||
| 415 |