Passed
Push — master ( 5063d9...a1994b )
by Jeroen
22:08
created

DropUsersEntityTable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B up() 0 65 7
1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
5
class DropUsersEntityTable extends AbstractMigration {
6
    /**
7
     * Move users_entity attributes to metadata
8
     */
9
    public function up() {
10
11
		if (!$this->hasTable('users_entity') || !$this->hasTable('metadata')) {
12
			return;
13
		}
14
15
		$prefix = $this->getAdapter()->getOption('table_prefix');
16
		$cols = [
17
			'name' => 'text',
18
			'username' => 'text',
19
			'password_hash' => 'text',
20
			'email' => 'text',
21
			'language' => 'text',
22
			'banned' => 'text',
23
			'admin' => 'text',
24
			'last_action' => 'integer',
25
			'prev_last_action' => 'integer',
26
			'last_login' => 'integer',
27
			'prev_last_login' => 'integer',
28
		];
29
				
30
		$users_query = "SELECT * FROM {$prefix}users_entity LIMIT 25";
31
		while ($rows = $this->fetchAll($users_query)) {
32
			foreach ($rows as $row) {
33
				foreach ($cols as $col => $type) {
34
					
35
					if ($col == 'last_action') {
36
						// special column last_action goes to last_action in entities table
37
						$this->execute("
38
							UPDATE {$prefix}entities SET last_action = {$row['last_action']}
39
							WHERE guid = {$row['guid']}
40
						");
41
						continue;
42
					}
43
					
44
					// remove existing metadata... attributes are more important
45
					$this->execute("
46
						DELETE FROM {$prefix}metadata
47
						WHERE entity_guid = {$row['guid']} AND
48
						name = '{$col}'
49
					");
50
					
51
					$this->insert('metadata', [
52
						'entity_guid' => $row['guid'],
53
						'name' => $col,
54
						'value' => $row[$col],
55
						'value_type' => $type,
56
						'owner_guid' => 0,
57
						'access_id' => 2,
58
						'time_created' => time(),
59
						'enabled' => 'yes',
60
					]);
61
				}
62
				
63
				// remove from users so it does not get processed again in the next while loop
64
				$this->execute("
65
					DELETE FROM {$prefix}users_entity
66
					WHERE guid = {$row['guid']}
67
				");
68
			}
69
		}
70
		
71
		// all data migrated, so drop the table
72
		$this->dropTable('users_entity');
73
    }
74
}
75