Passed
Push — master ( d9c32b...11a9d1 )
by Aimeos
03:50
created

UsersRemoveSignedConstraints::migrate()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 18
nop 0
dl 0
loc 52
rs 8.8817
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Removes signed constraints from users_* tables before migrating to unsigned
14
 */
15
class UsersRemoveSignedConstraints extends \Aimeos\MW\Setup\Task\Base
16
{
17
	/**
18
	 * Returns the list of task names which this task depends on.
19
	 *
20
	 * @return string[] List of task names
21
	 */
22
	public function getPreDependencies()
23
	{
24
		return [];
25
	}
26
27
28
	/**
29
	 * Returns the list of task names which depends on this task.
30
	 *
31
	 * @return array List of task names
32
	 */
33
	public function getPostDependencies()
34
	{
35
		return ['TablesCreateMShop'];
36
	}
37
38
39
	/**
40
	 * Executes the task
41
	 */
42
	public function migrate()
43
	{
44
		$sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \'users\' AND COLUMN_NAME = \'id\'';
45
46
		try {
47
			$type = $this->getValue( $sql, 'COLUMN_TYPE', 'db-customer' );
48
		} catch( \Aimeos\MW\Setup\Exception $e ) {
49
			$type = null;
50
		}
51
52
		if( $type === 'int(10)' )
53
		{
54
			$this->msg( sprintf( 'Remove signed constraints in users related tables' ), 0 );
55
			$this->status( '' );
56
57
58
			$this->msg( 'Checking constraint in "users_address"', 1 );
59
60
			if( $schema->constraintExists( 'users_address', 'fk_lvuad_pid' ) )
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $schema seems to be never defined.
Loading history...
61
			{
62
				$this->execute( 'ALTER TABLE "users_address" DROP FOREIGN KEY "fk_lvuad_pid"', 'db-customer' );
63
				$this->status( 'done' );
64
			}
65
			else
66
			{
67
				$this->status( 'OK' );
68
			}
69
70
71
			$this->msg( 'Checking constraint in "users_list"', 1 );
72
73
			if( $schema->constraintExists( 'users_list', 'fk_lvuli_pid' ) )
74
			{
75
				$this->execute( 'ALTER TABLE "users_list" DROP FOREIGN KEY "fk_lvuli_pid"', 'db-customer' );
76
				$this->status( 'done' );
77
			}
78
			else
79
			{
80
				$this->status( 'OK' );
81
			}
82
83
84
			$this->msg( 'Checking constraint in "users_property"', 1 );
85
86
			if( $schema->constraintExists( 'users_property', 'fk_lvupr_pid' ) )
87
			{
88
				$this->execute( 'ALTER TABLE "users_property" DROP FOREIGN KEY "fk_lvupr_pid"', 'db-customer' );
89
				$this->status( 'done' );
90
			}
91
			else
92
			{
93
				$this->status( 'OK' );
94
			}
95
		}
96
	}
97
}