Passed
Push — feature/909_Ampache_API_improv... ( cb99df...a84036 )
by Pauli
02:41
created

PodcastEpisodeMapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 60
rs 10
c 2
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A findAllByChannel() 0 9 2
A deleteByChannel() 0 2 1
A deleteByChannelExcluding() 0 8 2
A advFormatSqlCondition() 0 6 4
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 - 2023
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IDBConnection;
16
17
/**
18
 * Type hint a base class methdo to help Scrutinizer
19
 * @method PodcastEpisode updateOrInsert(PodcastEpisode $episode)
20
 * @phpstan-extends BaseMapper<PodcastEpisode>
21
 */
22
class PodcastEpisodeMapper extends BaseMapper {
23
	public function __construct(IDBConnection $db) {
24
		parent::__construct($db, 'music_podcast_episodes', PodcastEpisode::class, 'title');
25
	}
26
27
	/**
28
	 * @param int[] $channelIds
29
	 * @return PodcastEpisode[]
30
	 */
31
	public function findAllByChannel(array $channelIds, string $userId, ?int $limit=null, ?int $offset=null) : array {
32
		$channelCount = \count($channelIds);
33
		if ($channelCount === 0) {
34
			return [];
35
		} else {
36
			$condition = '`channel_id` IN ' . $this->questionMarks($channelCount);
37
			$sorting = 'ORDER BY `id` DESC';
38
			$sql = $this->selectUserEntities($condition, $sorting);
39
			return $this->findEntities($sql, \array_merge([$userId], $channelIds), $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
			return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, \array_merge([$userId], $channelIds), $limit, $offset);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
40
		}
41
	}
42
43
	public function deleteByChannel(int $channelId, string $userId) : void {
44
		$this->deleteByCond('`channel_id` = ? AND `user_id` = ?', [$channelId, $userId]);
45
	}
46
47
	public function deleteByChannelExcluding(int $channelId, array $excludedIds, string $userId) : void {
48
		$excludeCount = \count($excludedIds);
49
		if ($excludeCount === 0) {
50
			$this->deleteByChannel($channelId, $userId);
51
		} else {
52
			$this->deleteByCond(
53
				'`channel_id` = ? AND `user_id` = ? AND `id` NOT IN ' . $this->questionMarks($excludeCount),
54
				\array_merge([$channelId, $userId], $excludedIds)
55
			);
56
		}
57
	}
58
59
	/**
60
	 * Overridden from the base implementation to provide support for table-specific rules
61
	 *
62
	 * {@inheritdoc}
63
	 * @see BaseMapper::advFormatSqlCondition()
64
	 */
65
	protected function advFormatSqlCondition(string $rule, string $sqlOp) : string {
66
		switch ($rule) {
67
			case 'podcast':	return "`channel_id` IN (SELECT `id` FROM `*PREFIX*music_podcast_channels` `c` WHERE LOWER(`c`.`title`) $sqlOp LOWER(?))";
68
			case 'time':	return "`duration` $sqlOp ?";
69
			case 'pubdate':	return "`published` $sqlOp ?";
70
			default:		return parent::advFormatSqlCondition($rule, $sqlOp);
71
		}
72
	}
73
74
	/**
75
	 * @see \OCA\Music\Db\BaseMapper::findUniqueEntity()
76
	 * @param PodcastEpisode $episode
77
	 * @return PodcastEpisode
78
	 */
79
	protected function findUniqueEntity(Entity $episode) : Entity {
80
		$sql = $this->selectUserEntities("`guid_hash` = ? AND `channel_id` = ?");
81
		return $this->findEntity($sql, [$episode->getUserId(), $episode->getGuidHash(), $episode->getChannelId()]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity...isode->getChannelId())) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\Music\Db\Entity.
Loading history...
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...oudMapper::findEntity() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
		return /** @scrutinizer ignore-deprecated */ $this->findEntity($sql, [$episode->getUserId(), $episode->getGuidHash(), $episode->getChannelId()]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
The method getGuidHash() does not exist on OCA\Music\Db\Entity. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
		return $this->findEntity($sql, [$episode->getUserId(), $episode->/** @scrutinizer ignore-call */ getGuidHash(), $episode->getChannelId()]);
Loading history...
Bug introduced by
The method getChannelId() does not exist on OCA\Music\Db\Entity. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
		return $this->findEntity($sql, [$episode->getUserId(), $episode->getGuidHash(), $episode->/** @scrutinizer ignore-call */ getChannelId()]);
Loading history...
82
	}
83
}
84