Passed
Push — master ( 89d351...c23d0c )
by Pauli
01:55
created

BusinessLayer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
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 - 2020
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;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\DoesNotExistException 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...
22
use \OCP\AppFramework\Db\MultipleObjectsReturnedException;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\Mult...bjectsReturnedException 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...
23
24
abstract class BusinessLayer {
25
	protected $mapper;
26
27 18
	public function __construct(BaseMapper $mapper) {
28 18
		$this->mapper = $mapper;
29 18
	}
30
31
	/**
32
	 * Update an entity in the database
33
	 * @param Entity $entity
0 ignored issues
show
Bug introduced by
The type OCA\Music\AppFramework\BusinessLayer\Entity 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...
34
	 */
35
	public function update($entity) {
36
		$this->mapper->update($entity);
37
	}
38
39
	/**
40
	 * Delete an entity
41
	 * @param int $id the id of the entity
42
	 * @param string $userId the name of the user for security reasons
43
	 * @throws BusinessLayerException if the entity does not exist or more than one entity exists
44
	 */
45
	public function delete($id, $userId) {
46
		$entity = $this->find($id, $userId);
47
		$this->mapper->delete($entity);
48
	}
49
50
	/**
51
	 * Deletes entities without specifying the owning user.
52
	 * This should never be called directly from the HTML API, but only in case
53
	 * we can actually trust the passed IDs (e.g. file deleted hook).
54
	 * @param array $ids the ids of the entities which should be deleted
55
	 */
56 4
	public function deleteById($ids) {
57 4
		if (\count($ids) > 0) {
58 4
			$this->mapper->deleteById($ids);
59
		}
60 4
	}
61
62
	/**
63
	 * Finds an entity by id
64
	 * @param int $id the id of the entity
65
	 * @param string $userId the name of the user for security reasons
66
	 * @throws BusinessLayerException if the entity does not exist or more than one entity exists
67
	 * @return Entity the entity
68
	 */
69 1
	public function find($id, $userId) {
70
		try {
71 1
			return $this->mapper->find($id, $userId);
72
		} catch (DoesNotExistException $ex) {
73
			throw new BusinessLayerException($ex->getMessage());
74
		} catch (MultipleObjectsReturnedException $ex) {
75
			throw new BusinessLayerException($ex->getMessage());
76
		}
77
	}
78
79
	/**
80
	 * Finds an entity by id, or returns an empty entity instance if the requested one is not found
81
	 * @param int $id the id of the entity
82
	 * @param string $userId the name of the user for security reasons
83
	 * @return Entity the entity
84
	 */
85
	public function findOrDefault($id, $userId) {
86
		try {
87
			return $this->find($id, $userId);
88
		} catch (BusinessLayerException $ex) {
89
			return $this->mapper->createEntity();
90
		}
91
	}
92
93
	/**
94
	 * Find all entities matching the given IDs.
95
	 * Specifying the user is optional; if omitted, the caller should make sure that
96
	 * user's data is not leaked to unauthorized users.
97
	 * @param integer[] $ids  IDs of the entities to be found
98
	 * @param string|null $userId
99
	 * @return Entity[]
100
	 */
101 1
	public function findById($ids, $userId=null) {
102 1
		if (\count($ids) > 0) {
103 1
			return $this->mapper->findById($ids, $userId);
104
		} else {
105
			return [];
106
		}
107
	}
108
109
	/**
110
	 * Finds all entities
111
	 * @param string $userId the name of the user
112
	 * @param integer $sortBy sort order of the result set
113
	 * @param integer|null $limit
114
	 * @param integer|null $offset
115
	 * @return Entity[]
116
	 */
117 2
	public function findAll($userId, $sortBy=SortBy::None, $limit=null, $offset=null) {
118 2
		return $this->mapper->findAll($userId, $sortBy, $limit, $offset);
119
	}
120
121
	/**
122
	 * Return all entities with name matching the search criteria
123
	 * @param string $name
124
	 * @param string $userId
125
	 * @param bool $fuzzy
126
	 * @param integer|null $limit
127
	 * @param integer|null $offset
128
	 * @return Entity[]
129
	 */
130
	public function findAllByName($name, $userId, $fuzzy = false, $limit=null, $offset=null) {
131
		return $this->mapper->findAllByName($name, $userId, $fuzzy, $limit, $offset);
132
	}
133
134
	/**
135
	 * Find all starred entities
136
	 * @param string $userId
137
	 * @param integer|null $limit
138
	 * @param integer|null $offset
139
	 * @return Entity[]
140
	 */
141
	public function findAllStarred($userId, $limit=null, $offset=null) {
142
		return $this->mapper->findAllStarred($userId, $limit, $offset);
143
	}
144
145
	/**
146
	 * Set the given entities as "starred" on this date
147
	 * @param integer[] $ids
148
	 * @param string $userId
149
	 * @return int number of modified entities
150
	 */
151
	public function setStarred($ids, $userId) {
152
		if (\count($ids) > 0) {
153
			return $this->mapper->setStarredDate(new \DateTime(), $ids, $userId);
154
		} else {
155
			return 0;
156
		}
157
	}
158
159
	/**
160
	 * Remove the "starred" status of the given entities
161
	 * @param integer[] $ids
162
	 * @param string $userId
163
	 * @return int number of modified entities
164
	 */
165
	public function unsetStarred($ids, $userId) {
166
		if (\count($ids) > 0) {
167
			return $this->mapper->setStarredDate(NULL, $ids, $userId);
168
		} else {
169
			return 0;
170
		}
171
	}
172
173
	/**
174
	 * Tests if entity with given ID and user ID exists in the database
175
	 * @param int $id
176
	 * @param string $userId
177
	 * @return bool
178
	 */
179
	public function exists($id, $userId) {
180
		return $this->mapper->exists($id, $userId);
181
	}
182
183
	/**
184
	 * Get the number of entities
185
	 * @param string $userId
186
	 */
187
	public function count($userId) {
188
		return $this->mapper->count($userId);
189
	}
190
191
}
192