Passed
Push — master ( d127a4...965e5a )
by Darko
10:49
created

UpdatePostProcess::processAmazon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Blacklight\NNTP;
6
use Blacklight\processing\PostProcess;
7
use Illuminate\Console\Command;
8
9
class UpdatePostProcess extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'update:postprocess
17
                            {type : Type (all, nfo, movies, tv, music, console, games, book, anime, xxx, additional, amazon)}
18
                            {echo? : Echo output (true/false, default: true)}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Post-process releases by type';
26
27
    /**
28
     * Valid types and whether they require NNTP.
29
     */
30
    private array $validTypes = [
31
        'additional' => false,
32
        'all' => true,
33
        'amazon' => false, // Alias for book, music, console, games, xxx
34
        'anime' => false,
35
        'book' => false,
36
        'console' => false,
37
        'games' => false,
38
        'movies' => false,
39
        'music' => false,
40
        'nfo' => true,
41
        'tv' => false,
42
        'xxx' => false,
43
    ];
44
45
    /**
46
     * Execute the console command.
47
     */
48
    public function handle(): int
49
    {
50
        $type = $this->argument('type');
51
        $echoArg = $this->argument('echo');
52
        $echo = ($echoArg === null || $echoArg === 'true');
0 ignored issues
show
Unused Code introduced by
The assignment to $echo is dead and can be removed.
Loading history...
53
54
        if (! array_key_exists($type, $this->validTypes)) {
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type array; however, parameter $key of array_key_exists() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

54
        if (! array_key_exists(/** @scrutinizer ignore-type */ $type, $this->validTypes)) {
Loading history...
55
            $this->error("Invalid type: {$type}");
56
            $this->showHelp();
57
58
            return self::FAILURE;
59
        }
60
61
        try {
62
            $nntp = $this->validTypes[$type] ? $this->getNntp() : null;
63
            $postProcess = new PostProcess;
64
65
            match ($type) {
66
                'all' => $postProcess->processAll($nntp),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processAll($nntp) targeting Blacklight\processing\PostProcess::processAll() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
                'amazon' => $this->processAmazon($postProcess),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processAmazon($postProcess) targeting App\Console\Commands\Upd...rocess::processAmazon() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
                'nfo' => $postProcess->processNfos($nntp),
0 ignored issues
show
Bug introduced by
It seems like $nntp can also be of type null; however, parameter $nntp of Blacklight\processing\PostProcess::processNfos() does only seem to accept Blacklight\NNTP, maybe add an additional type check? ( Ignorable by Annotation )

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

68
                'nfo' => $postProcess->processNfos(/** @scrutinizer ignore-type */ $nntp),
Loading history...
Bug introduced by
Are you sure the usage of $postProcess->processNfos($nntp) targeting Blacklight\processing\PostProcess::processNfos() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
                'movies' => $postProcess->processMovies(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processMovies() targeting Blacklight\processing\PostProcess::processMovies() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
                'music' => $postProcess->processMusic(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processMusic() targeting Blacklight\processing\PostProcess::processMusic() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
                'console' => $postProcess->processConsoles(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processConsoles() targeting Blacklight\processing\Po...cess::processConsoles() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
                'games' => $postProcess->processGames(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processGames() targeting Blacklight\processing\PostProcess::processGames() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
                'book' => $postProcess->processBooks(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processBooks() targeting Blacklight\processing\PostProcess::processBooks() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
                'anime' => $postProcess->processAnime(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processAnime() targeting Blacklight\processing\PostProcess::processAnime() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
                'tv' => $postProcess->processTv(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processTv() targeting Blacklight\processing\PostProcess::processTv() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
                'xxx' => $postProcess->processXXX(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processXXX() targeting Blacklight\processing\PostProcess::processXXX() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
77
                'additional' => $postProcess->processAdditional(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $postProcess->processAdditional() targeting Blacklight\processing\Po...ss::processAdditional() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
78
                default => throw new \Exception("Unhandled type: {$type}"),
79
            };
80
81
            return self::SUCCESS;
82
        } catch (\Throwable $e) {
83
            $this->error($e->getMessage());
84
85
            return self::FAILURE;
86
        }
87
    }
88
89
    /**
90
     * Show help text.
91
     */
92
    private function showHelp(): void
93
    {
94
        $this->line('');
95
        $this->line('Available types:');
96
        $this->line('  all         - Does all the types of post processing');
97
        $this->line('  nfo         - Processes NFO files');
98
        $this->line('  movies      - Processes movies');
99
        $this->line('  music       - Processes music');
100
        $this->line('  console     - Processes console games');
101
        $this->line('  games       - Processes games');
102
        $this->line('  book        - Processes books');
103
        $this->line('  anime       - Processes anime');
104
        $this->line('  tv          - Processes tv');
105
        $this->line('  xxx         - Processes xxx');
106
        $this->line('  additional  - Processes previews/mediainfo/etc...');
107
        $this->line('  amazon      - Processes books, music, console, games, and xxx');
108
    }
109
110
    /**
111
     * Process amazon types (books, music, console, games, xxx).
112
     */
113
    private function processAmazon(PostProcess $postProcess): void
114
    {
115
        $postProcess->processBooks();
116
        $postProcess->processMusic();
117
        $postProcess->processConsoles();
118
        $postProcess->processGames();
119
        $postProcess->processXXX();
120
    }
121
122
    /**
123
     * Get NNTP connection.
124
     */
125
    private function getNntp(): NNTP
126
    {
127
        $nntp = new NNTP;
128
129
        if ((config('nntmux_nntp.use_alternate_nntp_server') === true
130
            ? $nntp->doConnect(false, true)
131
            : $nntp->doConnect()) !== true) {
132
            throw new \Exception('Unable to connect to usenet.');
133
        }
134
135
        return $nntp;
136
    }
137
}
138