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

ArtistMapper::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 1
eloc 4
nc 1
nop 2
crap 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 ArtistMapper extends BaseMapper {
18
19
	public function __construct(IDBConnection $db){
20
		parent::__construct($db, 'music_artists', '\OCA\Music\Db\Artist');
21
	}
22
23
	/**
24
	 * @param string $condition
25
	 */
26
	private function makeSelectQuery($condition=null){
27
		return 'SELECT `artist`.`name`, `artist`.`image`, `artist`.`id`, '.
28
			'`artist`.`mbid`, `artist`.`hash` FROM `*PREFIX*music_artists` `artist` '.
29
			'WHERE `artist`.`user_id` = ? ' . $condition;
30
	}
31
32
	/**
33
	 * @param string $userId
34
	 * @return Artist[]
35
	 */
36
	public function findAll($userId){
37
		$sql = $this->makeSelectQuery('ORDER BY LOWER(`artist`.`name`)');
38
		$params = array($userId);
39
		return $this->findEntities($sql, $params);
40
	}
41
42
	/**
43
	 * @param integer[] $artistIds
44
	 * @param string $userId
45
	 * @return Artist[]
46
	 */
47
	public function findMultipleById($artistIds, $userId){
48
		$sql = $this->makeSelectQuery('AND `artist`.`id` IN '
49
			. $this->questionMarks(count($artistIds))
50
			. ' ORDER BY LOWER(`artist`.`name`)');
51
		$params = $artistIds;
52
		array_unshift($params, $userId);
53
		return $this->findEntities($sql, $params);
54
	}
55
56
	/**
57
	 * @param string|null $artistName
58
	 * @param string $userId
59
	 * @param bool $fuzzy
60
	 */
61
	protected function makeFindByNameSqlAndParams($artistName, $userId, $fuzzy = false) {
62
		if ($artistName === null) {
63
			$condition = 'AND `artist`.`name` IS NULL';
64
			$params = array($userId);
65
		} elseif($fuzzy) {
66
			$condition = 'AND LOWER(`artist`.`name`) LIKE LOWER(?)';
67
			$params = array($userId, '%' . $artistName . '%');
68
		} else {
69
			$condition = 'AND `artist`.`name` = ?';
70
			$params = array($userId, $artistName);
71
		}
72
		$sql = $this->makeSelectQuery($condition . ' ORDER BY LOWER(`artist`.`name`)');
73
		return array(
74
			'sql' => $sql,
75
			'params' => $params,
76
		);
77
	}
78
79
	/**
80
	 * @param string|null $artistName
81
	 * @param string $userId
82
	 * @param bool $fuzzy
83
	 * @return Artist
84
	 */
85
	public function findByName($artistName, $userId, $fuzzy = false){
86
		$sqlAndParams = $this->makeFindByNameSqlAndParams($artistName, $userId, $fuzzy);
87
		return $this->findEntity($sqlAndParams['sql'], $sqlAndParams['params']);
88
	}
89
90
	/**
91
	 * @param string|null $artistName
92
	 * @param string $userId
93
	 * @param bool $fuzzy
94
	 * @return Artist[]
95
	 */
96
	public function findAllByName($artistName, $userId, $fuzzy = false){
97
		$sqlAndParams = $this->makeFindByNameSqlAndParams($artistName, $userId, $fuzzy);
98
		return $this->findEntities($sqlAndParams['sql'], $sqlAndParams['params']);
99
	}
100
101
	public function findUniqueEntity(Artist $artist){
102
		return $this->findEntity(
103
				'SELECT * FROM `*PREFIX*music_artists` WHERE `user_id` = ? AND `hash` = ?',
104
				[$artist->getUserId(), $artist->getHash()]
105
		);
106
	}
107
}
108