Issues (746)

app/Console/Commands/PartRepair.php (1 issue)

Severity
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\UsenetGroup;
6
use App\Services\Binaries\BinariesService;
7
use Blacklight\NNTP;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\Log;
10
11
class PartRepair extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'binaries:part-repair {group : Group name}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Do part repair for a group';
26
27
    /**
28
     * Execute the console command.
29
     */
30
    public function handle(): int
31
    {
32
        $groupName = $this->argument('group');
33
34
        try {
35
            $groupMySQL = UsenetGroup::getByName($groupName)->toArray();
36
37
            if ($groupMySQL === null) {
0 ignored issues
show
The condition $groupMySQL === null is always false.
Loading history...
38
                $this->error("Group not found: {$groupName}");
39
40
                return self::FAILURE;
41
            }
42
43
            $nntp = $this->getNntp();
44
45
            $data = $nntp->selectGroup($groupMySQL['name']);
46
47
            if (NNTP::isError($data) && $nntp->dataError($nntp, $groupMySQL['name']) === false) {
48
                return self::FAILURE;
49
            }
50
51
            $binaries = new BinariesService();
52
            $binaries->setNntp($nntp);
53
            $binaries->partRepair($groupMySQL);
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(): NNTP
68
    {
69
        $nntp = new 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