Completed
Push — master ( fab0a4...8e249d )
by Jeroen
41:43 queued 16:43
created

DropObjectsEntityTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B up() 44 44 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
5 View Code Duplication
class DropObjectsEntityTable extends AbstractMigration {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
    /**
7
     * Move objects_entity attributes to metadata
8
     */
9
    public function up() {
10
11
		if (!$this->hasTable('objects_entity') || !$this->hasTable('metadata')) {
12
			return;
13
		}
14
15
		$prefix = $this->getAdapter()->getOption('table_prefix');
16
		$cols = ['title', 'description'];
17
		
18
		$objects_query = "SELECT * FROM {$prefix}objects_entity LIMIT 25";
19
		while ($rows = $this->fetchAll($objects_query)) {
20
			foreach ($rows as $row) {
21
				foreach ($cols as $col) {
22
					
23
					// remove existing metadata... attributes are more important
24
					$this->execute("
25
						DELETE FROM {$prefix}metadata
26
						WHERE entity_guid = {$row['guid']} AND
27
						name = '{$col}'
28
					");
29
					
30
					$this->insert('metadata', [
31
						'entity_guid' => $row['guid'],
32
						'name' => $col,
33
						'value' => $row[$col],
34
						'value_type' => 'text',
35
						'owner_guid' => 0,
36
						'access_id' => 2,
37
						'time_created' => time(),
38
						'enabled' => 'yes',
39
					]);
40
				}
41
				
42
				// remove from objects so it does not get processed again in the next while loop
43
				$this->execute("
44
					DELETE FROM {$prefix}objects_entity
45
					WHERE guid = {$row['guid']}
46
				");
47
			}
48
		}
49
		
50
		// all data migrated, so drop the table
51
		$this->dropTable('objects_entity');
52
    }
53
}
54