PostProcessGuid::handle()   B
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 58
rs 7.7084
cc 9
nc 17
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Settings;
6
use Blacklight\Nfo;
7
use Blacklight\NNTP;
8
use Blacklight\processing\post\ProcessAdditional;
9
use Blacklight\processing\PostProcess;
10
use Illuminate\Console\Command;
11
use Illuminate\Support\Facades\Log;
12
13
class PostProcessGuid extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'postprocess:guid
21
                            {type : Type: additional, nfo, movie, tv, anime, or books}
22
                            {guid : First character of release guid (a-f, 0-9)}
23
                            {renamed? : For movie/tv: process renamed only (optional)}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Post process releases by GUID character';
31
32
    /**
33
     * Execute the console command.
34
     */
35
    public function handle(): int
36
    {
37
        $type = $this->argument('type');
38
        $guid = $this->argument('guid');
39
        $renamed = $this->argument('renamed') ?? '';
40
41
        if (! $this->isValidChar($guid)) {
42
            $this->error('GUID character must be a-f or 0-9.');
43
44
            return self::FAILURE;
45
        }
46
47
        try {
48
            switch ($type) {
49
                case 'additional':
50
                    $nntp = $this->getNntp();
51
                    (new ProcessAdditional(['Echo' => true, 'NNTP' => $nntp]))->start('', $guid);
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

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

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...
52
                    break;
53
54
                case 'nfo':
55
                    $nntp = $this->getNntp();
56
                    (new Nfo)->processNfoFiles(
57
                        $nntp,
58
                        '',
59
                        $guid,
60
                        (int) Settings::settingValue('lookupimdb'),
61
                        (int) Settings::settingValue('lookuptv')
62
                    );
63
                    break;
64
65
                case 'movie':
66
                    (new PostProcess)->processMovies('', $guid, $renamed);
67
                    break;
68
69
                case 'tv':
70
                    (new PostProcess)->processTv('', $guid, $renamed);
71
                    break;
72
73
                case 'anime':
74
                    (new PostProcess)->processAnime('', $guid);
75
                    break;
76
77
                case 'books':
78
                    (new PostProcess)->processBooks('', $guid);
79
                    break;
80
81
                default:
82
                    $this->error('Invalid type. Must be: additional, nfo, movie, tv, anime, or books.');
83
84
                    return self::FAILURE;
85
            }
86
87
            return self::SUCCESS;
88
        } catch (\Throwable $e) {
89
            Log::error($e->getTraceAsString());
90
            $this->error($e->getMessage());
91
92
            return self::FAILURE;
93
        }
94
    }
95
96
    /**
97
     * Check if the character contains a-f or 0-9.
98
     */
99
    private function isValidChar(string $char): bool
100
    {
101
        return \in_array(
102
            $char,
103
            ['a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
104
            true
105
        );
106
    }
107
108
    /**
109
     * Get NNTP connection.
110
     */
111
    private function getNntp(): NNTP
112
    {
113
        $nntp = new NNTP;
114
115
        if ((config('nntmux_nntp.use_alternate_nntp_server') === true
116
            ? $nntp->doConnect(false, true)
117
            : $nntp->doConnect()) !== true) {
118
            throw new \Exception('Unable to connect to usenet.');
119
        }
120
121
        return $nntp;
122
    }
123
}
124