Passed
Push — master ( ed5322...856ddd )
by Pauli
04:54 queued 14s
created

TrackMapper::findAllByCriteria()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 16
nop 9
dl 0
loc 29
rs 9.4222

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2016 - 2023
13
 */
14
15
namespace OCA\Music\Db;
16
17
use OCP\IDBConnection;
18
19
/**
20
 * @phpstan-extends BaseMapper<Track>
21
 */
22
class TrackMapper extends BaseMapper {
23
	public function __construct(IDBConnection $db) {
24
		parent::__construct($db, 'music_tracks', Track::class, 'title');
25
	}
26
27
	/**
28
	 * Override the base implementation to include data from multiple tables
29
	 *
30
	 * {@inheritdoc}
31
	 * @see BaseMapper::selectEntities()
32
	 */
33
	protected function selectEntities(string $condition, string $extension=null) : string {
34
		return "SELECT `*PREFIX*music_tracks`.*, `file`.`name` AS `filename`, `file`.`size`, `file`.`mtime` AS `file_mod_time`,
35
						`album`.`name` AS `album_name`, `artist`.`name` AS `artist_name`, `genre`.`name` AS `genre_name`
36
				FROM `*PREFIX*music_tracks`
37
				INNER JOIN `*PREFIX*filecache` `file`
38
				ON `*PREFIX*music_tracks`.`file_id` = `file`.`fileid`
39
				INNER JOIN `*PREFIX*music_albums` `album`
40
				ON `*PREFIX*music_tracks`.`album_id` = `album`.`id`
41
				INNER JOIN `*PREFIX*music_artists` `artist`
42
				ON `*PREFIX*music_tracks`.`artist_id` = `artist`.`id`
43
				LEFT JOIN `*PREFIX*music_genres` `genre`
44
				ON `*PREFIX*music_tracks`.`genre_id` = `genre`.`id`
45
				WHERE $condition $extension";
46
	}
47
48
	/**
49
	 * Overridden from the base implementation to add support for sorting by artist, play_count, and last_played.
50
	 *
51
	 * {@inheritdoc}
52
	 * @see BaseMapper::formatSortingClause()
53
	 */
54
	protected function formatSortingClause(int $sortBy, bool $invertSort = false) : ?string {
55
		$dir = $invertSort ? 'DESC' : 'ASC';
56
		switch ($sortBy) {
57
			case SortBy::Parent:
58
				// Note: the alternative form "LOWER(`artist_name`) wouldn't work on PostgreSQL, see https://github.com/owncloud/music/issues/1046 for a similar case
59
				return "ORDER BY LOWER(`artist`.`name`) $dir, LOWER(`title`) $dir";
60
			case SortBy::PlayCount:
61
				return "ORDER BY LOWER(`play_count`) $dir";
62
			case SortBy::LastPlayed:
63
				return "ORDER BY LOWER(`last_played`) $dir";
64
			default:
65
				return parent::formatSortingClause($sortBy, $invertSort);
66
		}
67
	}
68
69
	/**
70
	 * Returns all tracks of the given artist (both album and track artists are considered)
71
	 * @return Track[]
72
	 */
73
	public function findAllByArtist(int $artistId, string $userId, ?int $limit=null, ?int $offset=null) : array {
74
		$sql = $this->selectUserEntities(
75
				'`artist_id` = ? OR `album_id` IN (SELECT `id` from `*PREFIX*music_albums` WHERE `album_artist_id` = ?) ',
76
				'ORDER BY LOWER(`title`)');
77
		$params = [$userId, $artistId, $artistId];
78
		return $this->findEntities($sql, $params, $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

78
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params, $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...
79
	}
80
81
	/**
82
	 * @return Track[]
83
	 */
84
	public function findAllByAlbum(int $albumId, string $userId, ?int $artistId=null, ?int $limit=null, ?int $offset=null) : array {
85
		$condition = '`album_id` = ?';
86
		$params = [$userId, $albumId];
87
88
		if ($artistId !== null) {
89
			$condition .= ' AND `artist_id` = ? ';
90
			$params[] = $artistId;
91
		}
92
93
		$sql = $this->selectUserEntities($condition,
94
				'ORDER BY `*PREFIX*music_tracks`.`disk`, `number`, LOWER(`title`)');
95
		return $this->findEntities($sql, $params, $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

95
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params, $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...
96
	}
97
98
	/**
99
	 * @return Track[]
100
	 */
101
	public function findAllByFolder(int $folderId, string $userId, ?int $limit=null, ?int $offset=null) : array {
102
		$sql = $this->selectUserEntities('`file`.`parent` = ?', 'ORDER BY LOWER(`title`)');
103
		$params = [$userId, $folderId];
104
		return $this->findEntities($sql, $params, $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

104
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params, $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...
105
	}
106
107
	/**
108
	 * @return Track[]
109
	 */
110
	public function findAllByGenre(int $genreId, string $userId, ?int $limit=null, ?int $offset=null) : array {
111
		$sql = $this->selectUserEntities('`genre_id` = ?', 'ORDER BY LOWER(`title`)');
112
		$params = [$userId, $genreId];
113
		return $this->findEntities($sql, $params, $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

113
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params, $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...
114
	}
115
116
	/**
117
	 * @param string $userId
118
	 * @return int[]
119
	 */
120
	public function findAllFileIds(string $userId) : array {
121
		$sql = 'SELECT `file_id` FROM `*PREFIX*music_tracks` WHERE `user_id` = ?';
122
		$result = $this->execute($sql, [$userId]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

122
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId]);

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...
123
124
		return \array_map(function ($i) {
125
			return (int)$i['file_id'];
126
		}, $result->fetchAll());
127
	}
128
129
	/**
130
	 * Find a track of user matching a file ID
131
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
132
	 */
133
	public function findByFileId(int $fileId, string $userId) : Track {
134
		$sql = $this->selectUserEntities('`file_id` = ?');
135
		$params = [$userId, $fileId];
136
		return $this->findEntity($sql, $params);
0 ignored issues
show
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

136
		return /** @scrutinizer ignore-deprecated */ $this->findEntity($sql, $params);

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 Best Practice introduced by
The expression return $this->findEntity($sql, $params) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\Music\Db\Track.
Loading history...
137
	}
138
139
	/**
140
	 * Find tracks of user with multiple file IDs
141
	 * @param integer[] $fileIds
142
	 * @param string[] $userIds
143
	 * @return Track[]
144
	 */
145
	public function findByFileIds(array $fileIds, array $userIds) : array {
146
		$sql = $this->selectEntities(
147
				'`*PREFIX*music_tracks`.`user_id` IN ' . $this->questionMarks(\count($userIds)) .
148
				' AND `file_id` IN '. $this->questionMarks(\count($fileIds)));
149
		$params = \array_merge($userIds, $fileIds);
150
		return $this->findEntities($sql, $params);
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

150
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params);

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...
151
	}
152
153
	/**
154
	 * Finds tracks of all users matching one or multiple file IDs
155
	 * @param integer[] $fileIds
156
	 * @return Track[]
157
	 */
158
	public function findAllByFileIds(array $fileIds) : array {
159
		$sql = $this->selectEntities('`file_id` IN '.
160
				$this->questionMarks(\count($fileIds)));
161
		return $this->findEntities($sql, $fileIds);
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

161
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $fileIds);

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...
162
	}
163
164
	public function countByArtist(int $artistId) : int {
165
		$sql = 'SELECT COUNT(*) AS `count` FROM `*PREFIX*music_tracks` WHERE `artist_id` = ?';
166
		$result = $this->execute($sql, [$artistId]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

166
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$artistId]);

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...
167
		$row = $result->fetch();
168
		return (int)$row['count'];
169
	}
170
171
	public function countByAlbum(int $albumId) : int {
172
		$sql = 'SELECT COUNT(*) AS `count` FROM `*PREFIX*music_tracks` WHERE `album_id` = ?';
173
		$result = $this->execute($sql, [$albumId]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

173
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$albumId]);

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...
174
		$row = $result->fetch();
175
		return (int)$row['count'];
176
	}
