Completed
Push — master ( 4f34f2...9208b7 )
by Roeland
8s
created

AnnouncementsGroupsLinks   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 11.54%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 58
ccs 3
cts 26
cp 0.1154
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getName() 0 3 1
B run() 0 27 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 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
25
namespace OCA\AnnouncementCenter\Migration;
26
27
28
use OCP\IDBConnection;
29
use OCP\Migration\IOutput;
30
use OCP\Migration\IRepairStep;
31
32
class AnnouncementsGroupsLinks implements IRepairStep {
33
34
	/** @var IDBConnection */
35
	protected $connection;
36
37
	/**
38
	 * @param IDBConnection $connection
39
	 */
40 1
	public function __construct(IDBConnection $connection) {
41 1
		$this->connection = $connection;
42 1
	}
43
44
	/**
45
	 * Returns the step's name
46
	 *
47
	 * @return string
48
	 * @since 9.1.0
49
	 */
50
	public function getName() {
51
		return 'Add read permissions for existing announcements';
52
	}
53
54
	/**
55
	 * Run repair step.
56
	 * Must throw exception on error.
57
	 *
58
	 * @since 9.1.0
59
	 * @param IOutput $output
60
	 * @throws \Exception in case of failure
61
	 */
62
	public function run(IOutput $output) {
63
		$queryInsert = $this->connection->getQueryBuilder();
64
		$queryInsert->insert('announcements_groups')
65
			->values([
66
				'announcement_id' => $queryInsert->createParameter('aid'),
67
				'gid' => $queryInsert->createNamedParameter('everyone'),
68
			]);
69
70
		$query = $this->connection->getQueryBuilder();
71
		$query->select(['a.announcement_id', 'a.announcement_subject'])
72
			->from('announcements', 'a')
73
			->leftJoin('a', 'announcements_groups', 'ag', $query->expr()->eq(
74
				'a.announcement_id', 'ag.announcement_id'
75
			))
76
			->where($query->expr()->isNull('ag.gid'));
77
		$result = $query->execute();
78
79
		$output->startProgress();
80
		while ($row = $result->fetch()) {
81
			$output->advance(1, $row['announcement_subject']);
82
			$queryInsert->setParameter('aid', (int) $row['announcement_id'])
83
				->execute();
84
		}
85
		$output->finishProgress();
86
87
		$result->closeCursor();
88
	}
89
}
90