Completed
Push — scalability_improvements ( 393404 )
by Pauli
12:12
created

Cache::add()   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 3
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 2017
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\AppFramework\Db\Mapper;
16
use OCP\IDBConnection;
17
18
class Cache extends Mapper {
19
20
	public function __construct(IDBConnection $db){
21
		// there is no entity for this mapper -> '' as entity class name
22
		parent::__construct($db, 'music_cache', '');
23
	}
24
25
	/**
26
	 * @param string $userId
27
	 * @param string $key
28
	 * @param string $data
29
	 */
30
	public function add($userId, $key, $data){
31
		$sql = 'INSERT INTO `*PREFIX*music_cache` '.
32
			'(`user_id`, `key`, `data`) VALUES (?, ?, ?)';
33
		$result = $this->execute($sql, [$userId, $key, $data]);
34
		$result->closeCursor();
35
	}
36
37
	/**
38
	 * @param string $userId
39
	 * @param string $key
40
	 */
41
	public function remove($userId, $key = null){
42
		$sql = 'DELETE FROM `*PREFIX*music_cache` WHERE `user_id` = ?';
43
		$params = [$userId];
44
		if ($key != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
45
			$sql .= 'AND `key` = ?';
46
			$params[] = $key;
47
		}
48
		$result = $this->execute($sql, $params);
49
		$result->closeCursor();
50
	}
51
52
	/**
53
	 * @param string $userId
54
	 * @param string $key
55
	 */
56
	public function get($userId, $key) {
57
		$sql = 'SELECT `data` FROM `*PREFIX*music_cache` '.
58
			'WHERE `user_id` = ? AND `key` = ?';
59
		$result = $this->execute($sql, [$userId, $key]);
60
		$rows = $result->fetchAll();
61
		$result->closeCursor();
62
63
		return count($rows) ? $rows[0]['data'] : null;
64
	}
65
}
66