Passed
Push — master ( d3429f...53215a )
by Pauli
02:01
created

BusinessLayer::latestInsertTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * ownCloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Alessandro Cosentino 2012
12
 * @copyright Bernhard Posselt 2012, 2014
13
 * @copyright Pauli Järvinen 2017 - 2021
14
 */
15
16
namespace OCA\Music\AppFramework\BusinessLayer;
17
18
use \OCA\Music\Db\BaseMapper;
19
use \OCA\Music\Db\SortBy;
20
21
use \OCP\AppFramework\Db\DoesNotExistException;
22
use \OCP\AppFramework\Db\Entity;
23
use \OCP\AppFramework\Db\MultipleObjectsReturnedException;
24
25
abstract class BusinessLayer {
26
	protected $mapper;
27
28
	public function __construct(BaseMapper $mapper) {
29
		$this->mapper = $mapper;
30
	}
31
32
	/**
33
	 * Update an entity in the database
34
	 * @param Entity $entity
35
	 */
36
	public function update(Entity $entity) : void {
37
		$this->mapper->update($entity);
38
	}
39
40
	/**
41
	 * Delete an entity
42
	 * @param int $id the id of the entity
43
	 * @param string $userId the name of the user for security reasons
44
	 * @throws BusinessLayerException if the entity does not exist or more than one entity exists
45
	 */
46
	public function delete(int $id, string $userId) : void {
47
		$entity = $this->find($id, $userId);
48
		$this->mapper->delete($entity);
49
	}
50
51
	/**
52
	 * Deletes entities without specifying the owning user.
53
	 * This should never be called directly from the HTML API, but only in case
54
	 * we can actually trust the passed IDs (e.g. file deleted hook).
55
	 * @param array $ids the ids of the entities which should be deleted
56
	 */
57
	public function deleteById(array $ids) : void {
58
		if (\count($ids) > 0) {
59
			$this->mapper->deleteById($ids);
60
		}
61
	}
62
63
	/**
64
	 * Delete all entities of the given user
65
	 */
66
	public function deleteAll(string $userId) : void {
67
		$this->mapper->deleteAll($userId);
68
	}
69
70
	/**
71
	 * Finds an entity by id
72
	 * @param int $id the id of the entity
73
	 * @param string $userId the name of the user for security reasons
74
	 * @throws BusinessLayerException if the entity does not exist or more than one entity exists
75
	 * @return Entity the entity
76
	 */
77
	public function find(int $id, string $userId) : Entity {
78
		try {
79
			return $this->mapper->find($id, $userId);
80
		} catch (DoesNotExistException $ex) {
81
			throw new BusinessLayerException($ex->getMessage());
82
		} catch (MultipleObjectsReturnedException $ex) {
83
			throw new BusinessLayerException($ex->getMessage());
84
		}
85
	}
86
87
	/**
88
	 * Finds an entity by id, or returns an empty entity instance if the requested one is not found
89
	 * @param int $id the id of the entity
90
	 * @param string $userId the name of the user for security reasons
91
	 * @return Entity the entity
92
	 */
93
	public function findOrDefault(int $id, string $userId) : Entity {
94
		try {
95
			return $this->find($id, $userId);
96
		} catch (BusinessLayerException $ex) {
97
			return $this->mapper->createEntity();
98
		}
99
	}
100
101
	/**
102
	 * Find all entities matching the given IDs.
103
	 * Specifying the user is optional; if omitted, the caller should make sure that
104
	 * user's data is not leaked to unauthorized users.
105
	 * @param integer[] $ids  IDs of the entities to be found
106
	 * @param string|null $userId
107
	 * @return Entity[]
108
	 */
109
	public function findById(array $ids, string $userId=null) : array {
110
		if (\count($ids) > 0) {
111
			return $this->mapper->findById($ids, $userId);
112
		} else {
113
			return [];
114
		}
115
	}
116
117
	/**
118
	 * Finds all entities
119
	 * @param string $userId the name of the user
120
	 * @param integer $sortBy sort order of the result set
121
	 * @param integer|null $limit
122
	 * @param integer|null $offset
123
	 * @param string|null $createdMin Optional minimum `created` timestamp.
124
	 * @param string|null $createdMax Optional maximum `created` timestamp.
125
	 * @param string|null $updatedMin Optional minimum `updated` timestamp.
126
	 * @param string|null $updatedMax Optional maximum `updated` timestamp.
127
	 * @return Entity[]
128
	 */
129
	public function findAll(
130
			string $userId, int $sortBy=SortBy::None, int $limit=null, int $offset=null,
131
			?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array {
132
		return $this->mapper->findAll($userId, $sortBy, $limit, $offset, $createdMin, $createdMax, $updatedMin, $updatedMax);
133
	}
134
135
	/**
136
	 * Return all entities with name matching the search criteria
137
	 * @param string $name
138
	 * @param string $userId
139
	 * @param bool $fuzzy
140
	 * @param integer|null $limit
141
	 * @param integer|null $offset
142
	 * @param string|null $createdMin Optional minimum `created` timestamp.
143
	 * @param string|null $createdMax Optional maximum `created` timestamp.
144
	 * @param string|null $updatedMin Optional minimum `updated` timestamp.
145
	 * @param string|null $updatedMax Optional maximum `updated` timestamp.
146
	 * @return Entity[]
147
	 */
148
	public function findAllByName(
149
			string $name, string $userId, bool $fuzzy=false, int $limit=null, int $offset=null,
150
			?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array {
151
		return $this->mapper->findAllByName($name, $userId, $fuzzy, $limit, $offset, $createdMin, $createdMax, $updatedMin, $updatedMax);
152
	}
153
154
	/**
155
	 * Find all starred entities
156
	 * @param string $userId
157
	 * @param integer|null $limit
158
	 * @param integer|null $offset
159
	 * @return Entity[]
160
	 */
161
	public function findAllStarred(string $userId, int $limit=null, int $offset=null) : array {
162
		return $this->mapper->findAllStarred($userId, $limit, $offset);
163
	}
164
165
	/**
166
	 * Set the given entities as "starred" on this date
167
	 * @param int[] $ids
168
	 * @param string $userId
169
	 * @return int number of modified entities
170
	 */
171
	public function setStarred(array $ids, string $userId) : int {
172
		if (\count($ids) > 0) {
173
			return $this->mapper->setStarredDate(new \DateTime(), $ids, $userId);
174
		} else {
175
			return 0;
176
		}
177
	}
178
179
	/**
180
	 * Remove the "starred" status of the given entities
181
	 * @param integer[] $ids
182
	 * @param string $userId
183
	 * @return int number of modified entities
184
	 */
185
	public function unsetStarred(array $ids, string $userId) : int {
186
		if (\count($ids) > 0) {
187
			return $this->mapper->setStarredDate(null, $ids, $userId);
188
		} else {
189
			return 0;
190
		}
191
	}
192
193
	/**
194
	 * Tests if entity with given ID and user ID exists in the database
195
	 * @param int $id
196
	 * @param string $userId
197
	 * @return bool
198
	 */
199
	public function exists(int $id, string $userId) : bool {
200
		return $this->mapper->exists($id, $userId);
201
	}
202
203
	/**
204
	 * Get the number of entities
205
	 * @param string $userId
206
	 */
207
	public function count(string $userId) : int {
208
		return $this->mapper->count($userId);
209
	}
210
211
	/**
212
	 * Get the timestamp of the latest insert operation on the entity type in question
213
	 */
214
	public function latestInsertTime(string $userId) : \DateTime {
215
		return $this->mapper->latestInsertTime($userId) ?? new \DateTime('1970-01-01');
216
	}
217
	
218
	/**
219
	 * Get the timestamp of the latest update operation on the entity type in question
220
	 */
221
	public function latestUpdateTime(string $userId) : \DateTime {
222
		return $this->mapper->latestUpdateTime($userId) ?? new \DateTime('1970-01-01');
223
	}
224
}
225