Passed
Pull Request — master (#1490)
by René
04:02
created

PollMapper::deleteByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[email protected]>
6
 * @author René Gieling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Polls\Db;
26
27
use OCP\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\QBMapper;
30
31
/**
32
 * @template-extends QBMapper<Poll>
33
 */
34
class PollMapper extends QBMapper {
35
	public function __construct(IDBConnection $db) {
36
		parent::__construct($db, 'polls_polls', '\OCA\Polls\Db\Poll');
37 5
	}
38 5
39 5
	/**
40
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
41
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
42
	 * @return Poll
43
	 */
44
	public function find(int $id): Poll {
45
		$qb = $this->db->getQueryBuilder();
46
		$qb->select('*')
47
		   ->from($this->getTableName())
48
		   ->where(
49
			   $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
50
		   );
51
		return $this->findEntity($qb);
52
	}
53
54
	/**
55
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
56
	 * @return Poll[]
57
	 */
58
	public function findAll(): array {
59
		$qb = $this->db->getQueryBuilder();
60
		$qb->select('*')
61
		   ->from($this->getTableName());
62
		return $this->findEntities($qb);
63
	}
64
65
	/**
66
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
67
	 * @return Poll[]
68
	 */
69
	public function findForMe(string $userId): array {
70
		$qb = $this->db->getQueryBuilder();
71
		$qb->select('*')
72
			->from($this->getTableName())
73
			->where($qb->expr()->eq('deleted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
74
			->orWhere($qb->expr()->eq('owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
75
		return $this->findEntities($qb);
76
	}
77
78
	/**
79
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
80
	 * @return Poll[]
81
	 */
82
	public function findForAdmin(string $userId): array {
83
		$qb = $this->db->getQueryBuilder();
84
		$qb->select('*')
85
		   ->from($this->getTableName())
86
		   ->where(
87
			   $qb->expr()->neq('owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
88
		   );
89
90
		return $this->findEntities($qb);
91
	}
92
93
	/**
94
	 * @return void
95
	 */
96
	public function deleteByUserId(string $userId): void {
97
		$query = $this->db->getQueryBuilder();
98
		$query->delete($this->getTableName())
99
			->where('owner = :userId')
100
			->setParameter('userId', $userId);
101
		$query->execute();
102
	}
103
104
}
105