Skip to content

Commit 3dcde00

Browse files
committed
refactor: extend all controller from AbstractController
1 parent e4caee0 commit 3dcde00

File tree

9 files changed

+56
-126
lines changed

9 files changed

+56
-126
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
use phpMyFAQ\Attachment\AttachmentException;
2222
use phpMyFAQ\Attachment\AttachmentFactory;
2323
use phpMyFAQ\Configuration;
24+
use phpMyFAQ\Controller\AbstractController;
2425
use phpMyFAQ\Filter;
2526
use Symfony\Component\HttpFoundation\JsonResponse;
2627
use Symfony\Component\HttpFoundation\Request;
2728
use Symfony\Component\HttpFoundation\Response;
2829

29-
class AttachmentController
30+
class AttachmentController extends AbstractController
3031
{
3132
#[OA\Get(
3233
path: '/api/v3.0/attachments/{faqId}',
@@ -74,6 +75,7 @@ public function list(Request $request): JsonResponse
7475
$recordId = Filter::filterVar($request->get('recordId'), FILTER_VALIDATE_INT);
7576
$attachments = [];
7677
$result = [];
78+
7779
try {
7880
$attachments = AttachmentFactory::fetchByRecordId($faqConfig, $recordId);
7981
} catch (AttachmentException) {
@@ -88,11 +90,9 @@ public function list(Request $request): JsonResponse
8890
}
8991

9092
if ($result === []) {
91-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
93+
return $this->json($result, Response::HTTP_NOT_FOUND);
9294
}
9395

94-
$jsonResponse->setData($result);
95-
96-
return $jsonResponse;
96+
return $this->json($result, Response::HTTP_OK);
9797
}
9898
}

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ class CategoryController extends AbstractController
7171
)]
7272
public function list(): JsonResponse
7373
{
74-
$jsonResponse = new JsonResponse();
7574
$faqConfig = Configuration::getConfigurationInstance();
7675
$language = new Language($faqConfig);
7776
$currentLanguage = $language->setLanguageByAcceptLanguage();
@@ -87,14 +86,10 @@ public function list(): JsonResponse
8786
$result = array_values($category->getAllCategories());
8887

8988
if ($result === []) {
90-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
89+
return $this->json($result, Response::HTTP_NOT_FOUND);
9190
} else {
92-
$jsonResponse->setStatusCode(Response::HTTP_OK);
91+
return $this->json($result, Response::HTTP_OK);
9392
}
94-
95-
$jsonResponse->setData($result);
96-
97-
return $jsonResponse;
9893
}
9994

