Completed
Push — master ( 7dfd43...946082 )
by Joas
22s
created

GroupMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 12%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 54
ccs 3
cts 25
cp 0.12
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getGroupsForAnnouncement() 0 4 1
A deleteGroupsForAnnouncement() 0 7 2
A getGroupsForAnnouncements() 0 23 3
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
		$groups = $this->getGroupsForAnnouncement($announcement);
50
51
		foreach ($groups as $group) {
52
			$this->delete($group);
53
		}
54
	}
55
56
	/**
57
	 * @param Announcement[] $announcements
58
	 * @return array
59
	 */
60
	public function getGroupsForAnnouncements(array $announcements): array {
61
		$ids = array_map(function (Announcement $announcement) {
62
			return $announcement->getId();
63
		}, $announcements);
64
65
		$query = $this->db->getQueryBuilder();
66
		$query->select('*')
67
			->from('announcements_groups')
68
			->where($query->expr()->in('announcement_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
69
70
		/** @var Group[] $results */
71
		$results = $this->findEntities($query);
72
73
		$groups = [];
74
		foreach ($results as $result) {
75
			if (!isset($groups[$result->getId()])) {
76
				$groups[$result->getId()] = [];
77
			}
78
			$groups[$result->getId()][] = $result->getGroup();
79
		}
80
81
		return $groups;
82
	}
83
}
84