Passed
Pull Request — master (#875)
by Pauli
04:17 queued 01:56
created

PodcastEpisodeMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 43
rs 10
c 3
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A deleteByChannelExcluding() 0 8 2
A findAllByChannel() 0 7 2
A deleteByChannel() 0 2 1
A findUniqueEntity() 0 3 1
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