Passed
Push — master ( a9b7a7...f17f1a )
by Pauli
02:14
created

Cache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
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 - 2021
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IDBConnection;
16
17
use \OCA\Music\AppFramework\Db\UniqueConstraintViolationException;
18
19
class Cache {
20
	private $db;
21
22
	public function __construct(IDBConnection $db) {
23
		$this->db = $db;
24
	}
25
26
	/**
27
	 * @param string $userId
28
	 * @param string $key
29
	 * @param string $data
30
	 */
31
	public function add(string $userId, string $key, string $data) : void {
32
		$sql = 'INSERT INTO `*PREFIX*music_cache`
33
				(`user_id`, `key`, `data`) VALUES (?, ?, ?)';
34
		$this->executeUpdate($sql, [$userId, $key, $data]);
35
	}
36
37
	/**
38
	 * @param string $userId
39
	 * @param string $key
40
	 * @param string $data
41
	 */
42
	public function update(string $userId, string $key, string $data) : void {
43
		$sql = 'UPDATE `*PREFIX*music_cache` SET `data` = ?
44
				WHERE `user_id` = ? AND `key` = ?';
45
		$this->executeUpdate($sql, [$data, $userId, $key]);
46
	}
47
48
	/**
49
	 * @param string $userId
50
	 * @param string $key
51
	 * @param string $data
52
	 */
53
	public function set(string $userId, string $key, string $data) : void {
54
		try {
55
			$this->add($userId, $key, $data);
56
		} catch (UniqueConstraintViolationException $e) {
57
			$this->update($userId, $key, $data);
58
		}
59
	}
60
61
	/**
62
	 * Remove one or several key-value pairs
63
	 *
64
	 * @param string $userId User to target, omit to target all users
65
	 * @param string $key Key to target, omit to target all keys
66
	 */
67
	public function remove(string $userId = null, string $key = null) : void {
68
		$sql = 'DELETE FROM `*PREFIX*music_cache`';
69
		$params = [];
70
		if ($userId !== null) {
71
			$sql .= ' WHERE `user_id` = ?';
72
			$params[] = $userId;
73
74
			if ($key !== null) {
75
				$sql .= ' AND `key` = ?';
76
				$params[] = $key;
77
			}
78
		} elseif ($key !== null) {
79
			$sql .= ' WHERE `key` = ?';
80
			$params[] = $key;
81
		}
82
		$this->executeUpdate($sql, $params);
83
	}
84
85
	/**
86
	 * @param string $userId
87
	 * @param string $key
88
	 * @return string|null
89
	 */
90
	public function get(string $userId, string $key) : ?string {
91
		$sql = 'SELECT `data` FROM `*PREFIX*music_cache`
92
				WHERE `user_id` = ? AND `key` = ?';
93
		$result = $this->db->executeQuery($sql, [$userId, $key]);
94
		$rows = $result->fetchAll();
95
		$result->closeCursor();
96
97
		return \count($rows) ? $rows[0]['data'] : null;
98
	}
99
100
	/**
101
	 * Get all key-value pairs of one user, optionally limitting to keys with a given prefix.
102
	 * @param string $userId
103
	 * @param string|null $prefix
104
	 * @return array of arrays with keys 'key', 'data'
105
	 */
106
	public function getAll(string $userId, string $prefix = null) : array {
107
		$sql = 'SELECT `key`, `data` FROM `*PREFIX*music_cache`
108
				WHERE `user_id` = ?';
109
		$params = [$userId];
110
111
		if (!empty($prefix)) {
112
			$sql .= ' AND `key` LIKE ?';
113
			$params[] = $prefix . '%';
114
		}
115
116
		$result = $this->db->executeQuery($sql, $params);
117
		$rows = $result->fetchAll();
118
		$result->closeCursor();
119
120
		return $rows;
121
	}
122
123
	/**
124
	 * Given a cache key and its exact content, return the owning user
125
	 */
126
	public function getOwner(string $key, string $data) : ?string {
127
		$sql = 'SELECT `user_id` FROM `*PREFIX*music_cache`
128
				WHERE `key` = ? AND `data` = ?';
129
		$result = $this->db->executeQuery($sql, [$key, $data]);
130
		$rows = $result->fetchAll();
131
		$result->closeCursor();
132
		
133
		return \count($rows) ? $rows[0]['user_id'] : null;
134
	}
135
136
	private function executeUpdate(string $sql, array $params) {
137
		try {
138
			return $this->db->executeUpdate($sql, $params);
139
		} catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
140
			throw new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e);
141
		} catch (\OCP\DB\Exception $e) {
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
142
			// Nextcloud 21
143
			if ($e->getReason() == \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
144
				throw new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e);
145
			} else {
146
				throw $e;
147
			}
148
		}
149
	}
150
}
151