Completed
Pull Request — master (#347)
by Maxence
01:49
created

Version0017Date20191206144441::postSchemaChange()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 8.8072
c 0
b 0
f 0
cc 3
nc 4
nop 3

How to fix   Long Method   

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
			$table->dropPrimaryKey();
128
			$table->setPrimaryKey(['circle_id', 'user_id', 'contact_id']);
129
		}
130
131
		$table = $schema->getTable('circles_tokens');
132 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...
133
			$table->addColumn(
134
				'member_id', Type::STRING, [
135
							   'notnull' => false,
136
							   'length'  => 15,
137
						   ]
138
			);
139
		}
140 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...
141
			$table->addColumn(
142
				'accepted', Type::SMALLINT, [
143
							  'notnull' => false,
144
							  'length'  => 1,
145
						  ]
146
			);
147
		}
148
149
		return $schema;
150
	}
151
152
	/**
153
	 * @param IOutput $output
154
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
155
	 * @param array $options
156
	 */
157
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
158
159
		$qb = $this->connection->getQueryBuilder();
160
		$expr = $qb->expr();
161
162
		$orX = $expr->orX();
163
		$orX->add($expr->eq('member_id', $qb->createNamedParameter('')));
164
		$orX->add($expr->isNull('member_id'));
165
166
		$qb->select('circle_id', 'user_id', 'user_type')
167
		   ->from('circles_members')
168
		   ->where($orX);
169
170
		$result = $qb->execute();
171
		while ($row = $result->fetch()) {
172
			$uniqueId = substr(bin2hex(openssl_random_pseudo_bytes(24)), 0, 15);
173
174
			$update = $this->connection->getQueryBuilder();
175
			$expru = $update->expr();
176
			$update->update('circles_members')
177
				   ->set('member_id', $update->createNamedParameter($uniqueId))
178
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
179
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])))
180
				   ->andWhere($expru->eq('user_type', $update->createNamedParameter($row['user_type'])));
181
182
			$update->execute();
183
		}
184
185
186
		$qb2 = $this->connection->getQueryBuilder();
187
		$expr2 = $qb2->expr();
188
		$orX = $expr2->orX();
189
		$orX->add($expr2->eq('member_id', $qb2->createNamedParameter('')));
190
		$orX->add($expr2->isNull('member_id'));
191
		$qb2->select('user_id', 'circle_id')
192
			->from('circles_tokens')
193
			->where($orX);
194
195
		$result = $qb2->execute();
196
		while ($row = $result->fetch()) {
197
			$qbm = $this->connection->getQueryBuilder();
198
			$exprm = $qbm->expr();
199
200
			$qbm->select('member_id')
201
				->from('circles_members')
202
				->where($exprm->eq('circle_id', $qbm->createNamedParameter($row['circle_id'])))
203
				->andWhere($exprm->eq('user_id', $qbm->createNamedParameter($row['user_id'])))
204
				->andWhere($exprm->neq('user_type', $qbm->createNamedParameter('1')));
205
206
			$resultm = $qbm->execute();
207
			$member = $resultm->fetch();
208
209
			$update = $this->connection->getQueryBuilder();
210
			$expru = $update->expr();
211
			$update->update('circles_tokens')
212
				   ->set('member_id', $update->createNamedParameter($member['member_id']))
213
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
214
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])));
215
216
			$update->execute();
217
		}
218
219
	}
220
}
221