-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathBasePublicController.php
More file actions
68 lines (62 loc) · 1.57 KB
/
BasePublicController.php
File metadata and controls
68 lines (62 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Controller;
use Closure;
use OCA\Polls\Exceptions\Exception;
use OCA\Polls\Exceptions\NoUpdatesException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
/**
* @psalm-api
*/
class BasePublicController extends Controller {
public function __construct(
string $appName,
IRequest $request,
) {
parent::__construct($appName, $request);
}
/**
* response
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function response(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback());
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}
/**
* response
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseLong(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback());
} catch (NoUpdatesException $e) {
return new JSONResponse([], Http::STATUS_NOT_MODIFIED);
}
}
/**
* responseCreate
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseCreate(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_CREATED);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}
}