Completed
Push — php-7.1 ( 77242e...1bf6d8 )
by Pauli
14:40
created

BaseMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 18.6 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 8
loc 43
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A deleteById() 0 8 2
A count() 8 8 1
A questionMarks() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 2016
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\AppFramework\Db\Mapper;
16
use OCP\IDBConnection;
17
18
/**
19
 * Common base class for data access classes of the Music app
20
 */
21
class BaseMapper extends Mapper {
22
23
	public function __construct(IDBConnection $db, $tableName, $entityClass=null){
24
		parent::__construct($db, $tableName, $entityClass);
25
	}
26
27
	/**
28
	 * @param integer[] $ids  IDs of the entities to be deleted
29
	 */
30
	public function deleteById($ids){
31
		$count = count($ids);
32
		if($count === 0) {
33
			return;
34
		}
35
		$sql = 'DELETE FROM `' . $this->getTableName() . '` WHERE `id` IN '. $this->questionMarks($count);
36
		$this->execute($sql, $ids);
37
	}
38
39
	/**
40
	 * @param string $userId
41
	 */
42 View Code Duplication
	public function count($userId){
43
		$sql = 'SELECT COUNT(*) AS count FROM `' . $this->getTableName() . '` '.
44
			'WHERE `user_id` = ?';
45
		$params = array($userId);
46
		$result = $this->execute($sql, $params);
47
		$row = $result->fetch();
48
		return $row['count'];
49
	}
50
51
	/**
52
	 * helper creating a string like '(?,?,?)' with the specified number of elements
53
	 * @param int $count
54
	 */
55
	protected function questionMarks($count) {
56
		$questionMarks = array();
57
		for($i = 0; $i < $count; $i++){
58
			$questionMarks[] = '?';
59
		}
60
		return '(' . implode(',', $questionMarks) . ')';
61
	}
62
63
}
64