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.
Passed
Push — feature/scrutinizer-code-cover... ( 88f5e0...661209 )
by Peter
02:18
created

DbRebuild::restoreData()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 0
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
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 13
    public function __construct(Connection $connection, Kernel $artisan)
48
    {
49 13
        $this->connection = $connection;
50 13
        $this->artisan = $artisan;
51
52 13
        parent::__construct();
53 13
    }
54
55 13
    public function handle(): void
56
    {
57 13
        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 13
        $this->config = new Config($this->option('preset'));
64
65 11
        $this->backupData();
66
67
        try {
68 10
            $this->migrate();
69 9
            $this->callCommands();
70 8
            $this->seed();
71 3
        } catch (\Exception $e) {
72 3
            $this->restoreData();
73
74 3
            throw $e;
75
        }
76
77 7
        $this->restoreData();
78 7
    }
79
80 11
    private function backupData(): void
81
    {
82 11
        $this->info('Backing up data');
83
84 11
        $this->backup = collect();
85
86 11
        foreach ($this->config->getBackup() as $table) {
87 2
            if ($this->connection->getSchemaBuilder()->hasTable($table)) {
88 1
                $this->info('Backing up ' . $table);
89 1
                $this->backup->put($table, $this->connection->table($table)->get());
90 1
                continue;
91
            }
92
93 1
            $this->warn('Table not found: ' . $table);
94
        }
95 10
    }
96
97 10
    private function restoreData(): void
98
    {
99
        $this->backup->each(function (Collection $data, string $table) {
100 1
            if ($this->connection->getSchemaBuilder()->hasTable($table)) {
101 1
                $this->info('Restoring ' . $table);
102
103
                $data->each(function ($record) use ($table) {
104 1
                    $this->connection->table($table)->insert(get_object_vars($record));
105 1
                });
106
107 1
                return;
108
            }
109
110
            $this->error('Table not found: ' . $table);
111 10
        });
112 10
    }
113
114 10
    private function migrate(): void
115
    {
116 10
        $database = $this->config->getDatabase();
117
118 9
        if ($this->realConfirm(
119 9
            "This will drop all tables in {$database}. Are you sure you want to do this? [yes|no]",
120 9
            true
121
        )) {
122 8
            $this->connection->statement('SET FOREIGN_KEY_CHECKS=0;');
123 8
            $tables = $this->connection->select('SHOW TABLES');
124
125 8
            foreach ($tables as $table) {
126 8
                $tableName = $table->{key($table)};
127 8
                $this->info('dropping table ' . $tableName);
128 8
                $this->connection->getSchemaBuilder()->dropIfExists($tableName);
129
            }
130
131 8
            $this->connection->statement('SET FOREIGN_KEY_CHECKS=1;');
132
133 8
            $this->info("\nAll tables in {$database} dropped!");
134
135 8
            $this->info("\nMigration started");
136 8
            $this->artisan->call('migrate');
137 8
            $this->info('Migration finished');
138
        }
139 9
    }
140
141 9
    private function callCommands(): void
142
    {
143 9
        foreach ($this->config->getCommands() as $command) {
144 1
            $this->info('Calling command: ' . $command);
145 1
            $this->artisan->call($command);
146
        }
147 8
    }
148
149 8
    private function seed(): void
150
    {
151 8
        foreach ($this->config->getSeeds() as $seed) {
152 1
            $this->info('Calling seeder: ' . $seed);
153 1
            $this->artisan->call('db:seed', [
154 1
                '--class' => $seed,
155
            ]);
156
        }
157 7
    }
158
159
    /**
160
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
161
     */
162 9
    private function realConfirm(string $msg, bool $default = false): bool
163
    {
164 9
        if ($this->option('f')) {
165 1
            return true;
166
        }
167
168 8
        $answer = $this->choice($msg, ['No', 'Yes'], (true === $default) ? '1' : '0');
169
        switch ($answer) {
170 8
            case 'No':
171 1
                return false;
172 7
            case 'Yes':
173 7
                return true;
174
        }
175
    }
176
}
177