Completed
Push — master ( 69ab4d...e9ff9f )
by Maxence
02:43 queued 30s
created

Version0017Date20191206144441::changeSchema()   D

Complexity

Conditions 10
Paths 384

Size

Total Lines 81

Duplication

Lines 48
Ratio 59.26 %

Importance

Changes 0
Metric Value
dl 48
loc 81
rs 4.0412
c 0
b 0
f 0
cc 10
nc 384
nop 3

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
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2019
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
declare(strict_types=1);
28
29
namespace OCA\Circles\Migration;
30
31
use Closure;
32
use Doctrine\DBAL\Schema\SchemaException;
33
use Doctrine\DBAL\Types\Type;
34
use OCP\DB\ISchemaWrapper;
35
use OCP\IDBConnection;
36
use OCP\Migration\IOutput;
37
use OCP\Migration\SimpleMigrationStep;
38
39
/**
40
 * Auto-generated migration step: Please modify to your needs!
41
 */
42
class Version0017Date20191206144441 extends SimpleMigrationStep {
43
44
45
	/** @var IDBConnection */
46
	private $connection;
47
48
49
	/**
50
	 * @param IDBConnection $connection
51
	 */
52
	public function __construct(IDBConnection $connection) {
53
		$this->connection = $connection;
54
	}
55
56
	/**
57
	 * @param IOutput $output
58
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
59
	 * @param array $options
60
	 */
61
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
62
	}
63
64
	/**
65
	 * @param IOutput $output
66
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
67
	 * @param array $options
68
	 *
69
	 * @return null|ISchemaWrapper
70
	 * @throws SchemaException
71
	 */
72
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
73
		/** @var ISchemaWrapper $schema */
74
		$schema = $schemaClosure();
75
76
		$table = $schema->getTable('circles_circles');
77
		if (!$table->hasColumn('contact_addressbook')) {
78
			$table->addColumn(
79
				'contact_addressbook', 'integer', [
80
										 'notnull'  => false,
81
										 'unsigned' => true,
82
										 'length'   => 7,
83
									 ]
84
			);
85
		}
86 View Code Duplication
		if (!$table->hasColumn('contact_groupname')) {
0 ignored issues
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...
87
			$table->addColumn(
88
				'contact_groupname', 'string', [
89
									   'notnull' => false,
90
									   'length'  => 127,
91
								   ]
92
			);
93
		}
94
95
		$table = $schema->getTable('circles_members');
96 View Code Duplication
		if (!$table->hasColumn('member_id')) {
0 ignored issues
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...
97
			$table->addColumn(
98
				'member_id', Type::STRING, [
99
							   'notnull' => false,
100
							   'length'  => 15,
101
						   ]
102
			);
103
		}
104 View Code Duplication
		if (!$table->hasColumn('contact_meta')) {
0 ignored issues
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...
105
			$table->addColumn(
106
				'contact_meta', 'string', [
107
								  'notnull' => false,
108
								  'length'  => 1000,
109
							  ]
110
			);
111
		}
112 View Code Duplication
		if (!$table->hasColumn('contact_checked')) {
0 ignored issues
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...
113
			$table->addColumn(
114
				'contact_checked', Type::SMALLINT, [
115
									 'notnull' => false,
116
									 'length'  => 1,
117
								 ]
118
			);
119
		}
120
		if (!$table->hasColumn('contact_id')) {
121
			$table->addColumn(
122
				'contact_id', 'string', [
123
								'notnull' => false,
124
								'length'  => 127,
125
							]
126
			);
127
			if ($table->hasPrimaryKey()) {
128
				$table->dropPrimaryKey();
129
			}
130
			$table->setPrimaryKey(['circle_id', 'user_id', 'contact_id']);
131
		}
132
133
		$table = $schema->getTable('circles_tokens');
134 View Code Duplication
		if (!$table->hasColumn('member_id')) {
0 ignored issues
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...
135
			$table->addColumn(
136
				'member_id', Type::STRING, [
137
							   'notnull' => false,
138
							   'length'  => 15,
139
						   ]
140
			);
141
		}
142 View Code Duplication
		if (!$table->hasColumn('accepted')) {
0 ignored issues
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...
143
			$table->addColumn(
144
				'accepted', Type::SMALLINT, [
145
							  'notnull' => false,
146
							  'length'  => 1,
147
						  ]
148
			);
149
		}
150
151
		return $schema;
152
	}
153
154
	/**
155
	 * @param IOutput $output
156
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
157
	 * @param array $options
158
	 */
159
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
160
161
		$qb = $this->connection->getQueryBuilder();
162
		$expr = $qb->expr();
163
164
		$orX = $expr->orX();
165
		$orX->add($expr->eq('member_id', $qb->createNamedParameter('')));
166
		$orX->add($expr->isNull('member_id'));
167
168
		$qb->select('circle_id', 'user_id', 'user_type')
169
		   ->from('circles_members')
170
		   ->where($orX);
171
172
		$result = $qb->execute();
173
		while ($row = $result->fetch()) {
174
			$uniqueId = substr(bin2hex(openssl_random_pseudo_bytes(24)), 0, 15);
175
176
			$update = $this->connection->getQueryBuilder();
177
			$expru = $update->expr();
178
			$update->update('circles_members')
179
				   ->set('member_id', $update->createNamedParameter($uniqueId))
180
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
181
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])))
182
				   ->andWhere($expru->eq('user_type', $update->createNamedParameter($row['user_type'])));
183
184
			$update->execute();
185
		}
186
187
188
		$qb2 = $this->connection->getQueryBuilder();
189
		$expr2 = $qb2->expr();
190
		$orX = $expr2->orX();
191
		$orX->add($expr2->eq('member_id', $qb2->createNamedParameter('')));
192
		$orX->add($expr2->isNull('member_id'));
193
		$qb2->select('user_id', 'circle_id')
194
			->from('circles_tokens')
195
			->where($orX);
196
197
		$result = $qb2->execute();
198
		while ($row = $result->fetch()) {
199
			$qbm = $this->connection->getQueryBuilder();
200
			$exprm = $qbm->expr();
201
202
			$qbm->select('member_id')
203
				->from('circles_members')
204
				->where($exprm->eq('circle_id', $qbm->createNamedParameter($row['circle_id'])))
205
				->andWhere($exprm->eq('user_id', $qbm->createNamedParameter($row['user_id'])))
206
				->andWhere($exprm->neq('user_type', $qbm->createNamedParameter('1')));
207
208
			$resultm = $qbm->execute();
209
			$member = $resultm->fetch();
210
211
			$update = $this->connection->getQueryBuilder();
212
			$expru = $update->expr();
213
			$update->update('circles_tokens')
214
				   ->set('member_id', $update->createNamedParameter($member['member_id']))
215
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
216
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])));
217
218
			$update->execute();
219
		}
220
221
	}
222
}
223