GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — feature/scrutinizer-code-cover... ( 661209...f9e000 )
by Peter
02:06
created

DbRebuild::handle()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 5
nop 0
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 12
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Webparking\DbRebuild\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Console\Kernel;
7
use Illuminate\Database\Connection;
8
use Illuminate\Support\Collection;
9
use Webparking\DbRebuild\Config;
10
11
class DbRebuild extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'db:rebuild {--preset=default} {--f}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Saves sessions & users table, drops everything, migrates, seeds and optionally devSeeds';
26
27
    /**
28
     * @var Connection
29
     */
30
    private $connection;
31
32
    /**
33
     * @var Kernel
34
     */
35
    private $artisan;
36
37
    /**
38
     * @var \Illuminate\Support\Collection
39
     */
40
    protected $backup;
41
42
    /**
43
     * @var Config
44
     */
45
    protected $config;
46
47
    public function __construct(Connection $connection, Kernel $artisan)
48
    {
49
        $this->connection = $connection;
50
        $this->artisan = $artisan;
51
52
        parent::__construct();
53
    }
54
55
    public function handle(): void
56
    {
57
        if (app()->environment('production')) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

57
        if (app()->/** @scrutinizer ignore-call */ environment('production')) {
Loading history...
58
            $this->error('Rebuilding in production is not allowed!');
59
60
            return;
61
        }
62
63
        $this->config = new Config($this->option('preset'));
64
65
        $this->backupData();
66
67
        try {
68
            $this->migrate();
69
            $this->callCommands();
70
            $this->seed();
71
        } catch (\Exception $e) {
72
            $this->restoreData();
73
74
            throw $e;
75
        }
76
77
        $this->restoreData();
78
    }
79
80
    private function backupData(): void
81
    {
82
        $this->info('Backing up data');
83
84
        $this->backup = collect();
85
86
        foreach ($this->config->getBackup() as $table) {
87
            if ($this->connection->getSchemaBuilder()->hasTable($table)) {
88
                $this->info('Backing up ' . $table);
89
                $this->backup->put($table, $this->connection->table($table)->get());
90
                continue;
91
            }
92
93
            $this->warn('Table not found: ' . $table);
94
        }
95
    }
96
97
    private function restoreData(): void
98
    {
99
        $this->backup->each(function (Collection $data, string $table) {
100
            if ($this->connection->getSchemaBuilder()->hasTable($table)) {
101
                $this->info('Restoring ' . $table);
102
103
                $data->each(function ($record) use ($table) {
104
                    $this->connection->table($table)->insert(get_object_vars($record));
105
                });
106
107
                return;
108
            }
109
        });
110
    }
111
112
    private function migrate(): void
113
    {
114
        $database = $this->config->getDatabase();
115
116
        if ($this->realConfirm(
117
            "This will drop all tables in {$database}. Are you sure you want to do this? [yes|no]",
118
            true
119
        )) {
120
            $this->connection->statement('SET FOREIGN_KEY_CHECKS=0;');
121
            $tables = $this->connection->select('SHOW TABLES');
122
123
            foreach ($tables as $table) {
124
                $tableName = $table->{key($table)};
125
                $this->info('dropping table ' . $tableName);
126
                $this->connection->getSchemaBuilder()->dropIfExists($tableName);
127
            }
128
129
            $this->connection->statement('SET FOREIGN_KEY_CHECKS=1;');
130
131
            $this->info("\nAll tables in {$database} dropped!");
132
133
            $this->info("\nMigration started");
134
            $this->artisan->call('migrate');
135
            $this->info('Migration finished');
136
        }
137
    }
138
139
    private function callCommands(): void
140
    {
141
        foreach ($this->config->getCommands() as $command) {
142
            $this->info('Calling command: ' . $command);
143
            $this->artisan->call($command);
144
        }
145
    }
146
147
    private function seed(): void
148
    {
149
        foreach ($this->config->getSeeds() as $seed) {
150
            $this->info('Calling seeder: ' . $seed);
151
            $this->artisan->call('db:seed', [
152
                '--class' => $seed,
153
            ]);
154
        }
155
    }
156
157
    /**
158
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
159
     */
160
    private function realConfirm(string $msg, bool $default = false): bool
161
    {
162
        if ($this->option('f')) {
163
            return true;
164
        }
165
166
        $answer = $this->choice($msg, ['No', 'Yes'], (true === $default) ? '1' : '0');
167
        switch ($answer) {
168
            case 'No':
169
                return false;
170
            case 'Yes':
171
                return true;
172
        }
173
    }
174
}
175