CardMapper::findAllArchived()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 3
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
	public function __construct(
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);
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...
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() 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...
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() 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...
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() 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
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]);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntity() 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...
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, $since = -1) {
122
		$sql = 'SELECT * FROM `*PREFIX*deck_cards` 
123
          WHERE `stack_id` = ? AND NOT archived AND deleted_at = 0 AND last_modified > ? ORDER BY `order`';
124
		return $this->findEntities($sql, [$stackId, $since], $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() 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...
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() 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
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() 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...
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() 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...
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);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() 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...
148
	}
149
150
	public function findUnexposedDescriptionChances() {
151
		$sql = 'SELECT id,title,duedate,notified,description_prev,last_editor,description from `*PREFIX*deck_cards` WHERE last_editor IS NOT NULL AND description_prev IS NOT NULL';
152
		return $this->findEntities($sql);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::findEntities() 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...
153
	}
154
155
	public function delete(Entity $entity) {
156
		// delete assigned labels
157
		$this->labelMapper->deleteLabelAssignmentsForCard($entity->getId());
158
		// delete card
159
		return parent::delete($entity);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() 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...
160
	}
161
162
	public function deleteByStack($stackId) {
163
		$cards = $this->findAllByStack($stackId);
164
		foreach ($cards as $card) {
165
			$this->delete($card);
166
		}
167
168
	}
169
170 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...
171
		$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)';
172
		$stmt = $this->db->prepare($sql);
173
		$stmt->bindParam(1, $label, \PDO::PARAM_INT);
174
		$stmt->bindParam(2, $card, \PDO::PARAM_INT);
175
		$stmt->execute();
176
	}
177
178 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...
179
		$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?';
180
		$stmt = $this->db->prepare($sql);
181
		$stmt->bindParam(1, $card, \PDO::PARAM_INT);
182
		$stmt->bindParam(2, $label, \PDO::PARAM_INT);
183
		$stmt->execute();
184
	}
185
186 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...
187
		$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 = ?))';
188
		$stmt = $this->execute($sql, [$cardId]);
189
		$row = $stmt->fetch();
190
		return ($row['owner'] === $userId);
191
	}
192
193
	public function findBoardId($cardId) {
194
		$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 = ?))';
195
		$stmt = $this->execute($sql, [$cardId]);
196
		$row = $stmt->fetch();
197
		return $row['id'];
198
	}
199
200 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...
201
		$userManager = $this->userManager;
202
		$card->resolveRelation('owner', function($owner) use (&$userManager) {
203
			$user = $userManager->get($owner);
204
			if ($user !== null) {
205
				return new User($user);
206
			}
207
			return null;
208
		});
209
	}
210
211
212
}
213