Completed
Push — master ( 3e4ded...a3a70f )
by Julius
11s
created

CardMapper::mapOwner()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
rs 9.9332
cc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 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
namespace OCA\Deck\Db;
25
26
use OCP\AppFramework\Db\Entity;
27
use OCP\IDBConnection;
28
use OCP\IUserManager;
29
use OCP\Notification\IManager;
30
31
32
class CardMapper extends DeckMapper implements IPermissionMapper {
33
34
	/** @var LabelMapper */
35
	private $labelMapper;
36
	/** @var IUserManager */
37
	private $userManager;
38
	/** @var IManager */
39
	private $notificationManager;
40
	private $databaseType;
41
	private $database4ByteSupport;
42
43 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
		IDBConnection $db,
45
		LabelMapper $labelMapper,
46
		IUserManager $userManager,
47
		IManager $notificationManager,
48
		$databaseType = 'sqlite',
49
		$database4ByteSupport = true
50
	) {
51
		parent::__construct($db, 'deck_cards', Card::class);
52
		$this->labelMapper = $labelMapper;
53
		$this->userManager = $userManager;
54
		$this->notificationManager = $notificationManager;
55
		$this->databaseType = $databaseType;
56
		$this->database4ByteSupport = $database4ByteSupport;
57
	}
58
59
	public function insert(Entity $entity) {
60
		$entity->setDatabaseType($this->databaseType);
61
		$entity->setCreatedAt(time());
62
		$entity->setLastModified(time());
63
		if (!$this->database4ByteSupport) {
64
			$description = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $entity->getDescription());
65
			$entity->setDescription($description);
66
		}
67
		return parent::insert($entity);
68
	}
69
70
	public function update(Entity $entity, $updateModified = true) {
71
		if (!$this->database4ByteSupport) {
72
			$description = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $entity->getDescription());
73
			$entity->setDescription($description);
74
		}
75
		$entity->setDatabaseType($this->databaseType);
76
77
		if ($updateModified) {
78
			$entity->setLastModified(time());
79
		}
80
81
		// make sure we only reset the notification flag if the duedate changes
82
		if (in_array('duedate', $entity->getUpdatedFields(), true)) {
83
			$existing = $this->find($entity->getId());
84
			if ($existing->getDuedate() !== $entity->getDuedate()) {
85
				$entity->setNotified(false);
86
			}
87
			// remove pending notifications
88
			$notification = $this->notificationManager->createNotification();
89
			$notification
90
				->setApp('deck')
91
				->setObject('card', $entity->getId());
92
			$this->notificationManager->markProcessed($notification);
93
		}
94
		return parent::update($entity);
95
	}
96
97
	public function markNotified(Card $card) {
98
		$cardUpdate = new Card();
99
		$cardUpdate->setId($card->getId());
100
		$cardUpdate->setNotified(true);
101
		return parent::update($cardUpdate);
102
	}
103
104
	/**
105
	 * @param $id
106
	 * @return RelationalEntity if not found
107
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
108
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
109
	 */
110
	public function find($id) {
111
		$sql = 'SELECT * FROM `*PREFIX*deck_cards` ' .
112
			'WHERE `id` = ?';
113
		/** @var Card $card */
114
		$card = $this->findEntity($sql, [$id]);
115
		$labels = $this->labelMapper->findAssignedLabelsForCard($card->id);
116
		$card->setLabels($labels);
117
		$this->mapOwner($card);
118
		return $card;
119
	}
120
121
	public function findAll($stackId, $limit = null, $offset = null) {
122
		$sql = 'SELECT * FROM `*PREFIX*deck_cards` 
123
          WHERE `stack_id` = ? AND NOT archived AND deleted_at = 0 ORDER BY `order`';
124
		return $this->findEntities($sql, [$stackId], $limit, $offset);
125
	}
126
127
	public function findDeleted($boardId, $limit = null, $offset = null) {
128
		$sql = 'SELECT c.* FROM `*PREFIX*deck_cards` c
129
	  INNER JOIN `*PREFIX*deck_stacks` s ON s.id = c.stack_id
130
	  WHERE `s`.`board_id` = ? AND NOT c.archived AND NOT c.deleted_at = 0 ORDER BY `c`.`order`';
131
		return $this->findEntities($sql, [$boardId], $limit, $offset);
132
	}
133
134
	public function findAllArchived($stackId, $limit = null, $offset = null) {
135
		$sql = 'SELECT * FROM `*PREFIX*deck_cards` WHERE `stack_id`=? AND archived ORDER BY `last_modified`';
136
		return $this->findEntities($sql, [$stackId], $limit, $offset);
137
	}
138
139
	public function findAllByStack($stackId, $limit = null, $offset = null) {
140
		$sql = 'SELECT id FROM `*PREFIX*deck_cards` 
141
          WHERE `stack_id` = ?';
142
		return $this->findEntities($sql, [$stackId], $limit, $offset);
143
	}
144
145
	public function findOverdue() {
146
		$sql = 'SELECT id,title,duedate,notified from `*PREFIX*deck_cards` WHERE duedate < NOW() AND NOT archived';
147
		return $this->findEntities($sql);
148
	}
149
150
	public function delete(Entity $entity) {
151
		// delete assigned labels
152
		$this->labelMapper->deleteLabelAssignmentsForCard($entity->getId());
153
		// delete card
154
		return parent::delete($entity);
155
	}
156
157
	public function deleteByStack($stackId) {
158
		$cards = $this->findAllByStack($stackId);
159
		foreach ($cards as $card) {
160
			$this->delete($card);
161
		}
162
163
	}
164
165 View Code Duplication
	public function assignLabel($card, $label) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
		$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)';
167
		$stmt = $this->db->prepare($sql);
168
		$stmt->bindParam(1, $label, \PDO::PARAM_INT);
169
		$stmt->bindParam(2, $card, \PDO::PARAM_INT);
170
		$stmt->execute();
171
	}
172
173 View Code Duplication
	public function removeLabel($card, $label) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
		$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?';
175
		$stmt = $this->db->prepare($sql);
176
		$stmt->bindParam(1, $card, \PDO::PARAM_INT);
177
		$stmt->bindParam(2, $label, \PDO::PARAM_INT);
178
		$stmt->execute();
179
	}
180
181 View Code Duplication
	public function isOwner($userId, $cardId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
		$sql = 'SELECT owner FROM `*PREFIX*deck_boards` WHERE `id` IN (SELECT board_id FROM `*PREFIX*deck_stacks` WHERE id IN (SELECT stack_id FROM `*PREFIX*deck_cards` WHERE id = ?))';
183
		$stmt = $this->execute($sql, [$cardId]);
184
		$row = $stmt->fetch();
185
		return ($row['owner'] === $userId);
186
	}
187
188
	public function findBoardId($cardId) {
189
		$sql = 'SELECT id FROM `*PREFIX*deck_boards` WHERE `id` IN (SELECT board_id FROM `*PREFIX*deck_stacks` WHERE id IN (SELECT stack_id FROM `*PREFIX*deck_cards` WHERE id = ?))';
190
		$stmt = $this->execute($sql, [$cardId]);
191
		$row = $stmt->fetch();
192
		return $row['id'];
193
	}
194
195 View Code Duplication
	public function mapOwner(Card &$card) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
		$userManager = $this->userManager;
197
		$card->resolveRelation('owner', function($owner) use (&$userManager) {
198
			$user = $userManager->get($owner);
199
			if ($user !== null) {
200
				return new User($user);
201
			}
202
			return null;
203
		});
204
	}
205
206
207
}
208