Completed
Push — dev ( e5a31e...27c800 )
by Darko
06:50
created

NntmuxResetDb::handle()   C

Complexity

Conditions 10
Paths 31

Size

Total Lines 102
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
eloc 74
c 0
b 0
f 0
dl 0
loc 102
ccs 0
cts 33
cp 0
rs 6.7006
cc 10
nc 31
nop 0
crap 110

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\Settings;
6
use App\Models\UsenetGroup;
7
use Blacklight\SphinxSearch;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\File;
11
12
class NntmuxResetDb extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'nntmux:resetdb';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'This command will reset your database to blank state (will retain settings)';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * @throws \Exception
40
     */
41
    public function handle()
42
    {
43
        if ($this->confirm('This script removes all releases, nzb files, samples, previews , nfos, truncates all article tables and resets all groups. Are you sure you want reset the DB?')) {
44
            $timestart = now();
45
46
            DB::statement('SET FOREIGN_KEY_CHECKS = 0;');
47
48
            UsenetGroup::query()->update([
49
                'first_record' => 0,
50
                'first_record_postdate' => null,
51
                'last_record' => 0,
52
                'last_record_postdate' => null,
53
                'last_updated' => null,
54
            ]);
55
            $this->info('Reseting all groups completed.');
56
57
            $arr = [
58
                'videos',
59
                'tv_episodes',
60
                'tv_info',
61
                'release_nfos',
62
                'release_comments',
63
                'sharing',
64
                'sharing_sites',
65
                'users_releases',
66
                'user_movies',
67
                'user_series',
68
                'movieinfo',
69
                'musicinfo',
70
                'release_files',
71
                'audio_data',
72
                'release_subtitles',
73
                'video_data',
74
                'releaseextrafull',
75
                'releases',
76
                'anidb_titles',
77
                'anidb_info',
78
                'anidb_episodes',
79
                'releases_groups',
80
            ];
81
            foreach ($arr as &$value) {
82
                DB::statement("TRUNCATE TABLE $value");
83
                $this->info('Truncating '.$value.' completed.');
84
            }
85
            unset($value);
86
            $this->info('Truncating binaries, collections, missed_parts and parts tables...');
87
            DB::statement("CALL loop_cbpm('truncate')");
88
            $this->info('Truncating completed.');
89
90
            if (config('nntmux.elasticsearch_enabled') === true) {
91
                if (Elasticsearch::indices()->exists(['index' => 'releases'])) {
0 ignored issues
show
Bug introduced by
The type App\Console\Commands\Elasticsearch 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...
92
                    Elasticsearch::indices()->delete(['index' => 'releases']);
93
                }
94
                $releases_index = [
95
                    'index' => 'releases',
96
                    'body'  => [
97
                        'settings' => [
98
                            'number_of_shards' => 2,
99
                            'number_of_replicas' => 0
100
                        ]
101
                    ]
102
                ];
103
104
                Elasticsearch::indices()->create($releases_index);
105
106
                if (Elasticsearch::indices()->exists(['index' => 'predb'])) {
107
                    Elasticsearch::indices()->delete(['index' => 'predb']);
108
                }
109
                $predb_index = [
110
                    'index' => 'predb',
111
                    'body'  => [
112
                        'settings' => [
113
                            'number_of_shards' => 2,
114
                            'number_of_replicas' => 0
115
                        ]
116
                    ]
117
                ];
118
119
                Elasticsearch::indices()->create($predb_index);
120
121
                $this->info('All done! ElasticSearch indexes are deleted and recreated.');
122
            } else {
123
                (new SphinxSearch())->truncateRTIndex(['releases_rt', 'predb_rt']);
124
            }
125
126
            $this->info('Deleting nzbfiles subfolders.');
127
            $files = File::allFiles(Settings::settingValue('..nzbpath'));
128
            File::delete($files);
129
130
            $this->info('Deleting all images, previews and samples that still remain.');
131
132
            $files = File::allFiles(NN_COVERS);
133
            foreach ($files as $file) {
134
                if (basename($file) !== '.gitignore' && basename($file) !== 'no-cover.jpg' && basename($file) !== 'no-backdrop.jpg') {
135
                    File::delete($file);
136
                }
137
            }
138
139
            $this->info('Deleted all releases, images, previews and samples. This script finished '.now()->diffForHumans($timestart).' start');
140
            DB::statement('SET FOREIGN_KEY_CHECKS = 1;');
141
        } else {
142
            $this->info('Script execution stopped');
143
        }
144
    }
145
}
146