Passed
Pull Request — master (#1219)
by René
08:30
created

ResponseHandle::responseDeleteTolerant()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Polls\Controller;
24
25
use Closure;
26
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\DataResponse;
29
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCA\Polls\Exceptions\Exception;
32
33
trait ResponseHandle {
34
35
	/**
36
	 * response
37
	 * @NoAdminRequired
38
	 * @param Closure $callback
39
	 * @return DataResponse
40
	 */
41
	protected function response(Closure $callback) {
42
		try {
43
			return new DataResponse($callback(), Http::STATUS_OK);
44
		} catch (Exception $e) {
45
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
46
		}
47
	}
48
49
	/**
50
	 * responseCreate
51
	 * @NoAdminRequired
52
	 * @param Closure $callback
53
	 * @return DataResponse
54
	 */
55
	protected function responseCreate(Closure $callback) {
56
		try {
57
			return new DataResponse($callback(), Http::STATUS_CREATED);
58
		} catch (Exception $e) {
59
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
60
		}
61
	}
62
63
	/**
64
	 * responseDeleteTolerant
65
	 * @NoAdminRequired
66
	 * @param Closure $callback
67
	 * @return DataResponse
68
	 */
69
	protected function responseDeleteTolerant(Closure $callback) {
70
		try {
71
			return new DataResponse($callback(), Http::STATUS_OK);
72
		} catch (DoesNotExistException $e) {
73
			return new DataResponse(['message' => 'Not found, assume already deleted'], Http::STATUS_OK);
74
		} catch (Exception $e) {
75
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
76
		}
77
	}
78
}
79