BatchMigrationTrait   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B filterConnections() 0 10 5
A getDefaultConnection() 0 6 2
B getConnectionsByType() 0 21 6
A setMigrationType() 0 4 1
A runMigrationsOnConnections() 0 12 3
A fire() 0 11 2
A getOptions() 0 8 1
1
<?php namespace Codengine\CustomMigrations;
2
3
use Symfony\Component\Console\Input\InputOption;
4
5
/**
6
 * Class BatchMigrationTrait
7
 * Provides batch migration functions for multiple database connections
8
 *
9
 * @package Codengine\CustomMigrations
10
 */
11
trait BatchMigrationTrait {
12
	/**
13
	 * @var string
14
	 */
15
	protected $migrationType = 'default';
16
17
	/**
18
	 * Extends the default options by type-option
19
	 *
20
	 * @return array
21
	 */
22
	protected function getOptions()
23
	{
24
		$optExtend = array(
25
			array('type', null, InputOption::VALUE_OPTIONAL, 'The migration type to be executed.', 'default'),
26
		);
27
28
		return array_merge(parent::getOptions(), $optExtend);
29
	}
30
31
	/**
32
	 * Filters the connections and only returns the ones that match the migration type
33
	 *
34
	 * @param array $connection The database connections
35
	 * @return bool Returns TRUE on a match, else FALSE
36
	 */
37
	protected function filterConnections($connection)
38
	{
39
		switch($this->migrationType)
40
		{
41
			case 'default':
42
				return (empty($connection['migration_type']) || $connection['migration_type'] == 'default'); break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
43
			default:
44
				return (!empty($connection['migration_type']) && $connection['migration_type'] == $this->migrationType ? true : false); break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
45
		}
46
	}
47
48
	/**
49
	 * Returns the default DB connection
50
	 *
51
	 * @return array
52
	 */
53
	protected function getDefaultConnection()
54
	{
55
		$defaultConnection = \DB::getDefaultConnection();
56
		$connection = \Config::get('database.connections.' . $defaultConnection);
57
		return (empty($connection) ? array() : array($defaultConnection => $connection));
58
	}
59
60
	/**
61
	 * Retrieves database connections by type
62
	 *
63
	 * @param null|string $filter When specified (--database option), only this connection will be checked
64
	 * @return array An array containing the matching connections
65
	 */
66
	protected function getConnectionsByType($filter = null)
67
	{
68
		$connections = array();
0 ignored issues
show
Unused Code introduced by
$connections is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
		if($this->migrationType == "default" && empty($filter)) {
70
			return $this->getDefaultConnection();
71
		} elseif (!empty($filter)) {
72
			$connections = \Config::get('database.connections.' . $filter);
73
			if(!empty($connections))
74
			{
75
				$connections = array($filter => $connections);
76
			}
77
		} else {
78
			$connections = \Config::get('database.connections');
79
		}
80
81
		if(!empty($connections))
82
		{
83
			$connections = array_filter($connections, array($this, 'filterConnections'));
84
		}
85
		return (array)$connections;
86
	}
87
88
	/**
89
	 * Retrieves and sets the migration type
90
	 */
91
	protected function setMigrationType()
92
	{
93
		$this->migrationType = $this->input->getOption('type');
0 ignored issues
show
Bug introduced by
The property input does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
94
	}
95
96
	/**
97
	 * Run a batch migration on the specified connections
98
	 *
99
	 * @param array $connections
100
	 */
101
	protected function runMigrationsOnConnections($connections)
102
	{
103
		foreach($connections as $name => $connection)
104
		{
105
			$this->input->setOption('database', $name);
106
			if(isset($this->migrator))
107
			{
108
				$this->migrator->setMigrationType(array_get($connection, 'migration_type', 'default'));
0 ignored issues
show
Bug introduced by
The property migrator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
			}
110
			parent::fire();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fire() instead of runMigrationsOnConnections()). Are you sure this is correct? If so, you might want to change this to $this->fire().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
111
		}
112
	}
113
114
	/**
115
	 * Default command override
116
	 */
117
	public function fire()
118
	{
119
		$this->setMigrationType();
120
		$connections = $this->getConnectionsByType($this->input->getOption('database'));
121
		if(empty($connections))
122
		{
123
			$this->info("No connections found for the specified migration type");
0 ignored issues
show
Bug introduced by
It seems like info() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
124
		} else {
125
			$this->runMigrationsOnConnections($connections);
126
		}
127
	}
128
}