DatabaseCreateCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 3 1
A connection() 0 23 3
1
<?php
2
3
namespace Epesi\Core\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\App;
8
9
class DatabaseCreateCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'epesi:database-create {name : The name of the database.} 
17
													{--connection=mysql DB connection settings}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create database for the epesi application';
25
    
26
    protected $connection;
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle()
32
    {
33
    	DB::connection($this->connection())->statement('CREATE DATABASE `' . $this->argument('name') . '`');
0 ignored issues
show
Bug introduced by
Are you sure $this->argument('name') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

33
    	DB::connection($this->connection())->statement('CREATE DATABASE `' . /** @scrutinizer ignore-type */ $this->argument('name') . '`');
Loading history...
Bug introduced by
Are you sure the usage of $this->connection() targeting Epesi\Core\Console\Datab...teCommand::connection() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
$this->connection() of type void is incompatible with the type string expected by parameter $name of Illuminate\Support\Facades\DB::connection(). ( Ignorable by Annotation )

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

33
    	DB::connection(/** @scrutinizer ignore-type */ $this->connection())->statement('CREATE DATABASE `' . $this->argument('name') . '`');
Loading history...
34
    }
35
    
36
    /**
37
     * Creates the configuration for the connection and returns the key
38
     * 
39
     * @return void
40
     */
41
    protected function connection()
42
    {
43
    	$connection = $this->option('connection');
44
    	
45
    	// Just get access to the config.
46
    	$config = App::make('config');
47
    	
48
    	// Will contain the array of connections that appear in our database config file.
49
    	$connections = $config->get('database.connections');
50
    	
51
    	$driver = is_string($connection)? $connection: $connection['driver'];
0 ignored issues
show
introduced by
The condition is_string($connection) is always true.
Loading history...
52
53
    	$defaultConnection = $connections[$driver]?? $connections[$config->get('database.default')];
54
    	
55
    	$newConnection = array_merge($defaultConnection, is_string($connection)? []: $connection);
0 ignored issues
show
introduced by
The condition is_string($connection) is always true.
Loading history...
56
57
    	// Do not select database
58
    	$newConnection['database'] = '';
59
60
    	// This will add our new connection to the run-time configuration for the duration of the request.
61
    	$config->set('database.connections.create-db', $newConnection);
62
63
    	return 'create-db';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'create-db' returns the type string which is incompatible with the documented return type void.
Loading history...
64
    }
65
}
66