Completed
Push — master ( 1b010a...e0522b )
by Aimeos
01:49
created

EzuserAddAddress::getPreDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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