Skip to content

Commit a4dc019

Browse files
committed
Optimized attempt
Signed-off-by: dartcafe <github@dartcafe.de>
1 parent 39a0b2e commit a4dc019

15 files changed

Lines changed: 91 additions & 313 deletions

lib/Controller/AdminController.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,4 @@ public function runJanitorJob(): JSONResponse {
9595
public function runNotificationJob(): JSONResponse {
9696
return $this->response(fn () => $this->notificationCron->manuallyRun());
9797
}
98-
99-
/**
100-
* Switch archived status (move to archived polls)
101-
* @param int $pollId poll id
102-
* @deprecated 8.0.0 Not used anymore (use PUT /poll/{pollId}/toggleArchive)
103-
*/
104-
#[FrontpageRoute(verb: 'PUT', url: '/administration/poll/{pollId}/toggleArchive')]
105-
public function toggleArchive(int $pollId): JSONResponse {
106-
return $this->response(fn () => $this->pollService->toggleArchive($pollId));
107-
}
108-
109-
/**
110-
* Delete poll
111-
* @param int $pollId poll id
112-
* @deprecated 8.0.0 Not used anymore (use DELETE /poll/{pollId})
113-
*/
114-
#[FrontpageRoute(verb: 'DELETE', url: '/administration/poll/{pollId}')]
115-
public function delete(int $pollId): JSONResponse {
116-
return $this->responseDeleteTolerant(fn () => $this->pollService->delete($pollId));
117-
}
11898
}

lib/Controller/BaseApiV1Controller.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

lib/Controller/BaseApiV2Controller.php

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use Closure;
1212
use OCA\Polls\Exceptions\Exception;
13-
use OCA\Polls\Exceptions\NoUpdatesException;
1413
use OCP\AppFramework\Db\DoesNotExistException;
1514
use OCP\AppFramework\Http;
1615
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -23,16 +22,14 @@
2322
/**
2423
* @psalm-api
2524
*/
26-
class BaseApiV2Controller extends OCSController
27-
{
25+
class BaseApiV2Controller extends OCSController {
2826
public function __construct(
29-
string $appName,
27+
string $appName,
3028
IRequest $request,
31-
string $corsMethods = 'PUT, POST, GET, DELETE',
32-
string $corsAllowedHeaders = 'Authorization, Content-Type, Accept',
33-
int $corsMaxAge = 1728000,
34-
)
35-
{
29+
string $corsMethods = 'PUT, POST, GET, DELETE',
30+
string $corsAllowedHeaders = 'Authorization, Content-Type, Accept',
31+
int $corsMaxAge = 1728000,
32+
) {
3633
parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
3734
}
3835

@@ -41,43 +38,19 @@ public function __construct(
4138
* @param Closure $callback Callback function
4239
*/
4340
#[NoAdminRequired]
44-
protected function response(Closure $callback): DataResponse
45-
{
46-
return $this->handleResponse($callback, Http::STATUS_OK);
47-
}
48-
49-
/**
50-
* response
51-
* @param Closure $callback Callback function
52-
*/
53-
#[NoAdminRequired]
54-
protected function responseLong(Closure $callback): DataResponse
55-
{
56-
return $this->handleResponse($callback, Http::STATUS_OK);
57-
}
58-
59-
/**
60-
* responseCreate
61-
* @param Closure $callback Callback function
62-
*/
63-
#[NoAdminRequired]
64-
protected function responseCreate(Closure $callback): DataResponse
65-
{
66-
return $this->handleResponse($callback, Http::STATUS_CREATED);
67-
}
68-
69-
private function handleResponse(
70-
Closure $callback,
71-
int $successStatus = Http::STATUS_OK
72-
): DataResponse
73-
{
41+
protected function response(Closure $callback, int $successStatus = Http::STATUS_OK): DataResponse {
7442
try {
7543
return new DataResponse($callback(), $successStatus);
76-
} catch (NoUpdatesException $e) {
77-
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
44+
7845
} catch (DoesNotExistException $e) {
7946
throw new OCSNotFoundException($e->getMessage());
47+
8048
} catch (Exception $e) {
49+
50+
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
51+
return new DataResponse(statusCode: $e->getStatus());
52+
}
53+
8154
throw new OCSBadRequestException($e->getMessage());
8255
}
8356
}

lib/Controller/BaseController.php

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
use Closure;
1212
use OCA\Polls\Exceptions\Exception;
13-
use OCA\Polls\Exceptions\NoUpdatesException;
1413
use OCP\AppFramework\Controller;
15-
use OCP\AppFramework\Db\DoesNotExistException;
1614
use OCP\AppFramework\Http;
1715
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1816
use OCP\AppFramework\Http\JSONResponse;
@@ -32,77 +30,23 @@ public function __construct(
3230
/**
3331
* response
3432
* @param Closure $callback Callback function
33+
* @param int $successStatus HTTP status code for success
3534
*/
3635
#[NoAdminRequired]
37-
protected function response(Closure $callback): JSONResponse {
38-
return $this->handleResponse($callback, Http::STATUS_OK, Exception::class);
39-
}
40-
41-
/**
42-
* response
43-
* @param Closure $callback Callback function
44-
*/
45-
#[NoAdminRequired]
46-
protected function responseLong(Closure $callback): JSONResponse {
47-
return $this->handleResponse($callback, Http::STATUS_OK, NoUpdatesException::class, Http::STATUS_NOT_MODIFIED);
48-
}
49-
50-
/**
51-
* responseCreate
52-
* @param Closure $callback Callback function
53-
*/
54-
#[NoAdminRequired]
55-
protected function responseCreate(Closure $callback): JSONResponse {
56-
return $this->handleResponse($callback, Http::STATUS_CREATED, Exception::class);
57-
}
58-
59-
/**
60-
* responseDeleteTolerant
61-
* @param Closure $callback Callback function
62-
*/
63-
#[NoAdminRequired]
64-
protected function responseDeleteTolerant(Closure $callback): JSONResponse {
65-
return $this->handleResponse(
66-
$callback,
67-
Http::STATUS_OK,
68-
DoesNotExistException::class,
69-
Http::STATUS_OK,
70-
'Not found, assume already deleted',
71-
Exception::class
72-
);
73-
}
74-
75-
private function handleResponse(
36+
protected function response(
7637
Closure $callback,
77-
int $successStatus,
78-
string $primaryException,
79-
int $fallbackStatus = null,
80-
string $fallbackMessage = null,
81-
string $secondaryException = null
38+
int $successStatus = Http::STATUS_OK,
8239
): JSONResponse {
8340
try {
8441
return new JSONResponse($callback(), $successStatus);
85-
} catch (\Exception $e) {
86-
if (is_a($e, $primaryException, true)) {
87-
if ($fallbackStatus !== null) {
88-
return new JSONResponse(['message' => $fallbackMessage ?? ''], $fallbackStatus);
89-
}
90-
if ($e instanceof Exception) {
91-
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
92-
}
93-
}
42+
} catch (Exception $e) {
9443

95-
if (
96-
$secondaryException !== null &&
97-
is_a($e, $secondaryException, true) &&
98-
$e instanceof Exception
99-
) {
100-
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
44+
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
45+
return new JSONResponse(statusCode: $e->getStatus());
10146
}
10247

103-
throw $e;
48+
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
10449
}
10550
}
10651

10752
}
108-

lib/Controller/BasePublicController.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)