Completed
Pull Request — master (#551)
by Maxence
02:38
created

Version0021Date20210105123456::changeSchema()   F

Complexity

Conditions 9
Paths 600

Size

Total Lines 164

Duplication

Lines 31
Ratio 18.9 %

Importance

Changes 0
Metric Value
dl 31
loc 164
rs 3.5555
c 0
b 0
f 0
cc 9
nc 600
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
declare(strict_types=1);
4
5
/**
6
 * Circles - Bring cloud-users closer together.
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2021
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OCA\Circles\Migration;
31
32
use Closure;
33
use Doctrine\DBAL\Schema\SchemaException;
34
use OCP\DB\ISchemaWrapper;
35
use OCP\IDBConnection;
36
use OCP\Migration\IOutput;
37
use OCP\Migration\SimpleMigrationStep;
38
39
40
/**
41
 * Class Version0021Date20210105123456
42
 *
43
 * @package OCA\Circles\Migration
44
 */
45
class Version0021Date20210105123456 extends SimpleMigrationStep {
46
47
48
	/** @var IDBConnection */
49
	private $connection;
50
51
52
	/**
53
	 * @param IDBConnection $connection
54
	 */
55
	public function __construct(IDBConnection $connection) {
56
		$this->connection = $connection;
57
	}
58
59
60
	/**
61
	 * @param IOutput $output
62
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
63
	 * @param array $options
64
	 *
65
	 * @return null|ISchemaWrapper
66
	 */
67
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
68
		/** @var ISchemaWrapper $schema */
69
		$schema = $schemaClosure();
70
71
		try {
72
			$circles = $schema->getTable('circle_circles');
73
			if (!$circles->hasColumn('config')) {
74
				$circles->addColumn(
75
					'config', 'integer', [
76
								'notnull'  => false,
77
								'length'   => 11,
78
								'unsigned' => true,
79
							]
80
				);
81
			}
82
			if (!$circles->hasColumn('instance')) {
83
				$circles->addColumn(
84
					'instance', 'string', [
85
								  'notnull' => true,
86
								  'default' => '',
87
								  'length'  => 255
88
							  ]
89
				);
90
			}
91
			$circles->addIndex(['config']);
92
		} catch (SchemaException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Doctrine\DBAL\Schema\SchemaException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
93
		}
94
95
96
		try {
97
			$circles = $schema->getTable('circle_members');
98
			if (!$circles->hasColumn('single_id')) {
99
				$circles->addColumn(
100
					'single_id', 'string', [
101
								   'notnull' => false,
102
								   'length'  => 15
103
							   ]
104
				);
105
			}
106
		} catch (SchemaException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Doctrine\DBAL\Schema\SchemaException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
107
		}
108
109
110
		if (!$schema->hasTable('circle_remotes')) {
111
			$table = $schema->createTable('circle_remotes');
112
			$table->addColumn(
113
				'id', 'integer', [
114
						'autoincrement' => true,
115
						'notnull'       => true,
116
						'length'        => 4,
117
						'unsigned'      => true,
118
					]
119
			);
120
			$table->addColumn(
121
				'type', 'string', [
122
						  'notnull' => true,
123
						  'length'  => 15,
124
						  'default' => 'Unknown'
125
					  ]
126
			);
127
			$table->addColumn(
128
				'uid', 'string', [
129
						 'notnull' => false,
130
						 'length'  => 20,
131
					 ]
132
			);
133
			$table->addColumn(
134
				'instance', 'string', [
135
							  'notnull' => false,
136
							  'length'  => 127,
137
						  ]
138
			);
139
			$table->addColumn(
140
				'href', 'string', [
141
						  'notnull' => false,
142
						  'length'  => 254,
143
					  ]
144
			);
145
			$table->addColumn(
146
				'item', 'text', [
147
						  'notnull' => false,
148
					  ]
149
			);
150
			$table->addColumn(
151
				'creation', 'datetime', [
152
							  'notnull' => false,
153
						  ]
154
			);
155
156
			$table->setPrimaryKey(['id']);
157
			$table->addUniqueIndex(['instance']);
158
			$table->addIndex(['uid']);
159
			$table->addIndex(['href']);
160
		}
161
162 View Code Duplication
		if (!$schema->hasTable('circle_membership')) {
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...
163
			$table = $schema->createTable('circle_membership');
164
165
			$table->addColumn(
166
				'id', 'string', [
167
						'notnull' => true,
168
						'length'  => 15,
169
					]
170
			);
171
			$table->addColumn(
172
				'circle_id', 'string', [
173
							   'notnull' => true,
174
							   'length'  => 15,
175
						   ]
176
			);
177
			$table->addColumn(
178
				'member_id', 'string', [
179
							   'notnull' => true,
180
							   'length'  => 15,
181
						   ]
182
			);
183
			$table->addColumn(
184
				'level', 'integer', [
185
						   'notnull'  => true,
186
						   'length'   => 1,
187
						   'unsigned' => true
188
					   ]
189
			);
190
191
			$table->addIndex(['id']);
192
		}
193
194
195
		if (!$schema->hasTable('circle_shares')) {
196
			$table = $schema->createTable('circle_shares');
197
			$table->addColumn(
198
				'id', 'integer', [
199
						'autoincrement' => true,
200
						'notnull'       => true,
201
						'length'        => 4,
202
						'unsigned'      => true,
203
					]
204
			);
205
			$table->addColumn(
206
				'unique_id', 'string', [
207
							   'notnull' => true,
208
							   'length'  => 15
209
						   ]
210
			);
211
			$table->addColumn(
212
				'circle_id', 'string', [
213
							   'notnull' => true,
214
							   'length'  => 15,
215
						   ]
216
			);
217
			$table->addColumn(
218
				'instance', 'string', [
219
							  'notnull' => true,
220
							  'length'  => 127,
221
						  ]
222
			);
223
224
			$table->setPrimaryKey(['id']);
225
			$table->addUniqueIndex(['unique_id']);
226
			$table->addIndex(['circle_id', 'instance'], 'ci');
227
		}
228
229
		return $schema;
230
	}
231
232
233
}
234