Passed
Push — master ( de702b...3e9331 )
by Pauli
02:18
created

ArtistMapper::removeCovers()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 4
nop 2
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 12
rs 9.7998
c 0
b 0
f 0
1
<?php
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 Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2016 - 2020
13
 */
14
15
namespace OCA\Music\Db;
16
17
use \OCA\Music\Utility\Util;
18
19
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
class ArtistMapper extends BaseMapper {
22
	public function __construct(IDBConnection $db) {
23
		parent::__construct($db, 'music_artists', '\OCA\Music\Db\Artist', 'name');
24
	}
25
26
	/**
27
	 * @param string $userId
28
	 * @param integer $sortBy sort order of the result set
29
	 * @return Artist[]
30
	 */
31
	public function findAllHavingAlbums($userId, $sortBy=SortBy::None) {
32
		$sql = $this->selectUserEntities('EXISTS '.
33
				'(SELECT 1 FROM `*PREFIX*music_albums` `album` '.
34
				' WHERE `*PREFIX*music_artists`.`id` = `album`.`album_artist_id`)',
35
				($sortBy == SortBy::Name) ? 'ORDER BY LOWER(`name`)' : null);
36
37
		$params = [$userId];
38
		return $this->findEntities($sql, $params);
39
	}
40
41
	/**
42
	 * @param int $genreId
43
	 * @param string $userId
44
	 * @param int|null $limit
45
	 * @param int|null $offset
46
	 * @return Artist[]
47
	 */
48
	public function findAllByGenre($genreId, $userId, $limit=null, $offset=null) {
49
		$sql = $this->selectUserEntities('EXISTS '.
50
				'(SELECT 1 FROM `*PREFIX*music_tracks` `track`
51
				  WHERE `*PREFIX*music_artists`.`id` = `track`.`artist_id`
52
				  AND `track`.`genre_id` = ?)');
53
54
		$params = [$userId, $genreId];
55
		return $this->findEntities($sql, $params, $limit, $offset);
56
	}
57
58
	/**
59
	 * @param integer[] $coverFileIds
60
	 * @param string[]|null $userIds the users whose music library is targeted; all users are targeted if omitted
61
	 * @return Artist[] artists which got modified (with incomplete data, only id and user are valid),
62
	 *         empty array if none
63
	 */
64
	public function removeCovers($coverFileIds, $userIds=null) {
65
		// find albums using the given file as cover
66
		$sql = 'SELECT `id`, `user_id` FROM `*PREFIX*music_artists` WHERE `cover_file_id` IN ' .
67
		$this->questionMarks(\count($coverFileIds));
68
		$params = $coverFileIds;
69
		if ($userIds !== null) {
70
			$sql .= ' AND `user_id` IN ' . $this->questionMarks(\count($userIds));
71
			$params = \array_merge($params, $userIds);
72
		}
73
		$artists = $this->findEntities($sql, $params);
74
75
		// if any artists found, remove the cover from those
76
		$count = \count($artists);
77
		if ($count) {
78
			$sql = 'UPDATE `*PREFIX*music_artists`
79
					SET `cover_file_id` = NULL
80
					WHERE `id` IN ' . $this->questionMarks($count);
81
			$params = Util::extractIds($artists);
82
			$this->execute($sql, $params);
83
		}
84
85
		return $artists;
86
	}
87
88
	/**
89
	 * @see \OCA\Music\Db\BaseMapper::findUniqueEntity()
90
	 * @param Artist $artist
91
	 * @return Artist
92
	 */
93
	protected function findUniqueEntity($artist) {
94
		$sql = $this->selectUserEntities('`hash` = ?');
95
		return $this->findEntity($sql, [$artist->getUserId(), $artist->getHash()]);
96
	}
97
}
98