Passed
Push — master ( 2d44db...3ed9a4 )
by Darko
06:04
created

RecategorizeReleases::handle()   C

Complexity

Conditions 11
Paths 29

Size

Total Lines 62
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 51
c 2
b 1
f 0
dl 0
loc 62
rs 6.9224
cc 11
nc 29
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\Category;
6
use App\Models\Release;
7
use Blacklight\Categorize;
8
use Blacklight\NameFixer;
9
use Illuminate\Console\Command;
10
11
class RecategorizeReleases extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'nntmux:recategorize-releases
19
    {--misc : Re-categorize all releases in misc categories}
20
    {--all : Re-categorize all releases}
21
    {--test : Test only, no updates}
22
    {--group= : Re-categorize all releases in a group}
23
    {--groups= : Re-categorize all releases in a list of groups}
24
    {--category= : Re-categorize all releases in a category}
25
    {--categories= : Re-categorize all releases in a list of categories}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Re-categorize releases based on their name and group.';
33
34
    /**
35
     * Execute the console command.
36
     */
37
    public function handle()
38
    {
39
        $countQuery = Release::query();
40
        if ($this->option('misc')) {
41
            $countQuery->whereIn('categories_id', Category::OTHERS_GROUP);
42
        } elseif ($this->option('all')) {
43
            $countQuery->where('iscategorized', 0);
44
        } elseif ($this->option('group')) {
45
            $countQuery->where('groups_id', $this->option('group'));
46
        } elseif ($this->option('groups')) {
47
            $countQuery->whereIn('groups_id', explode(',', $this->option('groups')));
48
        } elseif ($this->option('category')) {
49
            $countQuery->where('categories_id', $this->option('category'));
50
        } elseif ($this->option('categories')) {
51
            $countQuery->whereIn('categories_id', explode(',', $this->option('categories')));
52
        } elseif ($this->option('test')) {
53
            $countQuery->where('iscategorized', 0);
54
        } else {
55
            $this->error('You must specify at least one option. See: --help');
56
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
57
        }
58
59
        $count = $countQuery->count();
60
61
        $cat = new Categorize();
62
        $results = $countQuery->select(['id', 'searchname', 'fromname', 'groups_id', 'categories_id'])->get();
63
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
Bug introduced by
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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

63
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
64
        $bar->start();
65
        foreach ($results as $result) {
66
            $bar->advance();
67
            $catId = $cat->determineCategory($result->groups_id, $result->searchname, $result->fromname);
68
            if ((int) $result->categories_id !== (int) $catId['categories_id']) {
69
                if ($this->option('test')) {
70
                    $this->info('Would have changed '.$result->searchname.' from '.$result->categories_id.' to '.$catId['categories_id']);
71
                } else {
72
                    Release::query()->where('id', $result->id)->update([
73
                        'iscategorized' => 1,
74
                        'videos_id' => 0,
75
                        'tv_episodes_id' => 0,
76
                        'imdbid' => null,
77
                        'musicinfo_id' => null,
78
                        'consoleinfo_id' => null,
79
                        'gamesinfo_id' => 0,
80
                        'bookinfo_id' => 0,
81
                        'anidbid' => null,
82
                        'xxxinfo_id' => 0,
83
                        'categories_id' => $catId['categories_id'],
84
                    ]);
85
86
                    NameFixer::echoChangedReleaseName([
87
                        'new_name' => $result->searchname,
88
                        'old_name' => $result->searchname,
89
                        'new_category' => $catId['categories_id'],
90
                        'old_category' => $result->categories_id,
91
                        'group' => $result->group->name,
92
                        'releases_id' => $result->id,
93
                        'method' => 'Recategorize',
94
                    ]);
95
                }
96
            }
97
        }
98
        $bar->finish();
99
    }
100
}
101