Total Complexity | 58 |
Total Lines | 413 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like NZBImport 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 NZBImport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class NZBImport |
||
17 | { |
||
18 | protected Binaries $binaries; |
||
19 | |||
20 | protected ReleaseCleaning $releaseCleaner; |
||
21 | |||
22 | protected \stdClass|bool $site; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected mixed $crossPostt; |
||
28 | |||
29 | protected Categorize $category; |
||
30 | |||
31 | /** |
||
32 | * List of all the group names/ids in the DB. |
||
33 | */ |
||
34 | protected array $allGroups; |
||
35 | |||
36 | /** |
||
37 | * Was this run from the browser? |
||
38 | */ |
||
39 | protected bool $browser; |
||
40 | |||
41 | /** |
||
42 | * Return value for browser. |
||
43 | */ |
||
44 | protected string $retVal; |
||
45 | |||
46 | /** |
||
47 | * Guid of the current releases. |
||
48 | */ |
||
49 | protected string $relGuid; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | public mixed $echoCLI; |
||
55 | |||
56 | public NZB $nzb; |
||
57 | |||
58 | /** |
||
59 | * @var string the MD5 hash of the first segment Message-ID of the NZB |
||
60 | */ |
||
61 | protected string $nzbGuid; |
||
62 | |||
63 | protected ColorCLI $colorCli; |
||
64 | |||
65 | public function __construct() |
||
66 | { |
||
67 | $this->echoCLI = config('nntmux.echocli'); |
||
68 | $this->binaries = new Binaries(); |
||
69 | $this->category = new Categorize(); |
||
70 | $this->nzb = new NZB(); |
||
71 | $this->releaseCleaner = new ReleaseCleaning(); |
||
72 | $this->colorCli = new ColorCLI(); |
||
73 | $this->crossPostt = Settings::settingValue('..crossposttime') !== '' ? Settings::settingValue('..crossposttime') : 2; |
||
74 | $this->retVal = ''; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
79 | */ |
||
80 | public function beginImport($filesToProcess, bool $useNzbName = false, bool $delete = false, bool $deleteFailed = false): bool|string |
||
81 | { |
||
82 | // Get all the groups in the DB. |
||
83 | if (! $this->getAllGroups()) { |
||
84 | if ($this->browser) { |
||
85 | return $this->retVal; |
||
86 | } |
||
87 | |||
88 | return false; |
||
89 | } |
||
90 | |||
91 | $start = now()->toImmutable()->format('Y-m-d H:i:s'); |
||
92 | $nzbsImported = $nzbsSkipped = 0; |
||
93 | |||
94 | // Loop over the file names. |
||
95 | foreach ($filesToProcess as $nzbFile) { |
||
96 | $this->nzbGuid = ''; |
||
97 | |||
98 | // Check if the file is really there. |
||
99 | if (File::isFile($nzbFile)) { |
||
100 | // Get the contents of the NZB file as a string. |
||
101 | if (Str::endsWith($nzbFile, '.nzb.gz')) { |
||
102 | $nzbString = Utility::unzipGzipFile($nzbFile); |
||
103 | } else { |
||
104 | $nzbString = File::get($nzbFile); |
||
105 | } |
||
106 | |||
107 | if ($nzbString === false) { |
||
108 | $this->echoOut('ERROR: Unable to read: '.$nzbFile); |
||
109 | |||
110 | if ($deleteFailed) { |
||
111 | File::delete($nzbFile); |
||
112 | } |
||
113 | $nzbsSkipped++; |
||
114 | |||
115 | continue; |
||
116 | } |
||
117 | |||
118 | // Load it as an XML object. |
||
119 | $nzbXML = @simplexml_load_string($nzbString); |
||
120 | if ($nzbXML === false || strtolower($nzbXML->getName()) !== 'nzb') { |
||
121 | $this->echoOut('ERROR: Unable to load NZB XML data: '.$nzbFile); |
||
122 | |||
123 | if ($deleteFailed) { |
||
124 | File::delete($nzbFile); |
||
125 | } |
||
126 | $nzbsSkipped++; |
||
127 | |||
128 | continue; |
||
129 | } |
||
130 | |||
131 | // Try to insert the NZB details into the DB. |
||
132 | try { |
||
133 | $inserted = $this->scanNZBFile($nzbXML, ($useNzbName ? str_ireplace('.nzb', '', basename($nzbFile)) : false)); |
||
134 | } catch (\Exception $e) { |
||
135 | $this->echoOut('ERROR: Problem inserting: '.$nzbFile); |
||
136 | $inserted = false; |
||
137 | } |
||
138 | |||
139 | if ($inserted) { |
||
140 | // Try to copy the NZB to the NZB folder. |
||
141 | $path = $this->nzb->getNZBPath($this->relGuid, 0, true); |
||
142 | |||
143 | // Try to compress the NZB file in the NZB folder. |
||
144 | $fp = gzopen($path, 'w5'); |
||
145 | gzwrite($fp, $nzbString); |
||
146 | gzclose($fp); |
||
147 | |||
148 | if (! File::isFile($path)) { |
||
149 | $this->echoOut('ERROR: Problem compressing NZB file to: '.$path); |
||
150 | |||
151 | // Remove the release. |
||
152 | Release::query()->where('guid', $this->relGuid)->delete(); |
||
153 | |||
154 | if ($deleteFailed) { |
||
155 | File::delete($nzbFile); |
||
156 | } |
||
157 | $nzbsSkipped++; |
||
158 | } else { |
||
159 | $this->updateNzbGuid(); |
||
160 | |||
161 | if ($delete) { |
||
162 | // Remove the nzb file. |
||
163 | File::delete($nzbFile); |
||
164 | } |
||
165 | |||
166 | $nzbsImported++; |
||
167 | } |
||
168 | } else { |
||
169 | $this->echoOut('ERROR: Failed to insert NZB!'); |
||
170 | if ($deleteFailed) { |
||
171 | File::delete($nzbFile); |
||
172 | } |
||
173 | $nzbsSkipped++; |
||
174 | } |
||
175 | } else { |
||
176 | $this->echoOut('ERROR: Unable to fetch: '.$nzbFile); |
||
177 | $nzbsSkipped++; |
||
178 | } |
||
179 | } |
||
180 | $this->echoOut( |
||
181 | 'Proccessed '. |
||
182 | $nzbsImported. |
||
183 | ' NZBs in '. |
||
184 | now()->diffForHumans($start). |
||
185 | $nzbsSkipped. |
||
186 | ' NZBs were skipped.' |
||
187 | ); |
||
188 | |||
189 | if ($this->browser) { |
||
190 | return $this->retVal; |
||
191 | } |
||
192 | |||
193 | return true; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @throws \Exception |
||
198 | */ |
||
199 | protected function scanNZBFile(&$nzbXML, bool $useNzbName = false): bool |
||
309 | ] |
||
310 | ); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Insert the NZB details into the database. |
||
315 | * |
||
316 | * |
||
317 | * @throws \Exception |
||
318 | */ |
||
319 | protected function insertNZB($nzbDetails): bool |
||
320 | { |
||
321 | // Make up a GUID for the release. |
||
322 | $this->relGuid = createGUID(); |
||
323 | |||
324 | // Remove part count from subject. |
||
325 | $partLess = preg_replace('/(\(\d+\/\d+\))*$/', 'yEnc', $nzbDetails['subject']); |
||
326 | // Remove added yEnc from above and anything after. |
||
327 | $subject = mb_convert_encoding(trim(preg_replace('/yEnc.*$/i', 'yEnc', $partLess)), 'UTF-8', mb_list_encodings()); |
||
328 | |||
329 | $renamed = 0; |
||
330 | if ($nzbDetails['useFName']) { |
||
331 | // If the user wants to use the file name.. use it. |
||
332 | $cleanName = $nzbDetails['useFName']; |
||
333 | $renamed = 1; |
||
334 | } else { |
||
335 | // Pass the subject through release cleaner to get a nicer name. |
||
336 | $cleanName = $this->releaseCleaner->releaseCleaner($subject, $nzbDetails['from'], $nzbDetails['groupName']); |
||
337 | if (isset($cleanName['properlynamed'])) { |
||
338 | $cleanName = $cleanName['cleansubject']; |
||
339 | $renamed = (isset($cleanName['properlynamed']) && $cleanName['properlynamed'] === true ? 1 : 0); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | $escapedSubject = $subject; |
||
344 | $escapedFromName = $nzbDetails['from']; |
||
345 | |||
346 | // Look for a duplicate on name, poster and size. |
||
347 | $dupeCheck = Release::query()->where(['name' => $escapedSubject, 'fromname' => $escapedFromName])->whereBetween('size', [$nzbDetails['totalSize'] * 0.99, $nzbDetails['totalSize'] * 1.01])->first(['id']); |
||
348 | |||
349 | if ($dupeCheck === null) { |
||
350 | $escapedSearchName = $cleanName; |
||
351 | $determinedCategory = $this->category->determineCategory($nzbDetails['groups_id'], $cleanName, $escapedFromName); |
||
352 | // Insert the release into the DB. |
||
353 | $relID = Release::insertRelease( |
||
354 | [ |
||
355 | 'name' => $escapedSubject, |
||
356 | 'searchname' => $escapedSearchName ?? $escapedSubject, |
||
357 | 'totalpart' => $nzbDetails['totalFiles'], |
||
358 | 'groups_id' => $nzbDetails['groups_id'], |
||
359 | 'guid' => $this->relGuid, |
||
360 | 'postdate' => $nzbDetails['postDate'], |
||
361 | 'fromname' => $escapedFromName, |
||
362 | 'size' => $nzbDetails['totalSize'], |
||
363 | 'categories_id' => $determinedCategory['categories_id'], |
||
364 | 'isrenamed' => $renamed, |
||
365 | 'reqidstatus' => 0, |
||
366 | 'predb_id' => 0, |
||
367 | 'nzbstatus' => NZB::NZB_ADDED, |
||
368 | ] |
||
369 | ); |
||
370 | } else { |
||
371 | $this->echoOut('This release is already in our DB so skipping: '.$subject); |
||
372 | |||
373 | return false; |
||
374 | } |
||
375 | |||
376 | if ($relID === null) { |
||
377 | $this->echoOut('ERROR: Problem inserting: '.$subject); |
||
378 | |||
379 | return false; |
||
380 | } |
||
381 | |||
382 | return true; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Get all groups in the DB. |
||
387 | */ |
||
388 | protected function getAllGroups(): bool |
||
389 | { |
||
390 | $this->allGroups = []; |
||
391 | $groups = UsenetGroup::query()->get(['id', 'name']); |
||
392 | |||
393 | if ($groups instanceof \Traversable) { |
||
394 | foreach ($groups as $group) { |
||
395 | $this->allGroups[$group['name']] = $group['id']; |
||
396 | } |
||
397 | } |
||
398 | |||
399 | if (\count($this->allGroups) === 0) { |
||
400 | $this->echoOut('You have no groups in your database!'); |
||
401 | |||
402 | return false; |
||
403 | } |
||
404 | |||
405 | return true; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Echo message to browser or CLI. |
||
410 | */ |
||
411 | protected function echoOut(string $message): void |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * The function updates the NZB guid after there is no chance of deletion. |
||
422 | */ |
||
423 | protected function updateNzbGuid(): void |
||
429 | } |
||
430 | } |
||
431 | } |
||
432 |