Passed
Push — master ( 96ef05...f81447 )
by Darko
09:59
created

UpdatePerGroup   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 53
c 1
b 0
f 0
dl 0
loc 113
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 55 4
A processReleases() 0 17 4
A getNntp() 0 11 3
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Settings;
6
use App\Models\UsenetGroup;
7
use Blacklight\Backfill;
8
use Blacklight\Binaries;
9
use Blacklight\Nfo;
10
use Blacklight\NNTP;
11
use Blacklight\processing\post\ProcessAdditional;
12
use Blacklight\processing\ProcessReleases;
13
use Illuminate\Console\Command;
14
use Illuminate\Support\Facades\Log;
15
16
class UpdatePerGroup extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'group:update-all {groupId : Group ID to process}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Do a single group (update_binaries/backFill/update_releases/postprocess)';
31
32
    /**
33
     * Execute the console command.
34
     */
35
    public function handle(): int
36
    {
37
        $groupId = $this->argument('groupId');
38
39
        if (! is_numeric($groupId)) {
40
            $this->error('Group ID must be numeric.');
41
42
            return self::FAILURE;
43
        }
44
45
        try {
46
            $groupMySQL = UsenetGroup::find($groupId)->toArray();
47
48
            if ($groupMySQL === null) {
49
                $this->error("Group not found with id {$groupId}");
50
51
                return self::FAILURE;
52
            }
53
54
            $nntp = $this->getNntp();
55
            $backFill = new Backfill;
56
57
            // Update the group for new binaries
58
            $this->info("Updating binaries for group: {$groupMySQL['name']}");
59
            (new Binaries)->updateGroup($groupMySQL);
60
61
            // BackFill the group with 20k articles
62
            $this->info("Backfilling group: {$groupMySQL['name']}");
63
            $backFill->backfillAllGroups($groupMySQL['name'], 20000, 'normal');
64
65
            // Create releases
66
            $this->info("Processing releases for group: {$groupMySQL['name']}");
67
            $this->processReleases(new ProcessReleases, $groupId);
68
69
            // Post process the releases
70
            $this->info("Post-processing additional for group: {$groupMySQL['name']}");
71
            (new ProcessAdditional(['Echo' => true, 'NNTP' => $nntp]))->start($groupId);
0 ignored issues
show
Unused Code introduced by
The call to Blacklight\processing\po...ditional::__construct() has too many arguments starting with array('Echo' => true, 'NNTP' => $nntp). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            (/** @scrutinizer ignore-call */ new ProcessAdditional(['Echo' => true, 'NNTP' => $nntp]))->start($groupId);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
73
            $this->info("Processing NFO files for group: {$groupMySQL['name']}");
74
            (new Nfo)->processNfoFiles(
75
                $nntp,
76
                $groupId,
77
                '',
78
                (int) Settings::settingValue('lookupimdb'),
0 ignored issues
show
Bug introduced by
(int)App\Models\Settings...tingValue('lookupimdb') of type integer is incompatible with the type boolean expected by parameter $processImdb of Blacklight\Nfo::processNfoFiles(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                /** @scrutinizer ignore-type */ (int) Settings::settingValue('lookupimdb'),
Loading history...
79
                (int) Settings::settingValue('lookuptv')
0 ignored issues
show
Bug introduced by
(int)App\Models\Settings...ettingValue('lookuptv') of type integer is incompatible with the type boolean expected by parameter $processTv of Blacklight\Nfo::processNfoFiles(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
                /** @scrutinizer ignore-type */ (int) Settings::settingValue('lookuptv')
Loading history...
80
            );
81
82
            $this->info("Completed all processing for group: {$groupMySQL['name']}");
83
84
            return self::SUCCESS;
85
        } catch (\Throwable $e) {
86
            Log::error($e->getTraceAsString());
87
            $this->error($e->getMessage());
88
89
            return self::FAILURE;
90
        }
91
    }
92
93
    /**
94
     * Create / process releases for a groupID.
95
     */
96
    private function processReleases(ProcessReleases $releases, string $groupID): void
97
    {
98
        $releaseCreationLimit = Settings::settingValue('maxnzbsprocessed') !== ''
99
            ? (int) Settings::settingValue('maxnzbsprocessed')
100
            : 1000;
101
102
        $releases->processIncompleteCollections($groupID);
103
        $releases->processCollectionSizes($groupID);
104
        $releases->deleteUnwantedCollections($groupID);
105
106
        do {
107
            $releasesCount = $releases->createReleases($groupID);
108
            $nzbFilesAdded = $releases->createNZBs($groupID);
109
        } while ($releaseCreationLimit <= $releasesCount['added'] + $releasesCount['dupes']
110
                 || $nzbFilesAdded >= $releaseCreationLimit);
111
112
        $releases->deleteCollections($groupID);
113
    }
114
115
    /**
116
     * Get NNTP connection.
117
     */
118
    private function getNntp(): NNTP
119
    {
120
        $nntp = new NNTP;
121
122
        if ((config('nntmux_nntp.use_alternate_nntp_server') === true
123
            ? $nntp->doConnect(false, true)
124
            : $nntp->doConnect()) !== true) {
125
            throw new \Exception('Unable to connect to usenet.');
126
        }
127
128
        return $nntp;
129
    }
130
}
131