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