Skip to content

Commit 32e50b2

Browse files
committed
refactor: extend all controller from AbstractController
1 parent 5034dc2 commit 32e50b2

File tree

9 files changed

+91
-142
lines changed

9 files changed

+91
-142
lines changed

phpmyfaq/src/phpMyFAQ/Controller/Administration/AttachmentController.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,24 @@ public function delete(Request $request): JsonResponse
4141
{
4242
$this->userHasPermission(PermissionType::ATTACHMENT_DELETE);
4343

44-
$jsonResponse = new JsonResponse();
4544
$deleteData = json_decode($request->getContent());
4645
try {
4746
if (!Token::getInstance()->verifyToken('delete-attachment', $deleteData->csrf)) {
48-
$jsonResponse->setStatusCode(Response::HTTP_UNAUTHORIZED);
49-
$jsonResponse->setData(['error' => Translation::get('err_NotAuth')]);
50-
$jsonResponse->send();
51-
exit();
47+
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
5248
}
5349

5450
$attachment = AttachmentFactory::create($deleteData->attId);
5551
if ($attachment->delete()) {
56-
$jsonResponse->setStatusCode(Response::HTTP_OK);
5752
$result = ['success' => Translation::get('msgAttachmentsDeleted')];
53+
return $this->json($result, Response::HTTP_OK);
5854
} else {
59-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
6055
$result = ['error' => Translation::get('ad_att_delfail')];
56+
return $this->json($result, Response::HTTP_BAD_REQUEST);
6157
}
6258
} catch (AttachmentException $attachmentException) {
63-
$jsonResponse->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
6459
$result = ['error' => $attachmentException->getMessage()];
60+
return $this->json($result, Response::HTTP_INTERNAL_SERVER_ERROR);
6561
}
66-
67-
$jsonResponse->setData($result);
68-
69-
return $jsonResponse;
7062
}
7163

7264
/**
@@ -79,11 +71,8 @@ public function upload(Request $request): JsonResponse
7971
{
8072
$this->userHasPermission(PermissionType::ATTACHMENT_ADD);
8173

82-
$jsonResponse = new JsonResponse();
83-
8474
if (!isset($_FILES['filesToUpload'])) {
85-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
86-
return $jsonResponse;
75+
return $this->json([], Response::HTTP_BAD_REQUEST);
8776
}
8877

8978
$files = AttachmentFactory::rearrangeUploadedFiles($_FILES['filesToUpload']);
@@ -113,15 +102,10 @@ public function upload(Request $request): JsonResponse
113102
'faqLanguage' => $request->request->get('record_lang')
114103
];
115104
} else {
116-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
117-
$jsonResponse->setData('The image is too large.');
118-
return $jsonResponse;
105+
return $this->json('The image is too large.', Response::HTTP_BAD_REQUEST);
119106
}
120107
}
121108

122-
$jsonResponse->setStatusCode(Response::HTTP_OK);
123-
$jsonResponse->setData($uploadedFiles);
124-
125-
return $jsonResponse;
109+
return $this->json($uploadedFiles, Response::HTTP_OK);
126110
}
127111
}

phpmyfaq/src/phpMyFAQ/Controller/Administration/CategoryController.php

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use phpMyFAQ\Category\CategoryRelation;
2525
use phpMyFAQ\Configuration;
2626
use phpMyFAQ\Controller\AbstractController;
27+
use phpMyFAQ\Core\Exception;
2728
use phpMyFAQ\Enums\PermissionType;
2829
use phpMyFAQ\Session\Token;
2930
use phpMyFAQ\Translation;
@@ -35,6 +36,9 @@
3536

3637
class CategoryController extends AbstractController
3738
{
39+
/**
40+
* @throws Exception
41+
*/
3842
#[Route('admin/api/category/delete')]
3943
public function delete(Request $request): JsonResponse
4044
{
@@ -47,9 +51,7 @@ public function delete(Request $request): JsonResponse
4751
$data = json_decode($request->getContent());
4852

4953
if (!Token::getInstance()->verifyToken('category', $data->csrfToken)) {
50-
$jsonResponse->setStatusCode(Response::HTTP_UNAUTHORIZED);
51-
$jsonResponse->setData(['error' => Translation::get('err_NotAuth')]);
52-
return $jsonResponse;
54+
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
5355
}
5456

5557
[ $currentAdminUser, $currentAdminGroups ] = CurrentUser::getCurrentUserGroupId($currentUser);
@@ -83,26 +85,23 @@ public function delete(Request $request): JsonResponse
8385
$category->delete($data->categoryId, $data->language) &&
8486
$categoryRelation->delete($data->categoryId, $data->language)
8587
) {
86-
$jsonResponse->setStatusCode(Response::HTTP_OK);
87-
$jsonResponse->setData(
88-
['success' => Translation::get('ad_categ_deleted')]
89-
);
88+
return $this->json(['success' => Translation::get('ad_categ_deleted')], Response::HTTP_OK);
9089
} else {
91-
$jsonResponse->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
92-
$jsonResponse->setData(
93-
['error' => Translation::get('ad_adus_dberr') . $configuration->getDb()->error()]
90+
return $this->json(
91+
['error' => Translation::get('ad_adus_dberr') . $configuration->getDb()->error()],
92+
Response::HTTP_INTERNAL_SERVER_ERROR
9493
);
9594
}
96-
97-
return $jsonResponse;
9895
}
9996

