AttachmentMapper::isOwner()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.9666
cc 3
nc 5
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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
25
namespace OCA\Deck\Db;
26
27
use OCP\AppFramework\Db\DoesNotExistException;
28
use OCP\AppFramework\Db\Entity;
29
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\IDBConnection;
32
use OCP\IUserManager;
33
use PDO;
34
35
36
class AttachmentMapper extends DeckMapper implements IPermissionMapper {
37
38
	private $cardMapper;
39
	private $userManager;
40
	private $qb;
41
42
	/**
43
	 * AttachmentMapper constructor.
44
	 *
45
	 * @param IDBConnection $db
46
	 * @param CardMapper $cardMapper
47
	 * @param IUserManager $userManager
48
	 */
49
	public function __construct(IDBConnection $db, CardMapper $cardMapper, IUserManager $userManager) {
50
		parent::__construct($db, 'deck_attachment', Attachment::class);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::__construct() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
		$this->cardMapper = $cardMapper;
52
		$this->userManager = $userManager;
53
		$this->qb = $this->db->getQueryBuilder();
54
	}
55
56
	/**
57
	 * @param $id
58
	 * @return Entity|Attachment
59
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
60
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
61
	 */
62
	public function find($id) {
63
		$qb = $this->db->getQueryBuilder();
64
		$qb->select('*')
65
			->from('deck_attachment')
66
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
67
68
		$cursor = $qb->execute();
69
		$row = $cursor->fetch(PDO::FETCH_ASSOC);
70
		if($row === false) {
71
			$cursor->closeCursor();
72
			throw new DoesNotExistException('Did expect one result but found none when executing' . $qb);
73
		}
74
75
		$row2 = $cursor->fetch();
76
		$cursor->closeCursor();
77
		if($row2 !== false ) {
78
			throw new MultipleObjectsReturnedException('Did not expect more than one result when executing' . $query);
79
		}
80
81
		return $this->mapRowToEntity($row);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::mapRowToEntity() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
	}
83
84
	/**
85
	 * Find all attachments for a card
86
	 *
87
	 * @param $cardId
88
	 * @return array
89
	 */
90
	public function findAll($cardId) {
91
		$qb = $this->db->getQueryBuilder();
92
		$qb->select('*')
93
			->from('deck_attachment')
94
			->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)))
95
			->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
96
97
98
		$entities = [];
99
		$cursor = $qb->execute();
100
		while($row = $cursor->fetch()){
101
			$entities[] = $this->mapRowToEntity($row);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::mapRowToEntity() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
102
		}
103
		$cursor->closeCursor();
104
		return $entities;
105
	}
106
107
	/**
108
	 * @param null $cardId
109
	 * @param bool $withOffset
110
	 * @return array
111
	 */
112
	public function findToDelete($cardId = null, $withOffset = true) {
113
		// add buffer of 5 min
114
		$timeLimit = time() - (60 * 5);
115
		$qb = $this->db->getQueryBuilder();
116
		$qb->select('*')
117
			->from('deck_attachment')
118
			->where($qb->expr()->gt('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
119
		if ($withOffset) {
120
			$qb
121
				->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($timeLimit, IQueryBuilder::PARAM_INT)));
122
		}
123
		if ($cardId !== null) {
124
			$qb
125
				->andWhere($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)));
126
		}
127
128
		$entities = [];
129
		$cursor = $qb->execute();
130
		while($row = $cursor->fetch()){
131
			$entities[] = $this->mapRowToEntity($row);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::mapRowToEntity() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
132
		}
133
		$cursor->closeCursor();
134
		return $entities;
135
	}
136
137
138
	/**
139
	 * Check if $userId is owner of Entity with $id
140
	 *
141
	 * @param $userId string userId
142
	 * @param $id int|string unique entity identifier
143
	 * @return boolean
144
	 */
145
	public function isOwner($userId, $id) {
146
		try {
147
			$attachment = $this->find($id);
148
			return $this->cardMapper->isOwner($userId, $attachment->getCardId());
149
		} catch (DoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
150
		} catch (MultipleObjectsReturnedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
151
		}
152
		return false;
153
	}
154
155
	/**
156
	 * Query boardId for Entity of given $id
157
	 *
158
	 * @param $id int|string unique entity identifier
159
	 * @return int|null id of Board
160
	 */
161
	public function findBoardId($id) {
162
		try {
163
			$attachment = $this->find($id);
164
		} catch (\Exception $e) {
165
			return null;
166
		}
167
		return $this->cardMapper->findBoardId($attachment->getCardId());
168
	}
169
}