Skip to content

Commit d3ecbef

Browse files
committed
refactor: simplified code
1 parent 5497629 commit d3ecbef

13 files changed

+101
-121
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Please check out our page about contributing on [phpmyfaq.de](https://www.phpmyf
178178

179179
You can read the complete documentation on [here](https://phpmyfaq.readthedocs.io/en/latest/).
180180

181-
## REST API v3.0 documentation
181+
## REST API v3.1 documentation
182182

183183
The REST API documentation is available as OpenAPI 3.0 specification:
184184

phpmyfaq/src/phpMyFAQ/Controller/AbstractController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
use Twig\TwigFilter;
3838

3939
#[OA\Info(
40-
version: '3.0',
40+
version: '3.1',
4141
description: 'phpMyFAQ includes a REST API and offers APIs for various services like fetching the phpMyFAQ ' .
4242
'version or doing a search against the phpMyFAQ installation.',
4343
title: 'REST API for phpMyFAQ 4.1',

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function search(Request $request): JsonResponse
4343
{
4444
$searchString = Filter::filterVar($request->query->get('search'), FILTER_SANITIZE_SPECIAL_CHARS);
4545

46-
$user = CurrentUser::getCurrentUser($this->configuration);
47-
[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);
46+
[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($this->currentUser);
4847

4948
$category = new Category($this->configuration, $currentGroups);
5049
$category->setUser($currentUser);
@@ -54,7 +53,7 @@ public function search(Request $request): JsonResponse
5453

5554
$faqPermission = new Permission($this->configuration);
5655
$faqSearch = new Search($this->configuration);
57-
$searchResultSet = new SearchResultSet($user, $faqPermission, $this->configuration);
56+
$searchResultSet = new SearchResultSet($this->currentUser, $faqPermission, $this->configuration);
5857

5958
if (!is_null($searchString)) {
6059
$faqSearch->setCategory($category);

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,25 @@
3232
class BookmarkController extends AbstractController
3333
{
3434
/**
35-
* @throws Exception|\JsonException
35+
* @throws \JsonException
36+
* @throws \Exception
3637
*/
3738
#[Route('api/bookmark/create')]
3839
public function create(Request $request): JsonResponse
3940
{
4041
$this->userIsAuthenticated();
4142

4243
$data = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);
43-
$id = Filter::filterVar($data->id, FILTER_VALIDATE_INT);
44+
$bookmarkId = Filter::filterVar($data->id, FILTER_VALIDATE_INT);
4445
$csrfToken = Filter::filterVar($data->csrfToken, FILTER_SANITIZE_SPECIAL_CHARS);
4546

4647
if (!Token::getInstance($this->container->get('session'))->verifyToken('add-bookmark', $csrfToken)) {
4748
return $this->json(['error' => Translation::get('ad_msg_noauth')], Response::HTTP_UNAUTHORIZED);
4849
}
4950

50-
$currentUser = CurrentUser::getCurrentUser($this->configuration);
51+
$bookmark = new Bookmark($this->configuration, $this->currentUser);
5152

52-
$bookmark = new Bookmark($this->configuration, $currentUser);
53-
54-
if ($bookmark->add($id)) {
53+
if ($bookmark->add($bookmarkId)) {
5554
return $this->json([
5655
'success' => Translation::get('msgBookmarkAdded'),
5756
'linkText' => Translation::get('removeBookmark'),
@@ -63,26 +62,25 @@ public function create(Request $request): JsonResponse
6362
}
6463

6564
/**
66-
* @throws Exception|\JsonException
65+
* @throws \JsonException
66+
* @throws \Exception
6767
*/
6868
#[Route('api/bookmark/delete')]
6969
public function delete(Request $request): JsonResponse
7070
{
7171
$this->userIsAuthenticated();
7272

7373
$data = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);
74-
$id = Filter::filterVar($data->id, FILTER_VALIDATE_INT);
74+
$bookmarkId = Filter::filterVar($data->id, FILTER_VALIDATE_INT);
7575
$csrfToken = Filter::filterVar($data->csrfToken, FILTER_SANITIZE_SPECIAL_CHARS);
7676

7777
if (!Token::getInstance($this->container->get('session'))->verifyToken('delete-bookmark', $csrfToken)) {
7878
return $this->json(['error' => Translation::get('ad_msg_noauth')], Response::HTTP_UNAUTHORIZED);
7979
}
8080

81-
$currentUser = CurrentUser::getCurrentUser($this->configuration);
82-
83-
$bookmark = new Bookmark($this->configuration, $currentUser);
81+
$bookmark = new Bookmark($this->configuration, $this->currentUser);
8482

85-
if ($bookmark->remove($id)) {
83+
if ($bookmark->remove($bookmarkId)) {
8684
return $this->json([
8785
'success' => Translation::get('msgBookmarkRemoved'),
8886
'linkText' => Translation::get('msgAddBookmark'),
@@ -94,7 +92,8 @@ public function delete(Request $request): JsonResponse
9492
}
9593

9694
/**
97-
* @throws Exception|\JsonException
95+
* @throws \JsonException
96+
* @throws \Exception
9897
*/
9998
#[Route('api/bookmark/delete-all')]
10099
public function deleteAll(Request $request): JsonResponse
@@ -108,9 +107,7 @@ public function deleteAll(Request $request): JsonResponse
108107
return $this->json(['error' => Translation::get('ad_msg_noauth')], Response::HTTP_UNAUTHORIZED);
109108
}
110109

111-
$currentUser = CurrentUser::getCurrentUser($this->configuration);
112-
113-
$bookmark = new Bookmark($this->configuration, $currentUser);
110+
$bookmark = new Bookmark($this->configuration, $this->currentUser);
114111

115112
if ($bookmark->removeAll()) {
116113
return $this->json(['success' => Translation::get('msgBookmarkRemoved')], Response::HTTP_OK);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace phpMyFAQ\Controller\Frontend;
1919

20-
use phpMyFAQ\Captcha\Captcha;
2120
use phpMyFAQ\Controller\AbstractController;
2221
use Symfony\Component\HttpFoundation\Response;
2322

@@ -28,17 +27,13 @@ class CaptchaController extends AbstractController
2827
*/
2928
public function renderImage(): Response
3029
{
31-
$captcha = Captcha::getInstance($this->configuration);
32-
33-
// Set headers
3430
$response = new Response();
3531
$response->setStatusCode(Response::HTTP_OK);
3632
$response->headers->set('Content-Type', 'image/jpeg');
3733

3834
// Set image content
39-
$response->setContent($captcha->getCaptchaImage());
35+
$response->setContent($this->container->get('phpmyfaq.captcha')->getCaptchaImage());
4036

41-
// Return the response
4237
return $response;
4338
}
4439
}

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,14 @@
1717

1818
namespace phpMyFAQ\Controller\Frontend;
1919

20-
use phpMyFAQ\Comments;
2120
use phpMyFAQ\Controller\AbstractController;
2221
use phpMyFAQ\Core\Exception;
2322
use phpMyFAQ\Entity\Comment;
2423
use phpMyFAQ\Enums\PermissionType;
25-
use phpMyFAQ\Faq;
2624
use phpMyFAQ\Filter;
27-
use phpMyFAQ\News;
28-
use phpMyFAQ\Notification;
2925
use phpMyFAQ\Session\Token;
30-
use phpMyFAQ\StopWords;
3126
use phpMyFAQ\Translation;
32-
use phpMyFAQ\User;
3327
use phpMyFAQ\User\CurrentUser;
34-
use phpMyFAQ\User\UserSession;
3528
use Symfony\Component\HttpFoundation\JsonResponse;
3629
use Symfony\Component\HttpFoundation\Request;
3730
use Symfony\Component\HttpFoundation\Response;
@@ -46,21 +39,19 @@ class CommentController extends AbstractController
4639
*/
4740
public function create(Request $request): JsonResponse
4841
{
49-
$user = CurrentUser::getCurrentUser($this->configuration);
50-
51-
$faq = new Faq($this->configuration);
52-
$comment = new Comments($this->configuration);
53-
$stopWords = new StopWords($this->configuration);
54-
$session = new UserSession($this->configuration);
55-
$session->setCurrentUser($user);
42+
$faq = $this->container->get('phpmyfaq.faq');
43+
$comment = $this->container->get('phpmyfaq.comments');
44+
$stopWords = $this->container->get('phpmyfaq.stop-words');
45+
$session = $this->container->get('phpmyfaq.user.session');
46+
$session->setCurrentUser($this->currentUser);
5647

5748
$language = $this->container->get('phpmyfaq.language');
5849
$languageCode = $language->setLanguage(
5950
$this->configuration->get('main.languageDetection'),
6051
$this->configuration->get('main.language')
6152
);
6253

63-
if (!$this->isCommentAllowed($user)) {
54+
if (!$this->isCommentAllowed($this->currentUser)) {
6455
return $this->json(['error' => Translation::get('ad_msg_noauth')], Response::HTTP_FORBIDDEN);
6556
}
6657

@@ -86,54 +77,56 @@ public function create(Request $request): JsonResponse
8677

8778
switch ($type) {
8879
case 'news':
89-
$id = $newsId;
80+
$commentId = $newsId;
9081
break;
9182
case 'faq':
92-
$id = $faqId;
83+
$commentId = $faqId;
9384
break;
9485
}
9586

96-
if (empty($id)) {
87+
if (empty($commentId)) {
9788
return $this->json(['error' => Translation::get('errSaveComment')], Response::HTTP_BAD_REQUEST);
9889
}
9990

10091
// Check display name and e-mail address for not logged-in users
101-
if (!$user->isLoggedIn()) {
102-
$user = new User($this->configuration);
92+
if (!$this->currentUser->isLoggedIn()) {
93+
$user = $this->container->get('phpmyfaq.user');
10394
if ($user->checkDisplayName($username) && $user->checkMailAddress($email)) {
10495
$this->configuration->getLogger()->error('Name and email already used by registered user.');
10596
return $this->json(['error' => Translation::get('errSaveComment')], Response::HTTP_CONFLICT);
10697
}
10798
}
10899

109100
if (
110-
!empty($username) && !empty($email) && !empty($commentText) && $stopWords->checkBannedWord($commentText) &&
111-
$comment->isCommentAllowed($id, $languageCode, $type) && $faq->isActive($id, $languageCode, $type)
101+
!empty($username) && !empty($email) && !empty($commentText) &&
102+
$stopWords->checkBannedWord($commentText) &&
103+
$comment->isCommentAllowed($commentId, $languageCode, $type) &&
104+
$faq->isActive($commentId, $languageCode, $type)
112105
) {
113-
$session->userTracking('save_comment', $id);
106+
$session->userTracking('save_comment', $commentId);
114107
$commentEntity = new Comment();
115108
$commentEntity
116-
->setRecordId($id)
109+
->setRecordId($commentId)
117110
->setType($type)
118111
->setUsername($username)
119112
->setEmail($email)
120113
->setComment(nl2br(strip_tags((string) $commentText)))
121114
->setDate($request->server->get('REQUEST_TIME'));
122115

123116
if ($comment->create($commentEntity)) {
124-
$notification = new Notification($this->configuration);
117+
$notification = $this->container->get('phpmyfaq.notification');
125118
if ('faq' == $type) {
126-
$faq->getFaq($id);
119+
$faq->getFaq($commentId);
127120
$notification->sendFaqCommentNotification($faq, $commentEntity);
128121
} else {
129-
$news = new News($this->configuration);
130-
$newsData = $news->get($id);
122+
$news = $this->container->get('phpmyfaq.news');
123+
$newsData = $news->get($commentId);
131124
$notification->sendNewsCommentNotification($newsData, $commentEntity);
132125
}
133126

134127
return $this->json(['success' => Translation::get('msgCommentThanks')], Response::HTTP_OK);
135128
} else {
136-
$session->userTracking('error_save_comment', $id);
129+
$session->userTracking('error_save_comment', $commentId);
137130
return $this->json(['error' => Translation::get('errSaveComment')], Response::HTTP_BAD_REQUEST);
138131
}
139132
} else {
@@ -144,6 +137,9 @@ public function create(Request $request): JsonResponse
144137
}
145138
}
146139

140+
/**
141+
* @throws \Exception
142+
*/
147143
private function isCommentAllowed(CurrentUser $user): bool
148144
{
149145
if (

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
use phpMyFAQ\Controller\AbstractController;
2121
use phpMyFAQ\Core\Exception;
2222
use phpMyFAQ\Filter;
23-
use phpMyFAQ\Mail;
24-
use phpMyFAQ\StopWords;
2523
use phpMyFAQ\Translation;
2624
use phpMyFAQ\Utils;
2725
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -39,8 +37,6 @@ class ContactController extends AbstractController
3937
#[Route('api/contact', methods: ['POST'])]
4038
public function create(Request $request): JsonResponse
4139
{
42-
$stopWords = new StopWords($this->configuration);
43-
4440
$data = json_decode($request->getContent());
4541

4642
$author = trim((string) Filter::filterVar($data->name, FILTER_SANITIZE_SPECIAL_CHARS));
@@ -51,6 +47,8 @@ public function create(Request $request): JsonResponse
5147
return $this->json(['error' => Translation::get('msgCaptcha')], Response::HTTP_BAD_REQUEST);
5248
}
5349

50+
$stopWords = $this->container->get('phpmyfaq.stop-words');
51+
5452
if (
5553
$author !== '' &&
5654
$author !== '0' &&
@@ -68,7 +66,7 @@ public function create(Request $request): JsonResponse
6866
$question
6967
);
7068

71-
$mailer = new Mail($this->configuration);
69+
$mailer = $this->container->get('phpmyfaq.mail');
7270
try {
7371
$mailer->setReplyTo($email, $author);
7472
$mailer->addTo($this->configuration->getAdminEmail());

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@
2424
use phpMyFAQ\Enums\PermissionType;
2525
use phpMyFAQ\Faq\MetaData;
2626
use phpMyFAQ\Filter;
27-
use phpMyFAQ\Helper\CategoryHelper;
28-
use phpMyFAQ\Helper\FaqHelper;
29-
use phpMyFAQ\Notification;
30-
use phpMyFAQ\Question;
31-
use phpMyFAQ\StopWords;
3227
use phpMyFAQ\Translation;
3328
use phpMyFAQ\User\CurrentUser;
34-
use phpMyFAQ\User\UserSession;
3529
use Symfony\Component\HttpFoundation\JsonResponse;
3630
use Symfony\Component\HttpFoundation\Request;
3731
use Symfony\Component\HttpFoundation\Response;
@@ -44,23 +38,20 @@ class FaqController extends AbstractController
4438
*/
4539
public function create(Request $request): JsonResponse
4640
{
47-
$user = CurrentUser::getCurrentUser($this->configuration);
48-
4941
$faq = $this->container->get('phpmyfaq.faq');
50-
$faqHelper = new FaqHelper($this->configuration);
51-
$category = new Category($this->configuration);
52-
$question = new Question($this->configuration);
53-
$stopWords = new StopWords($this->configuration);
54-
$session = new UserSession($this->configuration);
55-
$session->setCurrentUser($user);
42+
$faqHelper = $this->container->get('phpmyfaq.helper.faq');
43+
$question = $this->container->get('phpmyfaq.question');
44+
$stopWords = $this->container->get('phpmyfaq.stop-words');
45+
$session = $this->container->get('phpmyfaq.user.session');
46+
$session->setCurrentUser($this->currentUser);
5647

5748
$language = $this->container->get('phpmyfaq.language');
5849
$languageCode = $language->setLanguage(
5950
$this->configuration->get('main.languageDetection'),
6051
$this->configuration->get('main.language')
6152
);
6253

63-
if (!$this->isAddingFaqsAllowed($user)) {
54+
if (!$this->isAddingFaqsAllowed($this->currentUser)) {
6455
return $this->json(['error' => Translation::get('ad_msg_noauth')], Response::HTTP_FORBIDDEN);
6556
}
6657

@@ -79,6 +70,7 @@ public function create(Request $request): JsonResponse
7970
$answer = trim(nl2br($answer));
8071
}
8172

73+
$category = new Category($this->configuration);
8274
$keywords = Filter::filterVar($data->keywords, FILTER_SANITIZE_SPECIAL_CHARS);
8375
if (isset($data->{'rubrik[]'})) {
8476
if (is_string($data->{'rubrik[]'})) {
@@ -141,15 +133,15 @@ public function create(Request $request): JsonResponse
141133
->save();
142134

143135
// Let the admin and the category owners to be informed by email of this new entry
144-
$categoryHelper = new CategoryHelper();
136+
$categoryHelper = $this->container->get('phpmyfaq.helper.category');
145137
$categoryHelper
146138
->setCategory($category)
147139
->setConfiguration($this->configuration);
148140

149141
$moderators = $categoryHelper->getModerators($categories);
150142

151143
try {
152-
$notification = new Notification($this->configuration);
144+
$notification = $this->container->get('phpmyfaq.notification');
153145
$notification->sendNewFaqAdded($moderators, $faqEntity);
154146
} catch (Exception | TransportExceptionInterface $e) {
155147
$this->configuration->getLogger()->info('Notification could not be sent: ', [ $e->getMessage() ]);

0 commit comments

Comments
 (0)