NotesApiController::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * ownCloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Controller;
13
14
use OCP\AppFramework\ApiController;
15
use OCP\AppFramework\Http\DataResponse;
16
use OCP\IRequest;
17
18
use OCA\Notes\Service\NotesService;
19
use OCA\Notes\Db\Note;
20
use OCP\IUserSession;
21
22
/**
23
 * Class NotesApiController
24
 *
25
 * @package OCA\Notes\Controller
26
 */
27
class NotesApiController extends ApiController {
28
	use Errors;
29
30
	/** @var NotesService */
31
	private $service;
32
	/** @var IUserSession */
33
	private $userSession;
34
35
	/**
36
	 * @param string $AppName
37
	 * @param IRequest $request
38
	 * @param NotesService $service
39
	 * @param IUserSession $userSession
40
	 */
41 10
	public function __construct($AppName, IRequest $request,
42
								NotesService $service,
43 10
								IUserSession $userSession) {
44 10
		parent::__construct($AppName, $request);
45 10
		$this->service = $service;
46 10
		$this->userSession = $userSession;
47
	}
48
49
	/**
50
	 * @param Note $note
51
	 * @param string[] $exclude the fields that should be removed from the
52
	 * notes
53
	 * @return Note
54
	 */
55 4
	private function excludeFields(Note $note, array $exclude) {
56 4
		if (\count($exclude) > 0) {
57 4
			foreach ($exclude as $field) {
58 4
				if (\property_exists($note, $field)) {
59 2
					unset($note->$field);
60 2
				}
61 4
			}
62 4
		}
63 4
		return $note;
64
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 * @CORS
69
	 * @NoCSRFRequired
70
	 *
71
	 * @param string $exclude
72
	 * @return DataResponse
73
	 */
74
	public function index($exclude='') {
75 2
		$exclude = \explode(',', $exclude);
76 2
		$notes = $this->service->getAll($this->userSession->getUser()->getUID());
77 2
		foreach ($notes as $note) {
78 2
			$note = $this->excludeFields($note, $exclude);
79 2
		}
80 2
		return new DataResponse($notes);
81 2
	}
82
83
	/**
84
	 * @NoAdminRequired
85
	 * @CORS
86
	 * @NoCSRFRequired
87
	 *
88
	 * @param int $id
89
	 * @param string $exclude
90
	 * @return DataResponse
91
	 */
92
	public function get($id, $exclude='') {
93
		$exclude = \explode(',', $exclude);
94 3
95 3
		return $this->respond(function () use ($id, $exclude) {
96
			$note = $this->service->get($id, $this->userSession->getUser()->getUID());
97
			$note = $this->excludeFields($note, $exclude);
98 3
			return $note;
99 2
		});
100 2
	}
101 3
102
	/**
103
	 * @NoAdminRequired
104
	 * @CORS
105
	 * @NoCSRFRequired
106
	 *
107
	 * @param string $content
108
	 * @return DataResponse
109
	 */
110
	public function create($content) {
111
		return $this->respond(function () use ($content) {
112
			$note = $this->service->create($this->userSession->getUser()->getUID());
113 1
			return $this->service->update($note->getId(), $content, $this->userSession->getUser()->getUID());
114
		});
115 1
	}
116 1
117 1
	/**
118
	 * @NoAdminRequired
119
	 * @CORS
120
	 * @NoCSRFRequired
121
	 *
122
	 * @param int $id
123
	 * @param string $content
124
	 * @param boolean $favorite
125
	 * @return DataResponse
126
	 */
127
	public function update($id, $content=null, $favorite=null) {
128
		if ($favorite!==null) {
129
			$this->service->favorite($id, $favorite, $this->userSession->getUser()->getUID());
130
		}
131 2
		return $this->respond(function () use ($id, $content) {
132 2
			if ($content===null) {
133
				return $this->service->get($id, $this->userSession->getUser()->getUID());
134
			} else {
135
				return $this->service->update($id, $content, $this->userSession->getUser()->getUID());
136 2
			}
137
		});
138
	}
139 2
140
	/**
141 2
	 * @NoAdminRequired
142
	 * @CORS
143
	 * @NoCSRFRequired
144
	 *
145
	 * @param int $id
146
	 * @return DataResponse
147
	 */
148
	public function destroy($id) {
149
		return $this->respond(function () use ($id) {
150
			$this->service->delete($id, $this->userSession->getUser()->getUID());
151
			return [];
152
		});
153
	}
154
}
155