Completed
Pull Request — master (#694)
by René
04:05
created

Version0010Date20191227063812::changeSchema()   C

Complexity

Conditions 9
Paths 132

Size

Total Lines 105
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 72
c 0
b 0
f 0
nc 132
nop 3
dl 0
loc 105
ccs 0
cts 100
cp 0
crap 90
rs 6.842

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
 * @copyright Copyright (c) 2017 René Gieling <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Migration;
25
26
// use Doctrine\DBAL\Exception\TableNotFoundException;
27
// use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
28
use Doctrine\DBAL\Types\Type;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Types\Type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use OCP\DB\ISchemaWrapper;
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\IConfig;
32
use OCP\IDBConnection;
33
use OCP\Migration\SimpleMigrationStep;
34
use OCP\Migration\IOutput;
35
use OCP\Security\ISecureRandom;
36
37
/**
38
 * Installation class for the polls app.
39
 * Initial db creation
40
 */
41
class Version0010Date20191227063812 extends SimpleMigrationStep {
42
43
	/** @var IDBConnection */
44
	protected $connection;
45
46
	/** @var IConfig */
47
	protected $config;
48
49
	/**
50
	 * @param IDBConnection $connection
51
	 * @param IConfig $config
52
	 */
53
	public function __construct(IDBConnection $connection, IConfig $config) {
54
		$this->connection = $connection;
55
		$this->config = $config;
56
	}
57
58
	/**
59
	 * @param IOutput $output
60
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
61
	 * @param array $options
62
	 * @return null|ISchemaWrapper
63
	 * @since 13.0.0
64
	 */
65
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
66
		/** @var ISchemaWrapper $schema */
67
		$schema = $schemaClosure();
68
69
		if ($schema->hasTable('polls_events')) {
70
			$table = $schema->getTable('polls_events');
71
			if (!$table->hasColumn('expiration')) {
72
				$table->addColumn('expiration', Type::BOOLEAN, [
73
					'notnull' => true,
74
					'default' => 0
75
				]);
76
			}
77
			if (!$table->hasColumn('deleted')) {
78
				$table->addColumn('deleted', Type::BOOLEAN, [
79
					'notnull' => false,
80
					'default' => 0
81
				]);
82
			}
83
			if (!$table->hasColumn('delete_date')) {
84
				$table->addColumn('delete_date', Type::DATETIME, [
85
					'notnull' => false
86
				]);
87
			}
88
			if (!$table->hasColumn('vote_limit')) {
89
				$table->addColumn('vote_limit', Type::INTEGER, [
90
					'notnull' => false,
91
					'default' => 0
92
				]);
93
			}
94
			if (!$table->hasColumn('show_results')) {
95
				$table->addColumn('show_results', Type::STRING, [
96
					'notnull' => true,
97
					'length' => 64,
98
					'default' => 'always'
99
				]);
100
			}
101
102
		}
103
		if (!$schema->hasTable('polls_share')) {
104
			$table = $schema->createTable('polls_share');
105
			$table->addColumn('id', Type::INTEGER, [
106
				'autoincrement' => true,
107
				'notnull' => true,
108
			]);
109
			$table->addColumn('token', Type::STRING, [
110
				'notnull' => true,
111
				'length' => 64,
112
			]);
113
			$table->addColumn('type', Type::STRING, [
114
				'notnull' => true,
115
				'length' => 128,
116
			]);
117
			$table->addColumn('poll_id', Type::INTEGER, [
118
				'notnull' => true
119
			]);
120
			$table->addColumn('user_id', Type::STRING, [
121
				'notnull' => false,
122
				'length' => 64,
123
			]);
124
			$table->addColumn('user_email', Type::STRING, [
125
				'notnull' => false,
126
				'length' => 254,
127
			]);
128
			$table->setPrimaryKey(['id']);
129
		}
130
131
		if (!$schema->hasTable('polls_log')) {
132
			$table = $schema->createTable('polls_log');
133
			$table->addColumn('id', Type::INTEGER, [
134
				'autoincrement' => true,
135
				'notnull' => true
136
			]);
137
			$table->addColumn('created', Type::INTEGER, [
138
				'notnull' => true,
139
				'length' => 11,
140
				'default' => 0
141
			]);
142
			$table->addColumn('processed', Type::INTEGER, [
143
				'notnull' => true,
144
				'length' => 11,
145
				'default' => 0
146
			]);
147
			$table->addColumn('poll_id', Type::INTEGER, [
148
				'notnull' => true
149
			]);
150
			$table->addColumn('user_id', Type::STRING, [
151
				'notnull' => false,
152
				'length' => 1024
153
			]);
154
			$table->addColumn('display_name', Type::STRING, [
155
				'notnull' => false,
156
				'length' => 64
157
			]);
158
			$table->addColumn('message_id', Type::STRING, [
159
				'notnull' => false,
160
				'length' => 64
161
			]);
162
			$table->addColumn('message', Type::STRING, [
163
				'notnull' => false,
164
				'length' => 1024
165
			]);
166
			$table->setPrimaryKey(['id']);
167
		}
168
169
		return $schema;
170
	}
171
172
	/**
173
	 * @param IOutput $output
174
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
175
	 * @param array $options
176
	 * @since 13.0.0
177
	 */
178
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
179
		/** @var ISchemaWrapper $schema */
180
		$schema = $schemaClosure();
181
182
		if ($schema->hasTable('polls_events')) {
183
			$this->setExpiration();
184
		}
185
186
		if ($schema->hasTable('polls_share')) {
187
			$this->copyTokens();
188
			// $this->copyInvitationTokens();
189
		}
190
	}