177
178
	/**
179
	 * @return integer Duration in seconds
180
	 */
181
	public function totalDurationOfAlbum(int $albumId) : int {
182
		$sql = 'SELECT SUM(`length`) AS `duration` FROM `*PREFIX*music_tracks` WHERE `album_id` = ?';
183
		$result = $this->execute($sql, [$albumId]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

183
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$albumId]);

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...
184
		$row = $result->fetch();
185
		return (int)$row['duration'];
186
	}
187
188
	/**
189
	 * Get durations of the given tracks.
190
	 * @param integer[] $trackIds
191
	 * @return array {int => int} where keys are track IDs and values are corresponding durations
192
	 */
193
	public function getDurations(array $trackIds) : array {
194
		$result = [];
195
196
		if (!empty($trackIds)) {
197
			$sql = 'SELECT `id`, `length` FROM `*PREFIX*music_tracks` WHERE `id` IN ' .
198
						$this->questionMarks(\count($trackIds));
199
			$rows = $this->execute($sql, $trackIds)->fetchAll();
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

199
			$rows = /** @scrutinizer ignore-deprecated */ $this->execute($sql, $trackIds)->fetchAll();

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...
200
			foreach ($rows as $row) {
201
				$result[$row['id']] = (int)$row['length'];
202
			}
203
		}
204
		return $result;
205
	}
