forked from nextcloud/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseController.php
More file actions
52 lines (44 loc) · 1.12 KB
/
BaseController.php
File metadata and controls
52 lines (44 loc) · 1.12 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Controller;
use Closure;
use OCA\Polls\Exceptions\Exception;
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 BaseController extends Controller {
public function __construct(
string $appName,
IRequest $request,
) {
parent::__construct($appName, $request);
}
/**
* response
* @param Closure $callback Callback function
* @param int $successStatus HTTP status code for success
*/
#[NoAdminRequired]
protected function response(
Closure $callback,
int $successStatus = Http::STATUS_OK,
): JSONResponse {
try {
return new JSONResponse($callback(), $successStatus);
} catch (Exception $e) {
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
return new JSONResponse(statusCode: $e->getStatus());
}
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}
}