Passed
Pull Request — master (#875)
by Pauli
04:56 queued 02:27
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
			$sql = $this->selectUserEntities('`channel_id` IN ' . $this->questionMarks($channelCount));
33
			return $this->findEntities($sql, \array_merge([$userId], $channelIds), $limit, $offset);
34
		}
35
	}
36
37
	public function deleteByChannel(int $channelId, string $userId) : void {
38
		$this->deleteByCond('`channel_id` = ? AND `user_id` = ?', [$channelId, $userId]);
39
	}
40
41
	public function deleteByChannelExcluding(int $channelId, array $excludedIds, string $userId) : void {
42
		$excludeCount = \count($excludedIds);
43
		if ($excludeCount === 0) {
44
			$this->deleteByChannel($channelId, $userId);
45
		} else {
46
			$this->deleteByCond(
47
				'`channel_id` = ? AND `user_id` = ? AND `id` NOT IN ' . $this->questionMarks($excludeCount),
48
				\array_merge([$channelId, $userId], $excludedIds)
49
			);
50
		}
51
	}
52
53
	/**
54
	 * @see \OCA\Music\Db\BaseMapper::findUniqueEntity()
55
	 * @param PodcastEpisode $episode
56
	 * @return PodcastEpisode
57
	 */
58
	protected function findUniqueEntity(Entity $episode) : Entity {
59
		$sql = $this->selectUserEntities("`guid_hash` = ?");
60
		return $this->findEntity($sql, [$episode->getUserId(), $episode->getGuidHash()]);
61
	}
62
}
63