Completed
Pull Request — master (#141)
by Joas
02:27
created

AnnouncementMapper::getById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.9
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\DoesNotExistException;
27
use OCP\AppFramework\Db\QBMapper;
28
use OCP\DB\QueryBuilder\IQueryBuilder;
29
use OCP\IDBConnection;
30
31
class AnnouncementMapper extends QBMapper {
32
33 8
	public function __construct(IDBConnection $db) {
34 8
		parent::__construct($db, 'announcements', Announcement::class);
35 8
	}
36
37
	/**
38
	 * @param int $id
39
	 * @return Announcement
40
	 * @throws DoesNotExistException
41
	 */
42
	public function getById(int $id): Announcement {
43
		$query = $this->db->getQueryBuilder();
44
45
		$query->select('*')
46
			->from($this->getTableName())
47
			->where(
48
				$query->expr()->eq('announcement_id', $query->createNamedParameter($id))
49
			);
50
51
		return $this->findEntity($query);
52
	}
53
54
	/**
55
	 * @param array $userGroups
56
	 * @param int $offsetId
57
	 * @return Announcement[]
58
	 */
59
	public function getAnnouncements(array $userGroups, int $offsetId = 0): array {
60
		$query = $this->db->getQueryBuilder();
61
62
		$query->select('a.announcement_id')
63
			->from($this->getTableName(), 'a')
64
			->orderBy('a.announcement_time', 'DESC')
65
			->groupBy('a.announcement_id')
66
			->setMaxResults(5);
67
68
		if (!empty($userGroups)) {
69
			$query->leftJoin('a', 'announcements_groups', 'ag', $query->expr()->eq(
70
				'a.announcement_id', 'ag.announcement_id'
71
			))
72
				->andWhere($query->expr()->in('ag.gid', $query->createNamedParameter($userGroups, IQueryBuilder::PARAM_STR_ARRAY)));
73
		}
74
75
		if ($offsetId > 0) {
76
			$query->andWhere($query->expr()->lt('a.announcement_id', $query->createNamedParameter($offsetId, IQueryBuilder::PARAM_INT)));
77
		}
78
79
		$ids = [];
80
		$result = $query->execute();
81
		while ($row = $result->fetch()) {
82
			$ids[] = (int) $row['announcement_id'];
83
		}
84
		$result->closeCursor();
85
86
		if (empty($ids)) {
87
			return [];
88
		}
89
90
		$query = $this->db->getQueryBuilder();
91
		$query->select('*')
92
			->from('announcements')
93
			->orderBy('announcement_time', 'DESC')
94
			->where($query->expr()->in('announcement_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
95
96
		return $this->findEntities($query);
97
	}
98
}
99