10095
/**
@@ -186,7 +181,6 @@ public function create(Request $request): JsonResponse
186181
{
187182
$this->hasValidToken();
188183

189-
$jsonResponse = new JsonResponse();
190184
$configuration = Configuration::getConfigurationInstance();
191185
$user = CurrentUser::getCurrentUser($configuration);
192186

@@ -222,13 +216,11 @@ public function create(Request $request): JsonResponse
222216
if (!is_null($parentCategoryName)) {
223217
$parentCategoryIdFound = $category->getCategoryIdFromName($parentCategoryName);
224218
if ($parentCategoryIdFound === false) {
225-
$jsonResponse->setStatusCode(Response::HTTP_CONFLICT);
226219
$result = [
227220
'stored' => false,
228221
'error' => 'The given parent category name was not found.'
229222
];
230-
$jsonResponse->setData($result);
231-
return $jsonResponse;
223+
return $this->json($result, Response::HTTP_CONFLICT);
232224
}
233225

234226
$parentId = $parentCategoryIdFound;
@@ -256,20 +248,16 @@ public function create(Request $request): JsonResponse
256248
$categoryPermission->add(CategoryPermission::USER, [$categoryId], [-1]);
257249
$categoryPermission->add(CategoryPermission::GROUP, [$categoryId], [-1]);
258250

259-
$jsonResponse->setStatusCode(Response::HTTP_CREATED);
260251
$result = [
261252
'stored' => true
262253
];
254+
return $this->json($result, Response::HTTP_CREATED);
263255
} else {
264-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
265256
$result = [
266257
'stored' => false,
267258
'error' => 'Cannot add category'
268259
];
260+
return $this->json($result, Response::HTTP_BAD_REQUEST);
269261
}
270-
271-
$jsonResponse->setData($result);
272-
273-
return $jsonResponse;
274262
}
275263
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
use OpenApi\Attributes as OA;
2121
use phpMyFAQ\Comments;
2222
use phpMyFAQ\Configuration;
23+
use phpMyFAQ\Controller\AbstractController;
2324
use phpMyFAQ\Entity\CommentType;
2425
use phpMyFAQ\Filter;
2526
use Symfony\Component\HttpFoundation\JsonResponse;
2627
use Symfony\Component\HttpFoundation\Request;
2728
use Symfony\Component\HttpFoundation\Response;
2829

29-
class CommentController
30+
class CommentController extends AbstractController
3031
{
3132
#[OA\Get(
3233
path: '/api/v3.0/comments/{faqId}',
@@ -71,19 +72,16 @@ class CommentController
7172
)]
7273
public function list(Request $request): JsonResponse
7374
{
74-
$jsonResponse = new JsonResponse();
7575
$faqConfig = Configuration::getConfigurationInstance();
7676

7777
$recordId = Filter::filterVar($request->get('recordId'), FILTER_VALIDATE_INT);
7878

7979
$comments = new Comments($faqConfig);
8080
$result = $comments->getCommentsData($recordId, CommentType::FAQ);
8181
if ((is_countable($result) ? count($result) : 0) === 0) {
82-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
82+
$this->json($result, Response::HTTP_NOT_FOUND);
8383
}
8484

85-
$jsonResponse->setData($result);
86-
87-
return $jsonResponse;
85+
return $this->json($result, Response::HTTP_OK);
8886
}
8987
}

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

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class FaqController extends AbstractController
8181
)]
8282
public function getByCategoryId(Request $request): JsonResponse
8383
{
84-
$jsonResponse = new JsonResponse();
8584
$configuration = Configuration::getConfigurationInstance();
8685
$user = CurrentUser::getCurrentUser($configuration);
8786

@@ -95,15 +94,10 @@ public function getByCategoryId(Request $request): JsonResponse
9594

9695
try {
9796
$result = $faq->getAllAvailableFaqsByCategoryId($categoryId);
98-
$jsonResponse->setStatusCode(Response::HTTP_OK);
99-
$jsonResponse->setData($result);
97+
return $this->json($result, Response::HTTP_OK);
10098
} catch (Exception $e) {
101-
$jsonResponse->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
102-
$jsonResponse->setData(['error' => $e->getMessage()]);
103-
return $jsonResponse;
99+
return $this->json(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
104100
}
105-
106-
return $jsonResponse;
107101
}
108102

109103
/**
@@ -164,7 +158,6 @@ public function getByCategoryId(Request $request): JsonResponse
164158
)]
165159
public function getById(Request $request): JsonResponse
166160
{
167-
$jsonResponse = new JsonResponse();
168161
$configuration = Configuration::getConfigurationInstance();
169162
$user = CurrentUser::getCurrentUser($configuration);
170163

@@ -181,13 +174,10 @@ public function getById(Request $request): JsonResponse
181174

182175
if ((is_countable($result) ? count($result) : 0) === 0 || $result['solution_id'] === 42) {
183176
$result = new stdClass();
184-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
177+
return $this->json($result, Response::HTTP_NOT_FOUND);
185178
} else {
186-
$jsonResponse->setStatusCode(Response::HTTP_OK);
179+
return $this->json($result, Response::HTTP_OK);
187180
}
188-
189-
$jsonResponse->setData($result);
190-
return $jsonResponse;
191181
}
192182

193183
/**
@@ -236,7 +226,6 @@ public function getById(Request $request): JsonResponse
236226
)]
237227
public function getByTagId(Request $request): JsonResponse
238228
{
239-
$jsonResponse = new JsonResponse();
240229
$configuration = Configuration::getConfigurationInstance();
241230
$user = CurrentUser::getCurrentUser($configuration);
242231

@@ -253,15 +242,10 @@ public function getByTagId(Request $request): JsonResponse
253242

254243
try {
255244
$result = $faq->getRecordsByIds($recordIds);
256-
$jsonResponse->setStatusCode(Response::HTTP_OK);
257-
$jsonResponse->setData($result);
245+
return $this->json($result, Response::HTTP_OK);
258246
} catch (Exception $e) {
259-
$jsonResponse->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
260-
$jsonResponse->setData(['error' => $e->getMessage()]);
261-
return $jsonResponse;
247+
return $this->json(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
262248
}
263-
264-
return $jsonResponse;
265249
}
266250

267251
/**
@@ -298,7 +282,6 @@ public function getByTagId(Request $request): JsonResponse
298282
)]
299283
public function getPopular(): JsonResponse
300284
{
301-
$jsonResponse = new JsonResponse();
302285
$configuration = Configuration::getConfigurationInstance();
303286
$user = CurrentUser::getCurrentUser($configuration);
304287

@@ -311,13 +294,10 @@ public function getPopular(): JsonResponse
311294
$result = array_values($faq->getTopTenData());
312295

313296
if ((is_countable($result) ? count($result) : 0) === 0) {
314-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
315-
return $jsonResponse;
297+
$this->json($result, Response::HTTP_NOT_FOUND);
316298
}
317299

318-
$jsonResponse->setStatusCode(Response::HTTP_OK);
319-
$jsonResponse->setData($result);
320-
return $jsonResponse;
300+
return $this->json($result, Response::HTTP_OK);
321301
}
322302

323303
/**
@@ -367,13 +347,10 @@ public function getLatest(): JsonResponse
367347
$result = array_values($faq->getLatestData());
368348

369349
if ((is_countable($result) ? count($result) : 0) === 0) {
370-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
371-
return $jsonResponse;
350+
return $this->json($result, Response::HTTP_NOT_FOUND);
372351
}
373352

374-
$jsonResponse->setStatusCode(Response::HTTP_OK);
375-
$jsonResponse->setData($result);
376-
return $jsonResponse;
353+
return $this->json($result, Response::HTTP_OK);
377354
}
378355

379356
/**
@@ -415,7 +392,6 @@ public function getLatest(): JsonResponse
415392
)]
416393
public function getSticky(): JsonResponse
417394
{
418-
$jsonResponse = new JsonResponse();
419395
$configuration = Configuration::getConfigurationInstance();
420396
$user = CurrentUser::getCurrentUser($configuration);
421397

@@ -428,13 +404,10 @@ public function getSticky(): JsonResponse
428404
$result = array_values($faq->getStickyRecordsData());
429405

430406
if ((is_countable($result) ? count($result) : 0) === 0) {
431-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
432-
return $jsonResponse;
407+
return $this->json($result, Response::HTTP_NOT_FOUND);
433408
}
434409

435-
$jsonResponse->setStatusCode(Response::HTTP_OK);
436-
$jsonResponse->setData($result);
437-
return $jsonResponse;
410+
return $this->json($result, Response::HTTP_OK);
438411
}
439412

440413
/**
@@ -496,13 +469,10 @@ public function list(): JsonResponse
496469
$result = $faq->faqRecords;
497470

498471
if ((is_countable($result) ? count($result) : 0) === 0) {
499-
$jsonResponse->setStatusCode(Response::HTTP_NOT_FOUND);
500-
return $jsonResponse;
472+
return $this->json($result, Response::HTTP_NOT_FOUND);
501473
}
502474

503-
$jsonResponse->setStatusCode(Response::HTTP_OK);
504-
$jsonResponse->setData($result);
505-
return $jsonResponse;
475+
return $this->json($result, Response::HTTP_OK);
506476
}
507477

508478
/**
@@ -639,26 +609,22 @@ public function create(Request $request): JsonResponse
639609
if (!is_null($categoryName)) {
640610
$categoryIdFound = $category->getCategoryIdFromName($categoryName);
641611
if ($categoryIdFound === false) {
642-
$jsonResponse->setStatusCode(Response::HTTP_CONFLICT);
643612
$result = [
644613
'stored' => false,
645614
'error' => 'The given category name was not found.'
646615
];
647-
$jsonResponse->setData($result);
648-
return $jsonResponse;
616+
return $this->json($result, Response::HTTP_CONFLICT);
649617
}
650618

651619
$categoryId = $categoryIdFound;
652620
}
653621

654622
if ($faq->hasTitleAHash($question)) {
655-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
656623
$result = [
657624
'stored' => false,
658625
'error' => 'It is not allowed, that the question title contains a hash.'
659626
];
660-
$jsonResponse->setData($result);
661-
return $jsonResponse;
627+
return $this->json($result, Response::HTTP_BAD_REQUEST);
662628
}
663629

664630
$categories = [ $categoryId ];
@@ -687,9 +653,7 @@ public function create(Request $request): JsonResponse
687653
->setCategories($categories)
688654
->save();
689655

690-
$jsonResponse->setStatusCode(Response::HTTP_CREATED);
691-
$jsonResponse->setData(['stored' => true]);
692-
return $jsonResponse;
656+
return $this->json(['stored' => true], Response::HTTP_CREATED);
693657
}
694658

695659
/**
@@ -699,8 +663,8 @@ public function create(Request $request): JsonResponse
699663
#[OA\Put(
700664
path: '/api/v3.0/faq/update',
701665
operationId: 'updateFaq',
702-
tags: ['Endpoints with Authentication'],
703-
description: 'Used to update a FAQ in one existing category.'
666+
description: 'Used to update a FAQ in one existing category.',
667+
tags: ['Endpoints with Authentication']
704668
)]
705669
#[OA\Header(
706670
header: 'Accept-Language',
@@ -778,7 +742,6 @@ public function update(Request $request): JsonResponse
778742
{
779743
$this->hasValidToken();
780744

781-
$jsonResponse = new JsonResponse();
782745
$configuration = Configuration::getConfigurationInstance();
783746
$user = CurrentUser::getCurrentUser($configuration);
784747

@@ -808,13 +771,11 @@ public function update(Request $request): JsonResponse
808771
$isSticky = Filter::filterVar($data->{'is-sticky'}, FILTER_VALIDATE_BOOLEAN);
809772

810773
if ($faq->hasTitleAHash($question)) {
811-
$jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST);
812774
$result = [
813775
'stored' => false,
814776
'error' => 'It is not allowed, that the question title contains a hash.'
815777
];
816-
$jsonResponse->setData($result);
817-
return $jsonResponse;
778+
return $this->json($result, Response::HTTP_BAD_REQUEST);
818779
}
819780

820781
$isActive = !is_null($isActive);
@@ -837,8 +798,6 @@ public function update(Request $request): JsonResponse
837798

838799
$faq->update($faqData);
839800

840-
$jsonResponse->setStatusCode(Response::HTTP_OK);
841-
$jsonResponse->setData(['stored' => true]);
842-
return $jsonResponse;
801+
return $this->json(['stored' => true], Response::HTTP_OK);
843802
}
844803
}

0 commit comments

Comments
 (0)