97+
/**
98+
* @throws Exception
99+
*/
100100
#[Route('admin/api/category/permissions')]
101101
public function permissions(Request $request): JsonResponse
102102
{
103103
$this->userIsAuthenticated();
104104

105-
$jsonResponse = new JsonResponse();
106105
$categoryPermission = new CategoryPermission(Configuration::getConfigurationInstance());
107106

108107
$categoryData = $request->get('categories');
@@ -113,42 +112,43 @@ public function permissions(Request $request): JsonResponse
113112
$categories = explode(',', (string) $categoryData);
114113
}
115114

116-
$jsonResponse->setData(
115+
return $this->json(
117116
[
118117
'user' => $categoryPermission->get(CategoryPermission::USER, $categories),
119118
'group' => $categoryPermission->get(CategoryPermission::GROUP, $categories)
120-
]
119+
],
120+
Response::HTTP_OK
121121
);
122-
123-
return $jsonResponse;
124122
}
125123

124+
/**
125+
* @throws Exception
126+
*/
126127
#[Route('admin/api/category/translations')]
127128
public function translations(Request $request): JsonResponse
128129
{
129130
$this->userIsAuthenticated();
130131

131-
$jsonResponse = new JsonResponse();
132132
$configuration = Configuration::getConfigurationInstance();
133133
$category = new Category($configuration, [], false);
134134

135135
$translations = $category->getCategoryLanguagesTranslated($request->get('categoryId'));
136136

137-
return $jsonResponse->setData($translations);
137+
return $this->json($translations, Response::HTTP_OK);
138138
}
139139

140+
/**
141+
* @throws Exception
142+
*/
140143
#[Route('admin/api/category/update-order')]
141144
public function updateOrder(Request $request): JsonResponse
142145
{
143146
$this->userHasPermission(PermissionType::CATEGORY_EDIT);
144147

145-
$jsonResponse = new JsonResponse();
146148
$data = json_decode($request->getContent());
147149

148150
if (!Token::getInstance()->verifyToken('category', $data->csrfToken)) {
149-
$jsonResponse->setStatusCode(Response::HTTP_UNAUTHORIZED);
150-
$jsonResponse->setData(['error' => Translation::get('err_NotAuth')]);
151-
return $jsonResponse;
151+
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
152152
}
153153

154154
$configuration = Configuration::getConfigurationInstance();
@@ -166,9 +166,6 @@ public function updateOrder(Request $request): JsonResponse
166166
$category->setGroups($currentAdminGroups);
167167
$category->updateParentCategory($data->categoryId, $parentId);
168168

169-
$jsonResponse->setData(
170-
['success' => Translation::get('ad_categ_save_order')]
171-
);
172-
return $jsonResponse;
169+
return $this->json(['success' => Translation::get('ad_categ_save_order')], Response::HTTP_OK);
173170
}
174171
}

phpmyfaq/src/phpMyFAQ/Controller/Administration/CommentController.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use phpMyFAQ\Comments;
2121
use phpMyFAQ\Configuration;
2222
use phpMyFAQ\Controller\AbstractController;
23+
use phpMyFAQ\Core\Exception;
2324
use phpMyFAQ\Enums\PermissionType;
2425
use phpMyFAQ\Session\Token;
2526
use phpMyFAQ\Translation;
@@ -30,18 +31,18 @@
3031

