Completed
Push — scanner_improvements ( 4bed41 )
by Pauli
10:32
created

TrackMapper::findByFileIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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
 * @copyright Morris Jobke 2013, 2014
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IDBConnection;
16
17
class TrackMapper extends BaseMapper {
18
19
	public function __construct(IDBConnection $db){
20
		parent::__construct($db, 'music_tracks', '\OCA\Music\Db\Track');
21
	}
22
23
	/**
24
	 * @param string $condition
25
	 */
26
	private function makeSelectQueryWithoutUserId($condition){
27
		return 'SELECT * FROM `*PREFIX*music_tracks` `track` '.
28
			'WHERE ' . $condition;
29
	}
30
31
	/**
32
	 * @param string $condition
33
	 */
34
	private function makeSelectQuery($condition=null){
35
		return $this->makeSelectQueryWithoutUserId('`track`.`user_id` = ? ' . $condition);
36
	}
37
38
	/**
39
	 * @param string $userId
40
	 * @param integer $limit
41
	 * @param integer $offset
42
	 * @return Track[]
43
	 */
44
	public function findAll($userId, $limit=null, $offset=null){
45
		$sql = $this->makeSelectQuery('ORDER BY LOWER(`track`.`title`)');
46
		$params = array($userId);
47
		return $this->findEntities($sql, $params, $limit, $offset);
48
	}
49
50
	/**
51
	 * @param integer $artistId
52
	 * @param string $userId
53
	 * @return Track[]
54
	 */
55
	public function findAllByArtist($artistId, $userId){
56
		$sql = $this->makeSelectQuery('AND `track`.`artist_id` = ? '.
57
			'ORDER BY LOWER(`track`.`title`)');
58
		$params = array($userId, $artistId);
59
		return $this->findEntities($sql, $params);
60
	}
61
62
	/**
63
	 * @param integer $albumId
64
	 * @param string $userId
65
	 * @param integer $artistId
66
	 * @return Track[]
67
	 */
68
	public function findAllByAlbum($albumId, $userId, $artistId = null){
69
		$sql = $this->makeSelectQuery('AND `track`.`album_id` = ? ');
70
		$params = array($userId, $albumId);
71
		if($artistId !== null) {
72
			$sql .= 'AND `track`.`artist_id` = ? ';
73
			array_push($params, $artistId);
74
		}
75
		$sql .= 'ORDER BY LOWER(`track`.`title`)';
76
		return $this->findEntities($sql, $params);
77
	}
78
79
	/**
80
	 * @param string $userId
81
	 * @return int[]
82
	 */
83
	public function findAllFileIds($userId){
84
		$sql = 'SELECT `file_id` FROM `*PREFIX*music_tracks` WHERE `user_id` = ?';
85
		$result = $this->execute($sql, [$userId]);
86
87
		return array_map(function($i) { return $i['file_id']; }, $result->fetchAll());
88
	}
89
90
	/**
91
	 * Find a track of user matching a file ID
92
	 * @param integer $fileId
93
	 * @param string $userId
94
	 * @return Track
95
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
96
	 */
97
	public function findByFileId($fileId, $userId){
98
		$sql = $this->makeSelectQuery('AND `track`.`file_id` = ?');
99
		$params = array($userId, $fileId);
100
		return $this->findEntity($sql, $params);
101
	}
102
103
	/**
104
	 * Find tracks of user with multiple file IDs
105
	 * @param integer[] $fileIds
106
	 * @param string $userId
107
	 * @return Track[]
108
	 */
109
	public function findByFileIds($fileIds, $userId){
110
		$sql = $this->makeSelectQuery('AND `track`.`file_id` IN '.
111
				$this->questionMarks(count($fileIds)));
112
		$params = array_merge([$userId], $fileIds);
113
		return $this->findEntities($sql, $params);
114
	}
115
116
	/**
117
	 * Finds tracks of all users matching one or multiple file IDs
118
	 * @param integer[] $fileIds
119
	 * @return Track[]
120
	 */
121
	public function findAllByFileIds($fileIds){
122
		$sql = $this->makeSelectQueryWithoutUserId('`track`.`file_id` IN '.
123
				$this->questionMarks(count($fileIds)));
124
		return $this->findEntities($sql, $fileIds);
125
	}
126
127
	/**
128
	 * @param integer $artistId
129
	 * @return integer
130
	 */
131 View Code Duplication
	public function countByArtist($artistId){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
		$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '.
133
			'WHERE `track`.`artist_id` = ?';
134
		$result = $this->execute($sql, [$artistId]);
135
		$row = $result->fetch();
136
		return $row['count'];
137
	}
138
139
	/**
140
	 * @param integer $albumId
141
	 * @return integer
142
	 */
143 View Code Duplication
	public function countByAlbum($albumId){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
		$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '.
145
			'WHERE `track`.`album_id` = ?';
146
		$result = $this->execute($sql, [$albumId]);
147
		$row = $result->fetch();
148
		return $row['count'];
149
	}
150
151
	/**
152
	 * @param string $name
153
	 * @param string $userId
154
	 * @param bool $fuzzy
155
	 * @return Track[]
156
	 */
157 View Code Duplication
	public function findAllByName($name, $userId, $fuzzy = false){
158
		if ($fuzzy) {
159
			$condition = 'AND LOWER(`track`.`title`) LIKE LOWER(?) ';
160
			$name = '%' . $name . '%';
161
		} else {
162
			$condition = 'AND `track`.`title` = ? ';
163
		}
164
		$sql = $this->makeSelectQuery($condition . 'ORDER BY LOWER(`track`.`title`)');
165
		$params = array($userId, $name);
166
		return $this->findEntities($sql, $params);
167
	}
168
169
	/**
170
	 * @param string $name
171
	 * @param string $userId
172
	 * @return Track[]
173
	 */
174
	public function findAllByNameRecursive($name, $userId){
175
		$condition = ' AND (`track`.`artist_id` IN (SELECT `id` FROM `*PREFIX*music_artists` WHERE LOWER(`name`) LIKE LOWER(?)) OR '.
176
						' `track`.`album_id` IN (SELECT `id` FROM `*PREFIX*music_albums` WHERE LOWER(`name`) LIKE LOWER(?)) OR '.
177
						' LOWER(`track`.`title`) LIKE LOWER(?) ) ORDER BY LOWER(`track`.`title`)';
178
		$sql = $this->makeSelectQuery($condition);
179
		$name = '%' . $name . '%';
180
		$params = array($userId, $name, $name, $name);
181
		return $this->findEntities($sql, $params);
182
	}
183
184
	public function findUniqueEntity(Track $track) {
185
		return $this->findByFileId($track->getFileId(), $track->getUserId());
186
	}
187
}
188