Passed
Push — master ( bc1873...151d9b )
by Darko
12:38
created

ProcessAdditionalGuid::handle()   D

Complexity

Conditions 18
Paths 22

Size

Total Lines 76
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 46
c 2
b 0
f 0
dl 0
loc 76
rs 4.8666
cc 18
nc 22
nop 0

How to fix   Long Method    Complexity   

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\Release;
6
use Blacklight\processing\post\ProcessAdditional;
0 ignored issues
show
Bug introduced by
The type Blacklight\processing\post\ProcessAdditional was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Console\Command;
8
9
class ProcessAdditionalGuid extends Command
10
{
11
    protected $signature = 'releases:additional
12
                                {guid? : Release GUID (optional if using --id)}
13
                                {--id= : Numeric release ID to run additional postprocessing on (alternative to GUID)}
14
                                {--reset : Reset key additional postprocessing flags before running}
15
                                {--no-progress : Suppress progress / status output (forces quiet mode)}';
16
17
    protected $description = 'Run additional postprocessing for a specific release by GUID or ID regardless of prior postprocessing status';
18
19
    public function handle(): int
20
    {
21
        $guidArg = trim((string) $this->argument('guid'));
22
        $idOpt = $this->option('id');
23
24
        if ($guidArg === '' && ($idOpt === null || $idOpt === '')) {
25
            $this->error('You must supply either a GUID argument or --id=<release id>.');
26
27
            return 1; // missing identifier
28
        }
29
30
        if ($guidArg !== '' && $idOpt !== null && $idOpt !== '') {
31
            $this->error('Provide only one identifier: GUID or --id, not both.');
32
33
            return 4; // conflicting identifiers
34
        }
35
36
        $release = null;
37
        $guid = '';
38
39
        if ($idOpt !== null && $idOpt !== '') {
40
            if (! ctype_digit((string) $idOpt)) {
41
                $this->error('Release ID must be numeric.');
42
43
                return 5; // invalid id format
44
            }
45
            $release = Release::find((int) $idOpt);
46
            if ($release === null) {
47
                $this->error('Release not found for ID: '.$idOpt);
48
49
                return 2; // not found
50
            }
51
            $guid = $release->guid;
0 ignored issues
show
Bug introduced by
The property guid does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property guid does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
52
        } else {
53
            $guid = $guidArg;
54
            if ($guid === '') { // should not happen but guard
55
                $this->error('GUID is required if --id not supplied.');
56
57
                return 1;
58
            }
59
            $release = Release::where('guid', $guid)->first();
60
            if ($release === null) {
61
                $this->error('Release not found for GUID: '.$guid);
62
63
                return 2;
64
            }
65
        }
66
67
        if ($this->option('reset')) {
68
            Release::where('id', $release->id)->update([
69
                'passwordstatus' => -1,
70
                'haspreview' => -1,
71
                'jpgstatus' => 0,
72
                'videostatus' => 0,
73
                'audiostatus' => 0,
74
                'nfostatus' => -1,
75
            ]);
76
            $this->info('Reset postprocessing flags for release ID '.$release->id.' (GUID '.$guid.')');
77
        }
78
79
        if ($this->option('no-progress')) {
80
            config(['nntmux.echocli' => false]);
81
        }
82
83
        $processor = new ProcessAdditional;
84
        $ok = $processor->processSingleGuid($guid);
85
86
        if (! $ok) {
87
            $this->error('Processing failed or nothing processed for '.($idOpt ? 'ID '.$idOpt : 'GUID '.$guid).'.');
88
89
            return 3;
90
        }
91
92
        $this->info('Additional postprocessing completed for '.($idOpt ? 'ID '.$idOpt : 'GUID '.$guid).'.');
93
94
        return 0;
95
    }
96
}
97