Passed
Push — master ( d93244...31d6f2 )
by Pauli
03:37
created

PodcastEpisodeMapper::findUniqueEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 - 2025
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IConfig;
16
use OCP\IDBConnection;
17
18
/**
19
 * Type hint a base class method to help Scrutinizer
20
 * @method PodcastEpisode updateOrInsert(PodcastEpisode $episode)
21
 * @phpstan-extends BaseMapper<PodcastEpisode>
22
 */
23
class PodcastEpisodeMapper extends BaseMapper {
24
	public function __construct(IDBConnection $db, IConfig $config) {
25
		parent::__construct($db, $config, 'music_podcast_episodes', PodcastEpisode::class, 'title', ['user_id', 'guid_hash', 'channel_id'], 'channel_id');
26
	}
27
28
	/**
29
	 * @param int[] $channelIds
30
	 * @return PodcastEpisode[]
31
	 */
32
	public function findAllByChannel(array $channelIds, string $userId, ?int $limit=null, ?int $offset=null) : array {
33
		$channelCount = \count($channelIds);
34
		if ($channelCount === 0) {
35
			return [];
36
		} else {
37
			$condition = '`channel_id` IN ' . $this->questionMarks($channelCount);
38
			$sorting = 'ORDER BY `id` DESC';
39
			$sql = $this->selectUserEntities($condition, $sorting);
40
			return $this->findEntities($sql, \array_merge([$userId], $channelIds), $limit, $offset);
41
		}
42
	}
43
44
	public function deleteByChannel(int $channelId, string $userId) : void {
45
		$this->deleteByCond('`channel_id` = ? AND `user_id` = ?', [$channelId, $userId]);
46
	}
47
48
	public function deleteByChannelExcluding(int $channelId, array $excludedIds, string $userId) : void {
49
		$excludeCount = \count($excludedIds);
50
		if ($excludeCount === 0) {
51
			$this->deleteByChannel($channelId, $userId);
52
		} else {
53
			$this->deleteByCond(
54
				'`channel_id` = ? AND `user_id` = ? AND `id` NOT IN ' . $this->questionMarks($excludeCount),
55
				\array_merge([$channelId, $userId], $excludedIds)
56
			);
57
		}
58
	}
59
60
	/**
61
	 * Overridden from the base implementation to provide support for table-specific rules
62
	 *
63
	 * {@inheritdoc}
64
	 * @see BaseMapper::advFormatSqlCondition()
65
	 */
66
	protected function advFormatSqlCondition(string $rule, string $sqlOp, string $conv) : string {
67
		$condForRule = [
68
			'podcast'	=> "`channel_id` IN (SELECT `id` FROM `*PREFIX*music_podcast_channels` `c` WHERE $conv(`c`.`title`) $sqlOp $conv(?))",
69
			'time'		=> "`duration` $sqlOp ?",
70
			'pubdate'	=> "`published` $sqlOp ?"
71
		];
72
73
		return $condForRule[$rule] ?? parent::advFormatSqlCondition($rule, $sqlOp, $conv);
74
	}
75
}
76