Completed
Push — master ( d9af28...829b1d )
by Maxence
33:15 queued 16:32
created

UsingShortenUniqueIdInsteadOfCircleId   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 105
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 3 1
B run() 0 13 5
B swapToShortenUniqueId() 0 29 2
A swapToShortenUniqueIdInTable() 0 11 1
A swapToShortenUniqueIdInShares() 0 18 1
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 2017
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
namespace OCA\Circles\Migration;
28
29
use OCA\Circles\Db\CoreRequestBuilder;
30
use OCA\Circles\Model\Circle;
31
use OCP\IConfig;
32
use OCP\IDBConnection;
33
use OCP\Migration\IOutput;
34
use OCP\Migration\IRepairStep;
35
36
/**
37
 * Class UsingShortenUniqueIdInsteadOfCircleId
38
 *
39
 * @package OCA\Circles\Migration
40
 */
41
class UsingShortenUniqueIdInsteadOfCircleId implements IRepairStep {
42
43
	/** @var IDBConnection */
44
	protected $connection;
45
46
	/** @var  IConfig */
47
	protected $config;
48
49
	public function __construct(IDBConnection $connection, IConfig $config) {
50
		$this->connection = $connection;
51
		$this->config = $config;
52
	}
53
54
	/**
55
	 * Returns the step's name
56
	 *
57
	 * @return string
58
	 * @since 9.1.0
59
	 */
60
	public function getName() {
61
		return 'Using shorten unique id instead of circle id';
62
	}
63
64
	/**
65
	 * @param IOutput $output
66
	 */
67
	public function run(IOutput $output) {
68
		$oldVersion = explode(
69
			'.', \OC::$server->getConfig()
70
							 ->getAppValue('circles', 'installed_version', '')
71
		);
72
73
		if ((int)$oldVersion[0] === 0
74
			&& ((int)$oldVersion[1] < 12
75
				|| ((int)$oldVersion[1] === 12
76
					&& (int)$oldVersion[2] === 0))) {
77
			$this->swapToShortenUniqueId();
78
		}
79
	}
80
81
82
	private function swapToShortenUniqueId() {
83
84
		$qb = $this->connection->getQueryBuilder();
85
86
		/** @noinspection PhpMethodParametersCountMismatchInspection */
87
		$qb->select('id', 'unique_id')
88
		   ->from(CoreRequestBuilder::TABLE_CIRCLES);
89
90
		$cursor = $qb->execute();
91
		while ($data = $cursor->fetch()) {
92
			$circleId = $data['id'];
93
			$shortenUniqueId = substr($data['unique_id'], 0, Circle::UNIQUEID_SHORT_LENGTH);
94
95
			$this->swapToShortenUniqueIdInTable(
96
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_GROUPS
97
			);
98
			$this->swapToShortenUniqueIdInTable(
99
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
100
			);
101
			$this->swapToShortenUniqueIdInTable(
102
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS
103
			);
104
			$this->swapToShortenUniqueIdInTable(
105
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
106
			);
107
			$this->swapToShortenUniqueIdInShares($circleId, $shortenUniqueId);
108
		}
109
		$cursor->closeCursor();
110
	}
111
112
113
	private function swapToShortenUniqueIdInTable($circleId, $shortenUniqueId, $table) {
114
		$qb = $this->connection->getQueryBuilder();
115
		$qb->update($table)
116
		   ->where(
117
			   $qb->expr()
118
				  ->eq('circle_id', $qb->createNamedParameter($circleId))
119
		   );
120
121
		$qb->set('circle_id', $qb->createNamedParameter($shortenUniqueId));
122
		$qb->execute();
123
	}
124
125
126
	private function swapToShortenUniqueIdInShares($circleId, $shortenUniqueId) {
127
		$qb = $this->connection->getQueryBuilder();
128
		$expr = $qb->expr();
129
130
		/** @noinspection PhpMethodParametersCountMismatchInspection */
131
		$qb->update('share')
132
		   ->where(
133
			   $expr->andX(
134
				   $expr->eq(
135
					   'share_type', $qb->createNamedParameter(\OC\Share\Share::SHARE_TYPE_CIRCLE)
136
				   ),
137
				   $expr->eq('share_with', $qb->createNamedParameter($circleId))
138
			   )
139
		   );
140
141
		$qb->set('share_with', $qb->createNamedParameter($shortenUniqueId));
142
		$qb->execute();
143
	}
144
145
}
146
147
148