206
207
	/**
208
	 * @return Track[]
209
	 */
210
	public function findAllByNameRecursive(string $name, string $userId, ?int $limit=null, ?int $offset=null) {
211
		$condition = '( LOWER(`artist`.`name`) LIKE LOWER(?) OR
212
						LOWER(`album`.`name`) LIKE LOWER(?) OR
213
						LOWER(`title`) LIKE LOWER(?) )';
214
		$sql = $this->selectUserEntities($condition, 'ORDER BY LOWER(`title`)');
215
		$name = BaseMapper::prepareSubstringSearchPattern($name);
216
		$params = [$userId, $name, $name, $name];
217
		return $this->findEntities($sql, $params, $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

217
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params, $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...
218
	}
219
220
	/**
221
	 * Returns all tracks specified by name and/or artist name
222
	 * @param string|null $name the name of the track
223
	 * @param string|null $artistName the name of the artist
224
	 * @param string $userId the name of the user
225
	 * @return Track[] Tracks matching the criteria
226
	 */
227
	public function findAllByNameAndArtistName(?string $name, ?string $artistName, string $userId) : array {
228
		$sqlConditions = [];
229
		$params = [$userId];
230
231
		if (!empty($name)) {
232
			$sqlConditions[] = '`title` = ?';
233
			$params[] = $name;
234
		}
235
236
		if (!empty($artistName)) {
237
			$sqlConditions[] = '`artist`.`name` = ?';
238
			$params[] = $artistName;
239
		}
240
241
		// at least one condition has to be given, otherwise return an empty set
242
		if (\count($sqlConditions) > 0) {
243
			$sql = $this->selectUserEntities(\implode(' AND ', $sqlConditions));
244
			return $this->findEntities($sql, $params);
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

244
			return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params);

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...
245
		} else {
246
			return [];
247
		}
248
	}
249
250
	/**
251
	 * Returns all tracks specified by various criteria, all of which are optional
252
	 * @param int[] $genres Array of genre IDs
253
	 * @param int[] $artists Array of artist IDs
254
	 * @param int|null $fromYear Earliest release year to include
255
	 * @param int|null $toYear Latest release year to include
256
	 * @param int $sortBy Sorting rule as defined in the class SortBy
257
	 * @param string $userId the name of the user
258
	 * @return Track[] Tracks matching the criteria
259
	 */
