Issues (995)

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

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Console\Commands;
6
7
use App\Services\PostProcessService;
8
use App\Services\NNTP\NNTPService;
9
use Illuminate\Console\Command;
10
11
class UpdatePostProcess extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'update:postprocess
19
                            {type : Type (all, nfo, movies, tv, music, console, games, book, anime, xxx, additional, amazon)}
20
                            {echo? : Echo output (true/false, default: true)}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Post-process releases by type';
28
29
    /**
30
     * Valid types and whether they require NNTP.
31
     *
32
     * @var array<string, bool>
33
     */
34
    private const array VALID_TYPES = [
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 34 at column 24
Loading history...
35
        'additional' => false,
36
        'all' => true,
37
        'amazon' => false,
38
        'anime' => false,
39
        'book' => false,
40
        'console' => false,
41
        'games' => false,
42
        'movies' => false,
43
        'music' => false,
44
        'nfo' => true,
45
        'tv' => false,
46
        'xxx' => false,
47
    ];
48
49
    public function __construct(
50
        private readonly PostProcessService $postProcessService
51
    ) {
52
        parent::__construct();
53
    }
54
55
    /**
56
     * Execute the console command.
57
     */
58
    public function handle(): int
59
    {
60
        $type = $this->argument('type');
61
62
        if (!array_key_exists($type, self::VALID_TYPES)) {
63
            $this->error("Invalid type: {$type}");
64
            $this->showHelp();
65
66
            return self::FAILURE;
67
        }
68
69
        try {
70
            $nntp = self::VALID_TYPES[$type] ? $this->getNntp() : null;
71
72
            match ($type) {
73
                'all' => $this->postProcessService->processAll($nntp),
74
                'amazon' => $this->processAmazon(),
75
                'nfo' => $this->postProcessService->processNfos($nntp),
76
                'movies' => $this->postProcessService->processMovies(),
77
                'music' => $this->postProcessService->processMusic(),
78
                'console' => $this->postProcessService->processConsoles(),
79
                'games' => $this->postProcessService->processGames(),
80
                'book' => $this->postProcessService->processBooks(),
81
                'anime' => $this->postProcessService->processAnime(),
82
                'tv' => $this->postProcessService->processTv(),
83
                'xxx' => $this->postProcessService->processXXX(),
84
                'additional' => $this->postProcessService->processAdditional(),
85
                default => throw new \Exception("Unhandled type: {$type}"),
86
            };
87
88
            return self::SUCCESS;
89
        } catch (\Throwable $e) {
90
            $this->error($e->getMessage());
91
92
            return self::FAILURE;
93
        }
94
    }
95
96
    /**
97
     * Show help text.
98
     */
99
    private function showHelp(): void
100
    {
101
        $this->line('');
102
        $this->line('Available types:');
103
        $this->line('  all         - Does all the types of post processing');
104
        $this->line('  nfo         - Processes NFO files');
105
        $this->line('  movies      - Processes movies');
106
        $this->line('  music       - Processes music');
107
        $this->line('  console     - Processes console games');
108
        $this->line('  games       - Processes games');
109
        $this->line('  book        - Processes books');
110
        $this->line('  anime       - Processes anime');
111
        $this->line('  tv          - Processes tv');
112
        $this->line('  xxx         - Processes xxx');
113
        $this->line('  additional  - Processes previews/mediainfo/etc...');
114
        $this->line('  amazon      - Processes books, music, console, games, and xxx');
115
    }
116
117
    /**
118
     * Process amazon types (books, music, console, games, xxx).
119
     */
120
    private function processAmazon(): void
121
    {
122
        $this->postProcessService->processBooks();
123
        $this->postProcessService->processMusic();
124
        $this->postProcessService->processConsoles();
125
        $this->postProcessService->processGames();
126
        $this->postProcessService->processXXX();
127
    }
128
129
    /**
130
     * Get NNTP connection.
131
     */
132
    private function getNntp(): NNTPService
133
    {
134
        $nntp = new NNTPService();
135
136
        if ((config('nntmux_nntp.use_alternate_nntp_server') === true
137
            ? $nntp->doConnect(false, true)
138
            : $nntp->doConnect()) !== true) {
139
            throw new \RuntimeException('Unable to connect to usenet.');
140
        }
141
142
        return $nntp;
143
    }
144
}
145