Completed
Push — master ( 9c910e...d5b8ef )
by Pauli
19s queued 13s
created

PodcastEpisodeMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2021
11
 */
12
13
namespace OCA\Music\Db;
14
15
use \OCP\AppFramework\Db\Entity;
16
use \OCP\IDBConnection;
17
18
class PodcastEpisodeMapper extends BaseMapper {
19
	public function __construct(IDBConnection $db) {
20
		parent::__construct($db, 'music_podcast_episodes', '\OCA\Music\Db\PodcastEpisode', 'title');
21
	}
22
23
	/**
24
	 * @param int[] $channelIds
25
	 * @return PodcastEpisode[]
26
	 */
27
	public function findAllByChannel(array $channelIds, string $userId, ?int $limit=null, ?int $offset=null) : array {
28
		$channelCount = \count($channelIds);
29
		if ($channelCount === 0) {
30
			return [];
31
		} else {
32
			$condition = '`channel_id` IN ' . $this->questionMarks($channelCount);
33
			$sorting = 'ORDER BY `id` DESC';
34
			$sql = $this->selectUserEntities($condition, $sorting);
35
			return $this->findEntities($sql, \array_merge([$userId], $channelIds), $limit, $offset);
36
		}
37
	}
38
39
	public function deleteByChannel(int $channelId, string $userId) : void {
40
		$this->deleteByCond('`channel_id` = ? AND `user_id` = ?', [$channelId, $userId]);
41
	}
42
43
	public function deleteByChannelExcluding(int $channelId, array $excludedIds, string $userId) : void {
44
		$excludeCount = \count($excludedIds);
45
		if ($excludeCount === 0) {
46
			$this->deleteByChannel($channelId, $userId);
47
		} else {
48
			$this->deleteByCond(
49
				'`channel_id` = ? AND `user_id` = ? AND `id` NOT IN ' . $this->questionMarks($excludeCount),
50
				\array_merge([$channelId, $userId], $excludedIds)
51
			);
52
		}
53
	}
54
55
	/**
56
	 * @see \OCA\Music\Db\BaseMapper::findUniqueEntity()
57
	 * @param PodcastEpisode $episode
58
	 * @return PodcastEpisode
59
	 */
60
	protected function findUniqueEntity(Entity $episode) : Entity {
61
		$sql = $this->selectUserEntities("`guid_hash` = ?");
62
		return $this->findEntity($sql, [$episode->getUserId(), $episode->getGuidHash()]);
63
	}
64
}
65