Completed
Push — master ( ac66b3...ab3b6f )
by Aimeos
07:35 queued 01:23
created

EzuserAddAddress::getPostDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds the required address fields to ezuser table
14
 */
15
class EzuserAddAddress extends \Aimeos\MW\Setup\Task\TablesCreateMShop
16
{
17
	/**
18
	 * Returns the list of task names which depends on this task.
19
	 *
20
	 * @return array List of task names
21
	 */
22
	public function getPostDependencies()
23
	{
24
		return array();
25
	}
26
27
28
	/**
29
	 * Migrate database schema
30
	 */
31
	public function migrate()
32
	{
33
		$this->msg( 'Adding address fields to ezuser table', 0 );
34
35
		$dbal = $this->getConnection( 'db-customer' )->getRawObject();
36
37
		if( !( $dbal instanceof \Doctrine\DBAL\Connection ) ) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Connection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
38
			throw new \Aimeos\MW\Setup\Exception( 'Not a DBAL connection' );
39
		}
40
41
42
		$fromSchema = $dbal->getSchemaManager()->createSchema();
43
		$comparator = new \Doctrine\DBAL\Schema\Comparator();
0 ignored issues
show
Unused Code introduced by
$comparator 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...
44
		$toSchema = clone $fromSchema;
45
46
		$this->addIndexes( $this->addColumns( $toSchema->getTable( 'ezuser' ) ) );
47
		$sql = $fromSchema->getMigrateToSql( $toSchema, $dbal->getDatabasePlatform() );
48
49
		if( $sql !== array() )
50
		{
51
			$this->executeList( $sql, 'db-customer' );
52
			$this->status( 'done' );
53
		}
54
		else
55
		{
56
			$this->status( 'OK' );
57
		}
58
	}
59
60
61
	/**
62
	 * Adds the missing columns to the table
63
	 *
64
	 * @param \Doctrine\DBAL\Schema\Table $table Table object
65
	 * @return \Doctrine\DBAL\Schema\Table Updated table object
66
	 */
67
	protected function addColumns( \Doctrine\DBAL\Schema\Table $table )
68
	{
69
		$columns = array(
70
			'company' => array( 'string', array( 'length' => 100 ) ),
71
			'vatid' => array( 'string', array( 'length' => 32 ) ),
72
			'salutation' => array( 'string', array( 'length' => 8 ) ),
73
			'title' => array( 'string', array( 'length' => 64 ) ),
74
			'firstname' => array( 'string', array( 'length' => 64 ) ),
75
			'address1' => array( 'string', array( 'length' => 255 ) ),
76
			'address2' => array( 'string', array( 'length' => 255 ) ),
77
			'address3' => array( 'string', array( 'length' => 255 ) ),
78
			'postal' => array( 'string', array( 'length' => 16 ) ),
79
			'city' => array( 'string', array( 'length' => 255 ) ),
80
			'state' => array( 'string', array( 'length' => 255 ) ),
81
			'langid' => array( 'string', array( 'length' => 5, 'notnull' => false ) ),
82
			'countryid' => array( 'string', array( 'length' => 2, 'notnull' => false, 'fixed' => true ) ),
83
			'telephone' => array( 'string', array( 'length' => 32 ) ),
84
			'telefax' => array( 'string', array( 'length' => 32 ) ),
85
			'website' => array( 'string', array( 'length' => 255 ) ),
86
			'birthday' => array( 'date', array( 'notnull' => false ) ),
87
			'vdate' => array( 'date', array( 'notnull' => false ) ),
88
			'status' => array( 'smallint', array() ),
89
			'mtime' => array( 'datetime', array() ),
90
			'ctime' => array( 'datetime', array() ),
91
			'editor' => array( 'string', array( 'length' => 255 ) ),
92
		);
93
94
		foreach( $columns as $name => $def )
95
		{
96
			if( $table->hasColumn( $name ) === false ) {
97
				$table->addColumn( $name, $def[0], $def[1] );
98
			}
99
		}
100
101
		return $table;
102
	}
103
104
105
	/**
106
	 * Adds the missing indexes to the table
107
	 *
108
	 * @param \Doctrine\DBAL\Schema\Table $table Table object
109
	 * @return \Doctrine\DBAL\Schema\Table Updated table object
110
	 */
111
	protected function addIndexes( \Doctrine\DBAL\Schema\Table $table )
112
	{
113
		$indexes = array(
114
			'idx_ezpus_langid' => array( 'langid' ),
115
			'idx_ezpus_status_ln_fn' => array( 'status', 'lastname', 'firstname' ),
116
			'idx_ezpus_status_ad1_ad2' => array( 'status', 'address1', 'address2' ),
117
			'idx_ezpus_status_postal_city' => array( 'status', 'postal', 'city' ),
118
			'idx_ezpus_lastname' => array( 'lastname' ),
119
			'idx_ezpus_address1' => array( 'address1' ),
120
			'idx_ezpus_postal' => array( 'postal' ),
121
			'idx_ezpus_city' => array( 'city' ),
122
		);
123
124
		foreach( $indexes as $name => $def )
125
		{
126
			if( $table->hasIndex( $name ) === false ) {
127
				$table->addIndex( $def, $name );
128
			}
129
		}
130
131
		return $table;
132
	}
133
}
134