Skip to content

Commit 5034dc2

Browse files
committed
refactor: extend all controller from AbstractController
1 parent 28b7f43 commit 5034dc2

15 files changed

+66
-114
lines changed

phpmyfaq/src/phpMyFAQ/Controller/Api/QuestionController.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ class QuestionController extends AbstractController
5757
mediaType: 'application/json',
5858
schema: new OA\Schema(
5959
required: [
60-
'language',
6160
'category-id',
6261
'question',
6362
'author',
6463
'email'
6564
],
6665
properties: [
67-
new OA\Property(property: 'language', type: 'string'),
6866
new OA\Property(property: 'category-id', type: 'integer'),
6967
new OA\Property(property: 'question', type: 'string'),
7068
new OA\Property(property: 'author', type: 'string'),
@@ -73,7 +71,6 @@ class QuestionController extends AbstractController
7371
type: 'object'
7472
),
7573
example: '{
76-
"language": "de",
7774
"category-id": "1",
7875
"question": "Is this the world we created?",
7976
"author": "Freddie Mercury",
@@ -94,11 +91,9 @@ public function create(Request $request): JsonResponse
9491
{
9592
$this->hasValidToken();
9693

97-
$jsonResponse = new JsonResponse();
9894
$configuration = Configuration::getConfigurationInstance();
9995

10096
$data = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);
101-
$languageCode = Filter::filterVar($data->language, FILTER_SANITIZE_SPECIAL_CHARS);
10297
$categoryId = Filter::filterVar($data->{'category-id'}, FILTER_VALIDATE_INT);
10398
$question = Filter::filterVar($data->question, FILTER_SANITIZE_SPECIAL_CHARS);
10499
$author = Filter::filterVar($data->author, FILTER_SANITIZE_SPECIAL_CHARS);
@@ -125,13 +120,9 @@ public function create(Request $request): JsonResponse
125120
try {
126121
$questionHelper->sendSuccessMail($questionData, $categories);
127122
} catch (TransportExceptionInterface | Exception $e) {
128-
$jsonResponse->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
129-
$jsonResponse->setData(['error' => $e->getMessage() ]);
130-
return $jsonResponse;
123+
return $this->json(['error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR);
131124
}
132125

133-
$jsonResponse->setStatusCode(Response::HTTP_CREATED);
134-
$jsonResponse->setData(['stored' => true]);
135-
return $jsonResponse;
126+
return $this->json(['stored' => true], Response::HTTP_CREATED);
136127
}
137128
}

phpmyfaq/src/phpMyFAQ/Controller/Api/RegistrationController.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public function create(Request $request): JsonResponse
102102
{
103103
$this->hasValidToken();
104104

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

108107
$registration = new RegistrationHelper($configuration);
@@ -115,27 +114,22 @@ public function create(Request $request): JsonResponse
115114
$isVisible = $isVisible === 'true';
116115

117116
if (!$registration->isDomainAllowed($email)) {
118-
$jsonResponse->setStatusCode(Response::HTTP_CONFLICT);
119117
$result = [
120118
'registered' => false,
121119
'error' => 'The domain is not allowed.'
122120
];
123-
$jsonResponse->setData($result);
124-
return $jsonResponse;
121+
return $this->json($result, Response::HTTP_CONFLICT);
125122
}
126123

127124
if (!is_null($userName) && !is_null($fullName) && !is_null($email)) {
128125
$result = $registration->createUser($userName, $fullName, $email, $isVisible);
129-
$jsonResponse->setStatusCode(Response::HTTP_CREATED);
126+
return $this->json($result, Response::HTTP_CREATED);
130127
} else {
131-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
132128
$result = [
133129
'registered' => false,
134130
'error' => Translation::get('err_sendMail')
135131
];
132+
return $this->json($result, Response::HTTP_BAD_REQUEST);
136133
}
137-
138-
$jsonResponse->setData($result);
139-
return $jsonResponse;
140134
}
141135
}