191
192
	/**
193
	 * Set expiration if expire is filled
194
	 */
195
	protected function setExpiration() {
196
197
		$update = $this->connection->getQueryBuilder();
198
		$update->update('polls_events')
199
			->set('expiration', $update->createNamedParameter(true))
200
			->where('expire IS NOT NULL');
201
		$result = $update->execute();
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
202
	}
203
204
	/**
205
	 * Copy public tokens
206
	 */
207
	protected function copyTokens() {
208
		$insert = $this->connection->getQueryBuilder();
209
		$insert->insert('polls_share')
210
			->values([
211
				'token' => $insert->createParameter('token'),
212
				'type' => $insert->createParameter('type'),
213
				'poll_id' => $insert->createParameter('poll_id'),
214
				'user_id' => $insert->createParameter('user_id'),
215
				'user_email' => $insert->createParameter('user_email')
216
			]);
217
		$query = $this->connection->getQueryBuilder();
218
		$query->select('*')
219
			->from('polls_events');
220
		$result = $query->execute();
221
222
		while ($row = $result->fetch()) {
223
			if ($row['access'] == 'public') {
224
				// copy the hash to a public share
225
				$insert
226
				->setParameter('token', $row['hash'])
227
				->setParameter('type', 'public')
228
				->setParameter('poll_id', $row['id'])
229
				->setParameter('user_id', null)
230
				->setParameter('user_email', null);
231
				$insert->execute();
232
			} elseif ($row['access'] == 'hidden') {
233
				// copy the hash to a public share
234
				// poll stays hidden for registered users
235
				$insert
236
				->setParameter('token', $row['hash'])
237
				->setParameter('type', 'public')
238
				->setParameter('poll_id', $row['id'])
239
				->setParameter('user_id', null)
240
				->setParameter('user_email', null);
241
				$insert->execute();
242
			} elseif ($row['access'] == 'registered') {
243
				// copy the hash to a public share
244
				// to keep the hash
245
				$insert
246
				->setParameter('token', $row['hash'])
247
				->setParameter('type', 'public')
248
				->setParameter('poll_id', $row['id'])
249
				->setParameter('user_id', null)
250
				->setParameter('user_email', null);
251
			} else {
252
				// create a personal share for invitated users
253
254
				// explode the access entry to single access strings
255
				$users = explode(';', $row['access']);
256
				foreach ($users as $value) {
257
					// separate 'user' and 'group' from user names and create
258
					// a share for every entry
259
					$parts = explode('_', $value);
260
					$insert
261
					->setParameter('token', \OC::$server->getSecureRandom()->generate(
262
						16,
263
						ISecureRandom::CHAR_DIGITS .
264
						ISecureRandom::CHAR_LOWER .
265
						ISecureRandom::CHAR_UPPER
266
					))
267
					->setParameter('type', $parts[0])
268
					->setParameter('poll_id', $row['id'])
269
					->setParameter('user_id', $parts[1])
270
					->setParameter('user_email', null);
271
					$insert->execute();
272
				}
273
			}
274
		}
275
		$result->closeCursor();
276
	}
277
278
}
279