Test Failed
Push — master ( fe71c4...38b5f5 )
by Jeroen
02:19
created

RemoveSiteGuid::down()   F

Complexity

Conditions 18
Paths 6075

Size

Total Lines 182
Code Lines 94

Duplication

Lines 40
Ratio 21.98 %

Importance

Changes 0
Metric Value
cc 18
eloc 94
nc 6075
nop 0
dl 40
loc 182
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
use Phinx\Migration\AbstractMigration;
4
5
/**
6
 * Removes multisite support from 2.x schema
7
 */
8
class RemoveSiteGuid extends AbstractMigration {
9
10
	/**
11
	 * Ensure that legacy schema only has 1 site entity
12
	 * Refuse to upgrade if it doesn't
13
	 *
14
	 * @throws InstallationException
15
	 */
16
	public function validate() {
17
18
		// validate if multiple sites are in the database
19
		$tables = [
20
			'access_collections',
21
			'api_users',
22
			'config',
23
			'entities',
24
			'users_apisessions'
25
		];
26
27
		foreach ($tables as $table) {
28
			if (!$this->hasTable($table)) {
29
				continue;
30
			}
31
32
			$prefix = $this->getAdapter()->getOption('table_prefix');
33
			$row = $this->fetchRow("
34
				SELECT count(DISTINCT site_guid) as count 
35
				FROM {$prefix}{$table}
36
			");
37
38
			if ($row && $row['count'] > 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
				throw new InstallationException("Multiple sites detected in table: '{$prefix}{$table}'. Can't upgrade the database.");
40
			}
41
		}
42
	}
43
44
	/**
45
	 * Removes site guid from legacy 2.x tables
46
	 */
47
	public function up() {
48
49
		$this->validate();
50
51 View Code Duplication
		if ($this->hasTable('access_collections')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
52
53
			$table = $this->table('access_collections');
54
55
			if ($table->hasIndex('site_guid')) {
56
				$table->removeIndexByName('site_guid');
57
			}
58
59
			if ($table->hasColumn('site_guid')) {
60
				$table->removeColumn('site_guid');
61
			}
62
63
			$table->save();
64
		}
65
66
		if ($this->hasTable('api_users')) {
67
68
			$table = $this->table('api_users');
69
70
			if ($table->hasColumn('site_guid')) {
71
				$table->removeColumn('site_guid');
72
			}
73
74
			$table->save();
75
		}
76
77
		if ($this->hasTable('config')) {
78
79
			$table = $this->table('config', [
80
				'primary_key' => ["name"],
81
			]);
82
83
			if ($table->hasIndex('site_guid')) {
84
				$table->removeIndexByName('site_guid');
85
			}
86
87
			if ($table->hasColumn('site_guid')) {
88
				$table->removeColumn('site_guid');
89
			}
90
91
			$table->save();
92
		}
93
94 View Code Duplication
		if ($this->hasTable('entities')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
95
96
			$table = $this->table('entities');
97
98
			if ($table->hasIndex('site_guid')) {
99
				$table->removeIndexByName('site_guid');
100
			}
101
102
			if ($table->hasColumn('site_guid')) {
103
				$table->removeColumn('site_guid');
104
			}
105
106
			$table->save();
107
		}
108
109
		if ($this->hasTable('users_apisessions')) {
110
111
			$table = $this->table('users_apisessions');
112
113
			if ($table->hasIndex('site_guid')) {
114
				$table->removeIndexByName('site_guid');
115
			}
116
117
			if ($table->hasColumn('site_guid')) {
118
				$table->removeColumn('site_guid');
119
			}
120
121
			if ($table->hasIndex('user_guid')) {
122
				$table->removeIndex('user_guid');
0 ignored issues
show
Documentation introduced by
'user_guid' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
			}
124
125
			$table->addIndex(['user_guid'], [
126
				'name' => "user_guid",
127
				'unique' => false,
128
			]);
129
130
			$table->save();
131
		}
132
133
		if ($this->hasTable('entity_relationships')) {
134
			// Remove member_of_site relaitonship following site_guid removal
135
			$prefix = $this->getAdapter()->getOption('table_prefix');
136
			$this->query("
137
				DELETE FROM {$prefix}entity_relationships
138
				WHERE relationship = 'member_of_site'
139
			");
140
		}
141
142
	}
143
144
	/**
145
	 * Add site_guid column and index
146
	 */
147
	public function down() {
148
149
		if ($this->hasTable('access_collections')) {
150
151
			$table = $this->table('access_collections');
152
153 View Code Duplication
			if (!$table->hasColumn('site_guid')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
154
				$table->addColumn('site_guid', 'integer', [
155
					'null' => false,
156
					'limit' => MysqlAdapter::INT_BIG,
157
					'precision' => 20,
158
					'signed' => false,
159
				]);
160
			}
161
162
			if (!$table->hasIndex('site_guid')) {
163
				$table->addIndex(['site_guid'], [
164
					'name' => 'site_guid',
165
					'unique' => false,
166
				]);
167
			}
168
169
			$table->save();
170
171
			$prefix = $this->getAdapter()->getOption('table_prefix');
172
			$this->query("
173
				UPDATE {$prefix}access_collections
174
				SET site_guid = 1
175
				WHERE site_guid != 1
176
			");
177
178
		}
179
180
		if ($this->hasTable('api_users')) {
181
182
			$table = $this->table('api_users');
183
184 View Code Duplication
			if (!$table->hasColumn('site_guid')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
185
				$table->addColumn('site_guid', 'integer', [
186
					'null' => false,
187
					'limit' => MysqlAdapter::INT_BIG,
188
					'precision' => 20,
189
					'signed' => false,
190
				]);
191
			}
192
193
			$table->save();
194
195
			$prefix = $this->getAdapter()->getOption('table_prefix');
196
			$this->query("
197
				UPDATE {$prefix}api_users
198
				SET site_guid = 1
199
				WHERE site_guid != 1
200
			");
201
202
		}
203
204
		if ($this->hasTable('config')) {
205
206
			$table = $this->table('config', [
207
				'primary_key' => [
208
					"name",
209
					"site_guid"
210
				],
211
			]);
212
213 View Code Duplication
			if (!$table->hasColumn('site_guid')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
214
				$table->addColumn('site_guid', 'integer', [
215
					'null' => false,
216
					'limit' => MysqlAdapter::INT_BIG,
217
					'precision' => 20,
218
					'signed' => false,
219
				]);
220
			}
221
222
			if (!$table->hasIndex('site_guid')) {
223
				$table->addIndex(['site_guid'], [
224
					'name' => 'site_guid',
225
					'unique' => false,
226
				]);
227
			}
228
229
			$table->save();
230
231
			$prefix = $this->getAdapter()->getOption('table_prefix');
232
			$this->query("
233
				UPDATE {$prefix}config
234
				SET site_guid = 1
235
				WHERE site_guid != 1
236
			");
237
238
		}
239
240
		if ($this->hasTable('entities')) {
241
242
			// remove site guid from entities
243
			$table = $this->table('entities');
244
245 View Code Duplication
			if (!$table->hasColumn('site_guid')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
246
				$table->addColumn('site_guid', 'integer', [
247
					'null' => false,
248
					'limit' => MysqlAdapter::INT_BIG,
249
					'precision' => 20,
250
					'signed' => false,
251
				]);
252
			}
253
254
			if (!$table->hasIndex('site_guid')) {
255
				$table->addIndex(['site_guid'], [
256
					'name' => 'site_guid',
257
					'unique' => false,
258
				]);
259
			}
260
261
			$table->save();
262
263
			$prefix = $this->getAdapter()->getOption('table_prefix');
264
			$this->query("
265
				UPDATE {$prefix}entities
266
				SET site_guid = 1
267
				WHERE site_guid != 1
268
			");
269
270
			if ($this->hasTable('entity_relationships')) {
271
				$rows = $this->fetchAll("
272
					SELECT guid FROM {$prefix}entities
273
					WHERE type = 'user'
274
				");
275
276
				foreach ($rows as $row) {
277
					$this->insert('entity_relationships', [
278
						'guid_one' => $row['guid'],
279
						'relationship' => 'member_of_site',
280
						'guid_two' => 1,
281
						'time_created' => time(),
282
					]);
283
				}
284
			}
285
		}
286
287
		if ($this->hasTable('users_apisessions')) {
288
289
			// remove site guid from users_apisessions
290
			$table = $this->table('users_apisessions');
291
292
			if ($table->hasIndex('site_guid')) {
293
				$table->removeIndexByName('site_guid');
294
			}
295
296 View Code Duplication
			if (!$table->hasColumn('site_guid')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
297
				$table->addColumn('site_guid', 'integer', [
298
					'null' => false,
299
					'limit' => MysqlAdapter::INT_BIG,
300
					'precision' => 20,
301
					'signed' => false,
302
				]);
303
			}
304
305
			if ($table->hasIndex('user_guid')) {
306
				$table->removeIndexByName('user_guid');
307
			}
308
309
			$table->addIndex([
310
				'user_guid',
311
				'site_guid'
312
			], [
313
				'name' => "user_guid",
314
				'unique' => true,
315
			]);
316
317
			$table->save();
318
319
			$prefix = $this->getAdapter()->getOption('table_prefix');
320
			$this->query("
321
				UPDATE {$prefix}users_apisessions
322
				SET site_guid = 1
323
				WHERE site_guid != 1
324
			");
325
326
		}
327
328
	}
329
}
330