AnnouncementMapper::getAnnouncements()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 26
cp 0
rs 8.9848
c 0
b 0
f 0
cc 5
nc 16
nop 2
crap 30
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\DoesNotExistException;
27
use OCP\AppFramework\Db\Entity;
28
use OCP\AppFramework\Db\QBMapper;
29
use OCP\DB\QueryBuilder\IQueryBuilder;
30
use OCP\IDBConnection;
31
32
class AnnouncementMapper extends QBMapper {
33
34 1
	public function __construct(IDBConnection $db) {
35 1
		parent::__construct($db, 'announcements', Announcement::class);
36 1
	}
37
38
	/**
39
	 * @param int $id
40
	 * @return Announcement
41
	 * @throws DoesNotExistException
42
	 */
43
	public function getById(int $id): Announcement {
44
		$query = $this->db->getQueryBuilder();
45
46
		$query->select('*')
47
			->from($this->getTableName())
48
			->where(
49
				$query->expr()->eq('announcement_id', $query->createNamedParameter($id))
50
			);
51
52
		return $this->findEntity($query);
53
	}
54
55
	/**
56
	 * Deletes an entity from the table
57
	 * @param Entity $entity the entity that should be deleted
58
	 * @return Entity the deleted entity
59
	 * @since 14.0.0
60
	 */
61
	public function delete(Entity $entity): Entity {
62
		$qb = $this->db->getQueryBuilder();
63
64
		$qb->delete($this->tableName)
65
			->where(
66
				$qb->expr()->eq('announcement_id', $qb->createNamedParameter($entity->getId()))
67
			);
68
		$qb->execute();
69
		return $entity;
70
	}
71
72
	/**
73
	 * @param array $userGroups
74
	 * @param int $offsetId
75
	 * @return Announcement[]
76
	 */
77
	public function getAnnouncements(array $userGroups, int $offsetId = 0): array {
78
		$query = $this->db->getQueryBuilder();
79
80
		$query->select('a.announcement_id')
81
			->from($this->getTableName(), 'a')
82
			->orderBy('a.announcement_time', 'DESC')
83
			->groupBy('a.announcement_id')
84
			->setMaxResults(5);
85
86
		if (!empty($userGroups)) {
87
			$query->leftJoin('a', 'announcements_groups', 'ag', $query->expr()->eq(
88
				'a.announcement_id', 'ag.announcement_id'
89
			))
90
				->andWhere($query->expr()->in('ag.gid', $query->createNamedParameter($userGroups, IQueryBuilder::PARAM_STR_ARRAY)));
91
		}
92
93
		if ($offsetId > 0) {
94
			$query->andWhere($query->expr()->lt('a.announcement_id', $query->createNamedParameter($offsetId, IQueryBuilder::PARAM_INT)));
95
		}
96
97
		$ids = [];
98
		$result = $query->execute();
99
		while ($row = $result->fetch()) {
100
			$ids[] = (int) $row['announcement_id'];
101
		}
102
		$result->closeCursor();
103
104
		if (empty($ids)) {
105
			return [];
106
		}
107
108
		$query = $this->db->getQueryBuilder();
109
		$query->select('*')
110
			->from('announcements')
111
			->orderBy('announcement_time', 'DESC')
112
			->where($query->expr()->in('announcement_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
113
114
		return $this->findEntities($query);
115
	}
116
}
117