260
	public function findAllByCriteria(
261
			array $genres, array $artists, ?int $fromYear, ?int $toYear,
262
			int $sortBy, bool $invertSort, string $userId, ?int $limit=null, ?int $offset=null) : array {
263
264
		$sqlConditions = [];
265
		$params = [$userId];
266
267
		if (!empty($genres)) {
268
			$sqlConditions[] = '`genre_id` IN ' . $this->questionMarks(\count($genres));
269
			$params = \array_merge($params, $genres);
270
		}
271
272
		if (!empty($artists)) {
273
			$sqlConditions[] = '`artist_id` IN ' . $this->questionMarks(\count($artists));
274
			$params = \array_merge($params, $artists);
275
		}
276
277
		if (!empty($fromYear)) {
278
			$sqlConditions[] = '`year` >= ?';
279
			$params[] = $fromYear;
280
		}
281
282
		if (!empty($toYear)) {
283
			$sqlConditions[] = '`year` <= ?';
284
			$params[] = $toYear;
285
		}
286
287
		$sql = $this->selectUserEntities(\implode(' AND ', $sqlConditions), self::formatSortingClause($sortBy, $invertSort));
0 ignored issues
show
Bug Best Practice introduced by
The method OCA\Music\Db\TrackMapper::formatSortingClause() is not static, but was called statically. ( Ignorable by Annotation )

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

287
		$sql = $this->selectUserEntities(\implode(' AND ', $sqlConditions), self::/** @scrutinizer ignore-call */ formatSortingClause($sortBy, $invertSort));
Loading history...
288
		return $this->findEntities($sql, $params, $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

288
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $params, $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...
289
	}
290
291
	/**
292
	 * Find most frequently played tracks
293
	 * @return Track[]
294
	 */
295
	public function findFrequentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array {
296
		$sql = $this->selectUserEntities('`play_count` > 0', 'ORDER BY `play_count` DESC, LOWER(`title`)');
297
		return $this->findEntities($sql, [$userId], $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

297
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, [$userId], $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...
298
	}
299
300
	/**
301
	 * Find most recently played tracks
302
	 * @return Track[]
303
	 */
304
	public function findRecentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array {
305
		$sql = $this->selectUserEntities('`last_played` IS NOT NULL', 'ORDER BY `last_played` DESC');
306
		return $this->findEntities($sql, [$userId], $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

306
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, [$userId], $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...
307
	}
308
309
	/**
310
	 * Find least recently played tracks
311
	 * @return Track[]
312
	 */
313
	public function findNotRecentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array {
314
		$sql = $this->selectUserEntities(null, 'ORDER BY `last_played` ASC');
315
		return $this->findEntities($sql, [$userId], $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

315
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, [$userId], $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...
316
	}
317
318
	/**
319
	 * Finds all track IDs of the user along with the parent folder ID of each track
320
	 * @return array where keys are folder IDs and values are arrays of track IDs
321
	 */
322
	public function findTrackAndFolderIds(string $userId) : array {
323
		$sql = 'SELECT `track`.`id` AS id, `file`.`name` AS `filename`, `file`.`parent` AS parent
324
				FROM `*PREFIX*music_tracks` `track`
325
				JOIN `*PREFIX*filecache` `file`
326
				ON `track`.`file_id` = `file`.`fileid`
327
				WHERE `track`.`user_id` = ?';
328
329
		$rows = $this->execute($sql, [$userId])->fetchAll();
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

329
		$rows = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId])->fetchAll();

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...
330
331
		// Sort the results according the file names. This can't be made using ORDERBY in the
332
		// SQL query because then we couldn't use the "natural order" comparison algorithm
333
		\usort($rows, function ($a, $b) {
334
			return \strnatcasecmp($a['filename'], $b['filename']);
335
		});
336
337
		// group the files to parent folder "buckets"
338
		$result = [];
339
		foreach ($rows as $row) {
340
			$result[(int)$row['parent']][] = (int)$row['id'];
341
		}
342
343
		return $result;
344
	}
345
346
	/**
347
	 * Find names and parents of the file system nodes with given IDs within the given storage
348
	 * @param int[] $nodeIds
349
	 * @param string $storageId
350
	 * @return array where keys are the node IDs and values are associative arrays
351
	 *         like { 'name' => string, 'parent' => int };
352
	 */
353
	public function findNodeNamesAndParents(array $nodeIds, string $storageId) : array {
354
		$result = [];
355
356
		if (!empty($nodeIds)) {
357
			$sql = 'SELECT `fileid`, `name`, `parent` '.
358
					'FROM `*PREFIX*filecache` `filecache` '.
359
					'JOIN `*PREFIX*storages` `storages` '.
360
					'ON `filecache`.`storage` = `storages`.`numeric_id` '.
361
					'WHERE `storages`.`id` = ? '.
362
					'AND `filecache`.`fileid` IN '. $this->questionMarks(\count($nodeIds));
363
364
			$rows = $this->execute($sql, \array_merge([$storageId], $nodeIds))->fetchAll();
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

364
			$rows = /** @scrutinizer ignore-deprecated */ $this->execute($sql, \array_merge([$storageId], $nodeIds))->fetchAll();

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...
365
366
			foreach ($rows as $row) {
367
				$result[$row['fileid']] = [
368
					'name' => $row['name'],
369
					'parent' => (int)$row['parent']
370
				];
371
			}
372
		}
373
374
		return $result;
375
	}
376
377
	/**
378
	 * Returns all genre IDs associated with the given artist
379
	 * @return int[]
380
	 */
381
	public function getGenresByArtistId(int $artistId, string $userId) : array {
382
		$sql = 'SELECT DISTINCT(`genre_id`) FROM `*PREFIX*music_tracks` WHERE
383
				`genre_id` IS NOT NULL AND `user_id` = ? AND `artist_id` = ?';
384
		$rows = $this->execute($sql, [$userId, $artistId]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

384
		$rows = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId, $artistId]);

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...
385
		return $rows->fetchAll(\PDO::FETCH_COLUMN);
386
	}
387
388
	/**
389
	 * Returns all tracks IDs of the user, organized by the genre_id.
390
	 * @return array where keys are genre IDs and values are arrays of track IDs
391
	 */
392
	public function mapGenreIdsToTrackIds(string $userId) : array {
393
		$sql = 'SELECT `id`, `genre_id` FROM `*PREFIX*music_tracks`
394
				WHERE `genre_id` IS NOT NULL and `user_id` = ?';
395
		$rows = $this->execute($sql, [$userId])->fetchAll();
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

395
		$rows = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId])->fetchAll();

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...
396
397
		$result = [];
