Completed
Push — federated-circles ( 8c69b3...33013b )
by Maxence
07:05 queued 02:35
created

GenerateUniqueIdOnCreatedCircle::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[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\Circles\Migration;
25
26
use OCA\Circles\Model\Circle;
27
use OCA\Circles\Model\Member;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\Migration\IOutput;
31
use OCP\Migration\IRepairStep;
32
use OCP\Share;
33
34
/**
35
 * Class UpdateShareTimeToTimestamp
36
 *
37
 * @package OCA\Circles\Migration
38
 */
39
class GenerateUniqueIdOnCreatedCircle implements IRepairStep {
40
41
	/** @var IDBConnection */
42
	protected $connection;
43
44
	/** @var  IConfig */
45
	protected $config;
46
47
	public function __construct(IDBConnection $connection, IConfig $config) {
48
		$this->connection = $connection;
49
		$this->config = $config;
50
	}
51
52
	/**
53
	 * Returns the step's name
54
	 *
55
	 * @return string
56
	 * @since 9.1.0
57
	 */
58
	public function getName() {
59
		return 'Generate unique id on created circle';
60
	}
61
62
	/**
63
	 * @param IOutput $output
64
	 */
65
	public function run(IOutput $output) {
66
		$this->generateUniqueId($output);
67
	}
68
69
	/**
70
	 * Update shares
71
	 * - type 7 instead of 1
72
	 * - with circle ID instead of `customgroup_` + group URI
73
	 *
74
	 * @param IOutput $output
75
	 */
76
	public function generateUniqueId(IOutput $output) {
77
		$output->info('Generate unique id on circles');
78
79
		$qb = $this->connection->getQueryBuilder();
80
		$expr = $qb->expr();
81
82
		$qb->select('id')
83
		   ->from('circles_circles')
84
		   ->where($expr->eq('unique_id', $qb->createNamedParameter('')));
85
86
		$result = $qb->execute();
87
88
		$output->startProgress();
89
		while ($row = $result->fetch()) {
90
91
			$itemId = $row['id'];
92
			$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
93
94
			$update = $this->connection->getQueryBuilder();
95
			$update->update('circles_circles')
96
				   ->set('unique_id', $update->createNamedParameter('d' . $uniqueId))
97
				   ->where(
98
					   $update->expr()
99
							  ->eq('id', $update->createNamedParameter($itemId))
100
				   );
101
			$update->execute();
102
103
			$output->advance();
104
105
		}
106
107
		$result->closeCursor();
108
		$output->finishProgress();
109
	}
110
111
}
112