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

BaseMapper::findById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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 2016
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\AppFramework\Db\Mapper;
16
use OCP\IDBConnection;
17
18
use \Doctrine\DBAL\Exception\UniqueConstraintViolationException;
19
20
/**
21
 * Common base class for data access classes of the Music app
22
 */
23
class BaseMapper extends Mapper {
24
25
	public function __construct(IDBConnection $db, $tableName, $entityClass=null){
26
		parent::__construct($db, $tableName, $entityClass);
27
	}
28
29
	/**
30
	 * Find a single entity by id and user_id
31
	 * @param integer $id
32
	 * @param string $userId
33
	 * @return Entity
34
	 */
35
	public function find($id, $userId){
36
		$sql = 'SELECT * FROM `' . $this->getTableName() . '` WHERE `id` = ? AND `user_id` = ?';
37
		return $this->findEntity($sql, [$id, $userId]);
38
	}
39
40
	/**
41
	 * Find all entities matching the given IDs without specifying the user
42
	 * @param integer[] $ids  IDs of the entities to be found
43
	 * @return Entity[]
44
	 */
45
	public function findById($ids){
46
		$count = count($ids);
47
		$sql = 'SELECT * FROM `' . $this->getTableName() . '` WHERE `id` IN '. $this->questionMarks($count);
48
		return $this->findEntities($sql, $ids);
49
	}
50
51
	/**
52
	 * Delete all entities with given IDs without specifying the user
53
	 * @param integer[] $ids  IDs of the entities to be deleted
54
	 */
55
	public function deleteById($ids){
56
		$count = count($ids);
57
		if($count === 0) {
58
			return;
59
		}
60
		$sql = 'DELETE FROM `' . $this->getTableName() . '` WHERE `id` IN '. $this->questionMarks($count);
61
		$this->execute($sql, $ids);
62
	}
63
64
	/**
65
	 * Count all entities of a user
66
	 * @param string $userId
67
	 */
68 View Code Duplication
	public function count($userId){
69
		$sql = 'SELECT COUNT(*) AS count FROM `' . $this->getTableName() . '` '.
70
			'WHERE `user_id` = ?';
71
		$result = $this->execute($sql, [$userId]);
72
		$row = $result->fetch();
73
		return $row['count'];
74
	}
75
76
	/**
77
	 * Insert an entity, or if an entity with the same identity already exists,
78
	 * update the existing entity.
79
	 * @param Entity $entity
80
	 * @return Entity The inserted or updated entity, containing also the id field
81
	 */
82
	public function insertOrUpdate($entity) {
83
		try {
84
			return $this->insert($entity);
85
		} catch (UniqueConstraintViolationException $ex) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Exception\...raintViolationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
86
			$existingEntity = $this->findUniqueEntity($entity); // this should be implemented by the derived class
87
			$entity->setId($existingEntity->getId());
88
			return $this->update($entity);
89
		}
90
	}
91
92
	/**
93
	 * helper creating a string like '(?,?,?)' with the specified number of elements
94
	 * @param int $count
95
	 */
96
	protected function questionMarks($count) {
97
		$questionMarks = array();
98
		for($i = 0; $i < $count; $i++){
99
			$questionMarks[] = '?';
100
		}
101
		return '(' . implode(',', $questionMarks) . ')';
102
	}
103
104
}
105