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

BackfillGroup   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 1
b 0
f 0
dl 0
loc 68
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getNntp() 0 11 3
A handle() 0 29 5
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Settings;
6
use Blacklight\Backfill;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Log;
9
10
class BackfillGroup extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'backfill:group
18
                            {group : The group name to backfill}
19
                            {type=1 : Backfill type (1=interval, 2=all)}
20
                            {quantity? : Number of articles to backfill}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Backfill a specific usenet group';
28
29
    /**
30
     * Execute the console command.
31
     */
32
    public function handle(): int
33
    {
34
        $group = $this->argument('group');
35
        $type = (int) $this->argument('type');
36
        $quantity = $this->argument('quantity');
37
38
        if (! \in_array($type, [1, 2], true)) {
39
            $this->error('Invalid backfill type. Must be 1 (interval) or 2 (all).');
40
41
            return self::FAILURE;
42
        }
43
44
        try {
45
            $nntp = $this->getNntp();
0 ignored issues
show
Unused Code introduced by
The assignment to $nntp is dead and can be removed.
Loading history...
46
47
            if ($quantity === null) {
48
                $value = Settings::settingValue('backfill_qty');
49
                $quantity = ($type === 1 ? '' : $value);
50
            }
51
52
            $this->info("Backfilling group: {$group}");
53
            (new Backfill)->backfillAllGroups($group, $quantity);
54
55
            return self::SUCCESS;
56
        } catch (\Throwable $e) {
57
            Log::error($e->getTraceAsString());
58
            $this->error($e->getMessage());
59
60
            return self::FAILURE;
61
        }
62
    }
63
64
    /**
65
     * Get NNTP connection.
66
     */
67
    private function getNntp(): \Blacklight\NNTP
68
    {
69
        $nntp = new \Blacklight\NNTP;
70
71
        if ((config('nntmux_nntp.use_alternate_nntp_server') === true
72
            ? $nntp->doConnect(false, true)
73
            : $nntp->doConnect()) !== true) {
74
            throw new \Exception('Unable to connect to usenet.');
75
        }
76
77
        return $nntp;
78
    }
79
}
80