Completed
Push — master ( 0471ec...901b23 )
by René
10:22 queued 06:22
created

EventMapper::findAllForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[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
namespace OCA\Polls\Db;
25
26
use OCP\AppFramework\Db\Mapper;
27
use OCP\IDBConnection;
28
29
class EventMapper extends Mapper {
30
31
	/**
32
	 * EventMapper constructor.
33
	 * @param IDBConnection $db
34
	 */
35 15
	public function __construct(IDBConnection $db) {
36 15
		parent::__construct($db, 'polls_events', '\OCA\Polls\Db\Event');
37 15
	}
38
39
	/**
40
	 * @param Integer $id
41
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
42
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
43
	 * @return Event
44
	 */
45 4
	public function find($id) {
46 4
		$sql = 'SELECT * FROM ' . $this->getTableName() . ' WHERE id = ?';
47 4
		return $this->findEntity($sql, [$id]);
48
	}
49
50
	/**
51
	 * @param String $hash
52
	 * @param Integer $limit
53
	 * @param Integer $offset
54
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
55
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
56
	 * @return Event
57
	 */
58
	public function findByHash($hash, $limit = null, $offset = null) {
59
		$sql = 'SELECT * FROM ' . $this->getTableName() . ' WHERE hash = ?';
60
		return $this->findEntity($sql, [$hash], $limit, $offset);
61
	}
62
63
	/**
64
	 * @param string $userId
65
	 * @param int $limit
66
	 * @param int $offset
67
	 * @return Event[]
68
	 */
69
	public function findAllForUser($userId, $limit = null, $offset = null) {
70
		$sql = 'SELECT * FROM ' . $this->getTableName() . ' WHERE owner = ?';
71
		return $this->findEntities($sql, [$userId], $limit, $offset);
72
	}
73
74
	/**
75
	 * @param int $limit
76
	 * @param int $offset
77
	 * @return Event[]
78
	 */
79
	public function findAll($limit = null, $offset = null) {
80
		$sql = 'SELECT * FROM ' . $this->getTableName();
81
		return $this->findEntities($sql, ['*'], $limit, $offset);
82
	}
83
84
	/**
85
	 * @param string $userId
86
	 * @param int $limit
87
	 * @param int $offset
88
	 * @return Event[]
89
	 */
90
	public function findAllForUserWithInfo($userId, $limit = null, $offset = null) {
91
		$sql = 'SELECT DISTINCT *PREFIX*polls_events.id,
92
								*PREFIX*polls_events.hash,
93
								*PREFIX*polls_events.type,
94
								*PREFIX*polls_events.title,
95
								*PREFIX*polls_events.description,
96
								*PREFIX*polls_events.owner,
97
								*PREFIX*polls_events.created,
98
								*PREFIX*polls_events.access,
99
								*PREFIX*polls_events.expire,
100
								*PREFIX*polls_events.is_anonymous,
101
								*PREFIX*polls_events.full_anonymous
102
				FROM *PREFIX*polls_events
103
				LEFT JOIN *PREFIX*polls_votes
104
					ON *PREFIX*polls_events.id = *PREFIX*polls_votes.id
105
				LEFT JOIN *PREFIX*polls_comments
106
					ON *PREFIX*polls_events.id = *PREFIX*polls_comments.id
107
				WHERE
108
					(*PREFIX*polls_events.access = ? AND *PREFIX*polls_events.owner = ?)
109
					OR
110
					*PREFIX*polls_events.access != ?
111
					OR
112
					*PREFIX*polls_votes.user_id = ?
113
					OR
114
					*PREFIX*polls_comments.user_id = ?
115
					ORDER BY created';
116
		return $this->findEntities($sql, ['hidden', $userId, 'hidden', $userId, $userId], $limit, $offset);
117
	}
118
}
119