Passed
Push — master ( 467f5a...934f83 )
by Pauli
02:02
created

GenreMapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 47
ccs 0
cts 11
cp 0
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findUniqueEntity() 0 3 1
A __construct() 0 2 1
A selectGenres() 0 14 1
A selectEntities() 0 2 1
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 Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020
11
 */
12
13
namespace OCA\Music\Db;
14
15
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...
16
17
class GenreMapper extends BaseMapper {
18
	public function __construct(IDBConnection $db) {
19
		parent::__construct($db, 'music_genres', '\OCA\Music\Db\Genre', 'name');
20
	}
21
22
	/**
23
	 * @see \OCA\Music\Db\BaseMapper::findUniqueEntity()
24
	 * @param Genre $genre
25
	 * @return Genre
26
	 */
27
	protected function findUniqueEntity($genre) {
28
		$sql = $this->selectGenres('`*PREFIX*music_genres`.`user_id` = ? AND `lower_name` = ?');
29
		return $this->findEntity($sql, [$genre->getUserId(), $genre->getLowerName()]);
30
	}
31
32
	/**
33
	 * Create SQL query which selects genres excluding any empty genres (having no tracks)
34
	 * @see \OCA\Music\Db\BaseMapper::selectEntities
35
	 * @param string $condition
36
	 * @param string|null $extension
37
	 * @return string SQL query
38
	 */
39
	protected function selectEntities($condition, $extension=null) {
40
		return $this->selectGenres($condition, 'HAVING COUNT(`track`.`id`) > 0 ' . $extension);
41
	}
42
43
	/**
44
	 * Create SQL query to select genres. Unlike the function selectEntities used by the
45
	 * base class BaseMapper, this function returns also the genres with no tracks at all.
46
	 * @param string $condition 
47
	 * @param string|null $extension
48
	 * @return string SQL query
49
	 */
50
	private function selectGenres($condition, $extension=null) {
51
		return "SELECT
52
					`*PREFIX*music_genres`.`id`,
53
					`*PREFIX*music_genres`.`name`,
54
					`*PREFIX*music_genres`.`lower_name`,
55
					COUNT(`track`.`id`) AS `trackCount`,
56
					COUNT(DISTINCT(`track`.`album_id`)) AS `albumCount`,
57
					COUNT(DISTINCT(`track`.`artist_id`)) AS `artistCount`
58
				FROM `*PREFIX*music_genres`
59
				LEFT JOIN `*PREFIX*music_tracks` `track`
60
				ON `track`.`genre_id` = `*PREFIX*music_genres`.`id`
61
				WHERE $condition
62
				GROUP BY `*PREFIX*music_genres`.`id`, `*PREFIX*music_genres`.`name`, `*PREFIX*music_genres`.`lower_name`
63
				$extension";
64
	}
65
66
}
67