phpmyfaq/src/phpMyFAQ/Controller/Api/SearchController.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OpenApi\Attributes as OA;
2222
use phpMyFAQ\Category;
2323
use phpMyFAQ\Configuration;
24+
use phpMyFAQ\Controller\AbstractController;
2425
use phpMyFAQ\Faq\FaqPermission;
2526
use phpMyFAQ\Filter;
2627
use phpMyFAQ\Search;
@@ -31,7 +32,7 @@
3132
use Symfony\Component\HttpFoundation\Request;
3233
use Symfony\Component\HttpFoundation\Response;
3334

34-
class SearchController
35+
class SearchController extends AbstractController
3536
{
3637
/**
3738
* @throws Exception
@@ -70,7 +71,6 @@ class SearchController
7071
)]
7172
public function search(Request $request): JsonResponse
7273
{
73-
$jsonResponse = new JsonResponse();
7474
$faqConfig = Configuration::getConfigurationInstance();
7575
$user = CurrentUser::getCurrentUser($faqConfig);
7676

@@ -94,12 +94,10 @@ public function search(Request $request): JsonResponse
9494
$result[] = $data;
9595
}
9696

97-
$jsonResponse->setData($result);
97+
return $this->json($result, Response::HTTP_OK);
9898
} else {
99-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
99+
return $this->json([], Response::HTTP_NOT_FOUND);
100100
}
101-
102-
return $jsonResponse;
103101
}
104102

105103
#[OA\Get(
@@ -138,17 +136,15 @@ public function search(Request $request): JsonResponse
138136
)]
139137
public function popular(): JsonResponse
140138
{
141-
$jsonResponse = new JsonResponse();
142-
$faqConfig = Configuration::getConfigurationInstance();
139+
$configuration = Configuration::getConfigurationInstance();
143140

144-
$search = new Search($faqConfig);
141+
$search = new Search($configuration);
145142
$result = $search->getMostPopularSearches(7, true);
143+
146144
if ((is_countable($result) ? count($result) : 0) === 0) {
147-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
145+
return $this->json([], Response::HTTP_NOT_FOUND);
146+
} else {
147+
return $this->json($result, Response::HTTP_OK);
148148
}
149-
150-
$jsonResponse->setData($result);
151-
152-
return $jsonResponse;
153149
}
154150
}

phpmyfaq/src/phpMyFAQ/Controller/Api/TagController.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919

2020
use OpenApi\Attributes as OA;
2121
use phpMyFAQ\Configuration;
22+
use phpMyFAQ\Controller\AbstractController;
2223
use phpMyFAQ\Tags;
2324
use Symfony\Component\HttpFoundation\JsonResponse;
2425
use Symfony\Component\HttpFoundation\Response;
2526

26-
class TagController
27+
class TagController extends AbstractController
2728
{
2829
#[OA\Get(
2930
path: '/api/v3.0/tags',
@@ -45,17 +46,14 @@ class TagController
4546
)]
4647
public function list(): JsonResponse
4748
{
48-
$jsonResponse = new JsonResponse();
49-
$faqConfig = Configuration::getConfigurationInstance();
49+
$configuration = Configuration::getConfigurationInstance();
5050

51-
$tags = new Tags($faqConfig);
51+
$tags = new Tags($configuration);
5252
$result = $tags->getPopularTagsAsArray(16);
5353
if ((is_countable($result) ? count($result) : 0) === 0) {
54-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
54+
return $this->json([], Response::HTTP_NOT_FOUND);
55+
} else {
56+
return $this->json($result, Response::HTTP_OK);
5557
}
56-
57-
$jsonResponse->setData($result);
58-
59-
return $jsonResponse;
6058
}
6159
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/AutoCompleteController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use phpMyFAQ\Category;
2121
use phpMyFAQ\Configuration;
22+
use phpMyFAQ\Controller\AbstractController;
2223
use phpMyFAQ\Faq\FaqPermission;
2324
use phpMyFAQ\Filter;
2425
use phpMyFAQ\Helper\SearchHelper;
@@ -31,12 +32,11 @@
3132
use Symfony\Component\HttpFoundation\Response;
3233
use Symfony\Component\Routing\Annotation\Route;
3334

34-
class AutoCompleteController
35+
class AutoCompleteController extends AbstractController
3536
{
3637
#[Route('api/autocomplete')]
3738
public function search(Request $request): JsonResponse
3839
{
39-
$jsonResponse = new JsonResponse();
4040
$faqConfig = Configuration::getConfigurationInstance();
4141

4242
$searchString = Filter::filterVar($request->query->get('search'), FILTER_SANITIZE_SPECIAL_CHARS);
@@ -65,10 +65,10 @@ public function search(Request $request): JsonResponse
6565
$faqSearchHelper->setSearchTerm($searchString);
6666
$faqSearchHelper->setCategory($category);
6767
$faqSearchHelper->setPlurals(new Plurals());
68-
$jsonResponse->setData(Response::HTTP_OK);
69-
$jsonResponse->setData($faqSearchHelper->createAutoCompleteResult($searchResultSet));
68+
69+
return $this->json($faqSearchHelper->createAutoCompleteResult($searchResultSet), Response::HTTP_OK);
7070
}
7171

72-
return $jsonResponse;
72+
return $this->json([], Response::HTTP_NOT_FOUND);
7373
}
7474
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/BookmarkController.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use phpMyFAQ\Bookmark;
2121
use phpMyFAQ\Configuration;
2222
use phpMyFAQ\Controller\AbstractController;
23+
use phpMyFAQ\Core\Exception;
2324
use phpMyFAQ\Filter;
2425
use phpMyFAQ\User\CurrentUser;
2526
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -28,22 +29,22 @@
2829

2930
class BookmarkController extends AbstractController
3031
{
32+
/**
33+
* @throws Exception
34+
*/
3135
#[Route('api/bookmark')]
3236
public function delete(Request $request): JsonResponse
3337
{
3438
$this->userIsAuthenticated();
3539

36-
$jsonResponse = new JsonResponse();
37-
$faqConfig = Configuration::getConfigurationInstance();
40+
$configuration = Configuration::getConfigurationInstance();
3841

3942
$id = Filter::filterVar($request->get('bookmarkId'), FILTER_VALIDATE_INT);
4043

41-
$currentUser = CurrentUser::getCurrentUser($faqConfig);
44+
$currentUser = CurrentUser::getCurrentUser($configuration);
4245

43-
$bookmark = new Bookmark($faqConfig, $currentUser);
46+
$bookmark = new Bookmark($configuration, $currentUser);
4447

45-
$jsonResponse->setData(['success' => $bookmark->remove($id)]);
46-
47-
return $jsonResponse;
48+
return $this->json(['success' => $bookmark->remove($id)], JsonResponse::HTTP_OK);
4849
}
4950
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class CommentController extends AbstractController
99
{
1010
public function create(): JsonResponse
1111
{
12-
return new JsonResponse(['status' => 'ok']);
12+
return $this->json(['status' => 'ok']);
1313
}
1414
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/ContactController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class ContactController extends AbstractController
99
{
1010
public function create(): JsonResponse
1111
{
12-
return new JsonResponse(['status' => 'ok']);
12+
return $this->json(['status' => 'ok']);
1313
}
1414
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/FaqController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class FaqController extends AbstractController
99
{
1010
public function create(): JsonResponse
1111
{
12-
return new JsonResponse(['status' => 'ok']);
12+
return $this->json(['status' => 'ok']);
1313
}
1414
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/QuestionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class QuestionController extends AbstractController
99
{
1010
public function create(): JsonResponse
1111
{
12-
return new JsonResponse(['status' => 'ok']);
12+
return $this->json(['status' => 'ok']);
1313
}
1414
}

0 commit comments

Comments
 (0)