DbRestore::getFilePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Commands;
6
7
use DB;
8
use Illuminate\Console\Command;
9
use Illuminate\Console\ConfirmableTrait;
10
11
use function file_exists;
12
use function file_get_contents;
13
14
class DbRestore extends Command
15
{
16
    use ConfirmableTrait;
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'db:restore {file : Dump file path to restore.}
24
                    {--database= : The database connection to use.}
25
                    {--force : Force the operation to run when in production.}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Restore mysql dump.';
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39
    public function handle(): void
40
    {
41
        if (! $this->confirmToProceed('Application In Production! Will be imported sql file!')) {
42
            return;
43
        }
44
45
        $dbConn = $this->getDatabase();
46
        $connection = DB::connection($dbConn);
47
48
        $path = $this->getFilePath();
49
        if (! file_exists($path)) {
50
            $this->error('File ' . $path . ' not found!');
51
52
            return;
53
        }
54
55
        $connection->unprepared(file_get_contents($path));
56
57
        $this->info('Database backup restored successfully');
58
    }
59
60
    protected function getDatabase(): string
61
    {
62
        $database = $this->input->getOption('database');
63
64
        return $database ?: $this->laravel['config']['database.default'];
65
    }
66
67
    protected function getFilePath(): string
68
    {
69
        $file = $this->input->getArgument('file');
70
71
        return base_path($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->input->getArgument('file') on line 69 can also be of type array<integer,string> or null; however, base_path() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
72
    }
73
}
74