3132
class CommentController extends AbstractController
3233
{
34+
/**
35+
* @throws Exception
36+
*/
3337
#[Route('admin/api/content/comments')]
3438
public function delete(Request $request): JsonResponse
3539
{
3640
$this->userHasPermission(PermissionType::COMMENT_DELETE);
3741

38-
$jsonResponse = new JsonResponse();
3942
$data = json_decode($request->getContent());
4043

4144
if (!Token::getInstance()->verifyToken('delete-comment', $data->data->{'pmf-csrf-token'})) {
42-
$jsonResponse->setStatusCode(Response::HTTP_UNAUTHORIZED);
43-
$jsonResponse->setData(['error' => Translation::get('err_NotAuth')]);
44-
$jsonResponse->send();
45+
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
4546
}
4647

4748
$comments = new Comments(Configuration::getConfigurationInstance());
@@ -57,13 +58,9 @@ public function delete(Request $request): JsonResponse
5758
$result = $comments->delete($data->type, $commentId);
5859
}
5960

60-
$jsonResponse->setStatusCode(Response::HTTP_OK);
61-
$jsonResponse->setData(['success' => $result]);
61+
return $this->json(['success' => $result], Response::HTTP_OK);
6262
} else {
63-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
64-
$jsonResponse->setData(['error' => false]);
63+
return $this->json(['error' => false], Response::HTTP_BAD_REQUEST);
6564
}
66-
67-
return $jsonResponse;
6865
}
6966
}

phpmyfaq/src/phpMyFAQ/Controller/Administration/ConfigurationController.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@
3232

3333
class ConfigurationController extends AbstractController
3434
{
35+
/**
36+
* @throws Exception
37+
*/
3538
#[Route('admin/api/configuration/send-test-mail')]
3639
public function sendTestMail(Request $request): JsonResponse
3740
{
3841
$this->userHasPermission(PermissionType::CONFIGURATION_EDIT);
3942

40-
$jsonResponse = new JsonResponse();
4143
$configuration = Configuration::getConfigurationInstance();
4244

4345
$data = json_decode($request->getContent());
4446

4547
if (!Token::getInstance()->verifyToken('configuration', $data->csrf)) {
46-
$jsonResponse->setStatusCode(Response::HTTP_UNAUTHORIZED);
47-
$jsonResponse->setData(['error' => Translation::get('err_NotAuth')]);
48-
return $jsonResponse;
48+
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
4949
}
5050

5151
try {
@@ -56,13 +56,9 @@ public function sendTestMail(Request $request): JsonResponse
5656
$mail->message = 'It works on my machine. 🚀';
5757
$result = $mail->send();
5858

59-
$jsonResponse->setStatusCode(Response::HTTP_OK);
60-
$jsonResponse->setData(['success' => $result]);
59+
return $this->json(['success' => $result], Response::HTTP_OK);
6160
} catch (Exception | TransportExceptionInterface $e) {
62-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
63-
$jsonResponse->setData(['error' => $e->getMessage()]);
61+
return $this->json(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
6462
}
65-
66-
return $jsonResponse;
6763
}
6864
}

phpmyfaq/src/phpMyFAQ/Controller/Administration/ConfigurationTabController.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public function save(Request $request): JsonResponse
8484
$oldConfigurationData = $configuration->getAll();
8585

8686
if (!Token::getInstance()->verifyToken('configuration', $csrfToken)) {
87-
$jsonResponse->setStatusCode(Response::HTTP_UNAUTHORIZED);
88-
$jsonResponse->setData(['error' => Translation::get('err_NotAuth')]);
87+
return $this->json(['error' => Translation::get('err_NotAuth')], Response::HTTP_UNAUTHORIZED);
8988
} else {
9089
// Set the new values
9190
$forbiddenValues = ['{', '}'];
@@ -143,11 +142,8 @@ public function save(Request $request): JsonResponse
143142

144143
$configuration->update($newConfigValues);
145144

146-
$jsonResponse->setStatusCode(Response::HTTP_OK);
147-
$jsonResponse->setData(['success' => Translation::get('ad_config_saved')]);
145+
return $this->json(['success' => Translation::get('ad_config_saved')], Response::HTTP_OK);
148146
}
149-
150-
return $jsonResponse;
151147
}
152148

153149
/**

phpmyfaq/src/phpMyFAQ/Controller/Administration/DashboardController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
class DashboardController extends AbstractController
3333
{
34+
/**
35+
* @throws Exception
36+
*/
3437
#[Route('admin/api/dashboard/versions')]
3538
public function versions(): JsonResponse
3639
{
@@ -69,6 +72,9 @@ public function visits(): JsonResponse
6972
return $this->json(['error' => 'User tracking is disabled.'], 400);
7073
}
7174

75+
/**
76+
* @throws Exception
77+
*/
7278
#[Route('admin/api/dashboard/topten')]
7379
public function topTen(): JsonResponse
7480
{

0 commit comments

Comments
 (0)