Completed
Pull Request — master (#139)
by Maxence
02:40
created

FixUniqueId::swapToShortenUniqueIdInShares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 11
nc 1
nop 2
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\Command;
28
29
use Exception;
30
use OC\Core\Command\Base;
31
use OC\Share\Share;
32
use OCA\Circles\Db\CirclesRequest;
33
use OCA\Circles\Db\CoreRequestBuilder;
34
use OCA\Circles\Model\Circle;
35
use OCP\IDBConnection;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
40
class FixUniqueId extends Base {
41
42
	/** @var IDBConnection */
43
	protected $connection;
44
45
	/** @var CirclesRequest */
46
	private $circlesRequest;
47
48
	public function __construct(CirclesRequest $circlesRequest, IDBConnection $connection) {
49
		parent::__construct();
50
		$this->circlesRequest = $circlesRequest;
51
		$this->connection = $connection;
52
	}
53
54
	protected function configure() {
55
		parent::configure();
56
		$this->setName('circles:fixuniqueid')
57
			 ->setDescription('fix Unique Id issue.');
58
	}
59
60
	protected function execute(InputInterface $input, OutputInterface $output) {
61
62
		try {
63
			$this->swapToShortenUniqueId();
64
65
			$output->writeln('done');
66
		} catch (Exception $e) {
67
			$output->writeln($e->getMessage());
68
		}
69
	}
70
71
72 View Code Duplication
	private function swapToShortenUniqueId() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
74
		$qb = $this->connection->getQueryBuilder();
75
76
		/** @noinspection PhpMethodParametersCountMismatchInspection */
77
		$qb->select('id', 'unique_id')
78
		   ->from(CoreRequestBuilder::TABLE_CIRCLES);
79
80
		$cursor = $qb->execute();
81
		while ($data = $cursor->fetch()) {
82
			$circleId = $data['id'];
83
84
			$shortenUniqueId = substr($data['unique_id'], 0, Circle::SHORT_UNIQUE_ID_LENGTH);
85
86
			$this->swapToShortenUniqueIdInTable(
87
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_GROUPS
88
			);
89
			$this->swapToShortenUniqueIdInTable(
90
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
91
			);
92
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
//			$this->cleanBuggyDuplicateEntries(
94
//				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS, 'user_id'
95
//			);
96
97
			$this->swapToShortenUniqueIdInTable(
98
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS
99
			);
100
101
			$this->swapToShortenUniqueIdInTable(
102
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
103
			);
104
105
			$this->swapToShortenUniqueIdInShares($circleId, $shortenUniqueId);
106
		}
107
		$cursor->closeCursor();
108
	}
109
110
111 View Code Duplication
	private function swapToShortenUniqueIdInTable($circleId, $shortenUniqueId, $table) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
112
113
		$qb = $this->connection->getQueryBuilder();
114
		$qb->update($table)
115
		   ->where(
116
			   $qb->expr()
117
				  ->eq('circle_id', $qb->createNamedParameter($circleId))
118
		   );
119
120
		$qb->set('circle_id', $qb->createNamedParameter($shortenUniqueId));
121
		$qb->execute();
122
	}
123
124
125
	private function swapToShortenUniqueIdInShares($circleId, $shortenUniqueId) {
126
		$qb = $this->connection->getQueryBuilder();
127
		$expr = $qb->expr();
128
129
		/** @noinspection PhpMethodParametersCountMismatchInspection */
130
		$qb->update('share')
131
		   ->where(
132
			   $expr->andX(
133
				   $expr->eq(
134
					   'share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)
135
				   ),
136
				   $expr->eq('share_with', $qb->createNamedParameter($circleId))
137
			   )
138
		   );
139
140
		$qb->set('share_with', $qb->createNamedParameter($shortenUniqueId));
141
		$qb->execute();
142
	}
143
144
145
}
146
147
148
149