1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - News |
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\News\Service; |
15
|
|
|
|
16
|
|
|
use \OCP\AppFramework\Db\DoesNotExistException; |
17
|
|
|
use \OCP\AppFramework\Db\MultipleObjectsReturnedException; |
18
|
|
|
|
19
|
|
|
use \OCA\News\Db\NewsMapper; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
abstract class Service { |
23
|
|
|
|
24
|
|
|
protected $mapper; |
25
|
|
|
|
26
|
|
|
public function __construct(NewsMapper $mapper){ |
27
|
|
|
$this->mapper = $mapper; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Delete an entity |
33
|
|
|
* @param int $id the id of the entity |
34
|
|
|
* @param string $userId the name of the user for security reasons |
35
|
|
|
* @throws ServiceNotFoundException if the entity does not exist, or there |
36
|
|
|
* are more than one of it |
37
|
|
|
*/ |
38
|
|
|
public function delete($id, $userId){ |
39
|
|
|
$entity = $this->find($id, $userId); |
40
|
|
|
$this->mapper->delete($entity); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Finds an entity by id |
46
|
|
|
* @param int $id the id of the entity |
47
|
|
|
* @param string $userId the name of the user for security reasons |
48
|
|
|
* @throws ServiceNotFoundException if the entity does not exist, or there |
49
|
|
|
* are more than one of it |
50
|
|
|
* @return \OCP\AppFramework\Db\Entity the entity |
51
|
|
|
*/ |
52
|
|
|
public function find($id, $userId){ |
53
|
|
|
try { |
54
|
|
|
return $this->mapper->find($id, $userId); |
55
|
|
|
} catch(DoesNotExistException $ex){ |
56
|
|
|
throw new ServiceNotFoundException($ex->getMessage()); |
57
|
|
|
} catch(MultipleObjectsReturnedException $ex){ |
58
|
|
|
throw new ServiceNotFoundException($ex->getMessage()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|