Completed
Push — master ( 1da820...8d8a5f )
by Joas
02:21 queued 12s
created

GroupMapper::getGroupsForAnnouncement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019 Joas Schilling <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\AnnouncementCenter\Model;
24
25
26
use OCP\AppFramework\Db\QBMapper;
27
use OCP\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IDBConnection;
29
30
class GroupMapper extends QBMapper {
31
32 1
	public function __construct(IDBConnection $db) {
33 1
		parent::__construct($db, 'announcements_groups', Group::class);
34 1
	}
35
36
	/**
37
	 * @param Announcement $announcement
38
	 * @return Group[]
39
	 */
40
	public function getGroupsForAnnouncement(Announcement $announcement): array {
41
		$result = $this->getGroupsForAnnouncements([$announcement]);
42
		return $result[$announcement->getId()] ?? [];
43
	}
44
45
	/**
46
	 * @param Announcement $announcement
47
	 */
48
	public function deleteGroupsForAnnouncement(Announcement $announcement): void {
49
		$query = $this->db->getQueryBuilder();
50
		$query->delete('announcements_groups')
51
			->where($query->expr()->eq('announcement_id', $query->createNamedParameter($announcement->getId())));
52
		$query->execute();
53
	}
54
55
	/**
56
	 * @param Announcement[] $announcements
57
	 * @return array
58
	 */
59
	public function getGroupsForAnnouncements(array $announcements): array {
60
		$ids = array_map(function (Announcement $announcement) {
61
			return $announcement->getId();
62
		}, $announcements);
63
64
		$query = $this->db->getQueryBuilder();
65
		$query->select('*')
66
			->from('announcements_groups')
67
			->where($query->expr()->in('announcement_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
68
69
		/** @var Group[] $results */
70
		$results = $this->findEntities($query);
71
72
		$groups = [];
73
		foreach ($results as $result) {
74
			if (!isset($groups[$result->getId()])) {
75
				$groups[$result->getId()] = [];
76
			}
77
			$groups[$result->getId()][] = $result->getGroup();
78
		}
79
80
		return $groups;
81
	}
82
}
83