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
|
|
|
* @copyright Alessandro Cosentino 2012 |
11
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OCA\Music\AppFramework\BusinessLayer; |
15
|
|
|
|
16
|
|
|
use \OCA\Music\Db\BaseMapper; |
17
|
|
|
|
18
|
|
|
use \OCP\AppFramework\Db\DoesNotExistException; |
19
|
|
|
use \OCP\AppFramework\Db\MultipleObjectsReturnedException; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
abstract class BusinessLayer { |
23
|
|
|
|
24
|
|
|
protected $mapper; |
25
|
|
|
|
26
|
|
|
public function __construct(BaseMapper $mapper){ |
27
|
|
|
$this->mapper = $mapper; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Delete an entity |
32
|
|
|
* @param int $id the id of the entity |
33
|
|
|
* @param string $userId the name of the user for security reasons |
34
|
|
|
* @throws DoesNotExistException if the entity does not exist |
35
|
|
|
* @throws MultipleObjectsReturnedException if more than one entity exists |
36
|
|
|
*/ |
37
|
|
|
public function delete($id, $userId){ |
38
|
|
|
$entity = $this->find($id, $userId); |
39
|
|
|
$this->mapper->delete($entity); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Deletes entities without specifying the owning user. |
44
|
|
|
* This should never be called directly from the HTML API, but only in case |
45
|
|
|
* we can actually trust the passed IDs (e.g. file deleted hook). |
46
|
|
|
* @param array $ids the ids of the entities which should be deleted |
47
|
|
|
*/ |
48
|
|
|
public function deleteById($ids){ |
49
|
|
|
$this->mapper->deleteById($ids); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Finds an entity by id |
54
|
|
|
* @param int $id the id of the entity |
55
|
|
|
* @param string $userId the name of the user for security reasons |
56
|
|
|
* @throws DoesNotExistException if the entity does not exist |
57
|
|
|
* @throws MultipleObjectsReturnedException if more than one entity exists |
58
|
|
|
* @return Entity the entity |
59
|
|
|
*/ |
60
|
|
|
public function find($id, $userId){ |
61
|
|
|
try { |
62
|
|
|
return $this->mapper->find($id, $userId); |
63
|
|
|
} catch(DoesNotExistException $ex){ |
64
|
|
|
throw new BusinessLayerException($ex->getMessage()); |
65
|
|
|
} catch(MultipleObjectsReturnedException $ex){ |
66
|
|
|
throw new BusinessLayerException($ex->getMessage()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Finds all entities |
72
|
|
|
* @param string $userId the name of the user for security reasons |
73
|
|
|
* @return array the entities |
74
|
|
|
*/ |
75
|
|
|
public function findAll($userId){ |
76
|
|
|
return $this->mapper->findAll($userId); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the number of entities |
81
|
|
|
* @param string $userId |
82
|
|
|
*/ |
83
|
|
|
public function count($userId){ |
84
|
|
|
return $this->mapper->count($userId); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|