398
		foreach ($rows as $row) {
399
			$result[(int)$row['genre_id']][] = (int)$row['id'];
400
		}
401
402
		return $result;
403
	}
404
405
	/**
406
	 * Returns file IDs of the tracks which do not have genre scanned. This is not the same
407
	 * thing as unknown genre, which means that the genre has been scanned but was not found
408
	 * from the track metadata.
409
	 * @return int[]
410
	 */
411
	public function findFilesWithoutScannedGenre(string $userId) : array {
412
		$sql = 'SELECT `track`.`file_id` FROM `*PREFIX*music_tracks` `track`
413
				INNER JOIN `*PREFIX*filecache` `file`
414
				ON `track`.`file_id` = `file`.`fileid`
415
				WHERE `genre_id` IS NULL and `user_id` = ?';
416
		$rows = $this->execute($sql, [$userId]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

416
		$rows = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId]);

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...
417
		return $rows->fetchAll(\PDO::FETCH_COLUMN);
418
	}
419
420
	/**
421
	 * Update "last played" timestamp and increment the total play count of the track.
422
	 * The DB row is updated *without* updating the `updated` column.
423
	 * @return bool true if the track was found and updated, false otherwise
424
	 */
425
	public function recordTrackPlayed(int $trackId, string $userId, \DateTime $timeOfPlay) : bool {
426
		$sql = 'UPDATE `*PREFIX*music_tracks`
427
				SET `last_played` = ?, `play_count` = `play_count` + 1
428
				WHERE `user_id` = ? AND `id` = ?';
429
		$params = [$timeOfPlay->format(BaseMapper::SQL_DATE_FORMAT), $userId, $trackId];
430
		$result = $this->execute($sql, $params);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() 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

430
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, $params);

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...
431
		return ($result->rowCount() > 0);
432
	}
433
434
	/**
435
	 * @see \OCA\Music\Db\BaseMapper::findUniqueEntity()
436
	 * @param Track $track
437
	 * @return Track
438
	 */
439
	protected function findUniqueEntity(Entity $track) : Entity {
440
		return $this->findByFileId($track->getFileId(), $track->getUserId());
0 ignored issues
show
Bug introduced by
The method getFileId() 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

440
		return $this->findByFileId($track->/** @scrutinizer ignore-call */ getFileId(), $track->getUserId());
Loading history...
Bug introduced by
It seems like $track->getFileId() can also be of type null; however, parameter $fileId of OCA\Music\Db\TrackMapper::findByFileId() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

440
		return $this->findByFileId(/** @scrutinizer ignore-type */ $track->getFileId(), $track->getUserId());
Loading history...
441
	}
442
}
443