From b17f277a053b7ad9c09d2201c49d5d38f1083684 Mon Sep 17 00:00:00 2001 From: Frank Schreuder Date: Tue, 12 Dec 2023 23:30:07 +0100 Subject: [PATCH] Add ForumDisabledException to better handle disabled forum --- composer.lock | 14 ++--- src/Controller/Forum/BaseForumController.php | 4 +- src/Controller/Forum/CategoryController.php | 25 +++++++-- src/Controller/Forum/ForumController.php | 3 +- src/Controller/Forum/PostController.php | 17 ++++-- src/Controller/Forum/TopicController.php | 57 ++++++++++++++------ src/Exception/ForumDisabledException.php | 10 ++++ 7 files changed, 96 insertions(+), 34 deletions(-) create mode 100644 src/Exception/ForumDisabledException.php diff --git a/composer.lock b/composer.lock index 2d5af6e9..3ecd00b6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3d7ec44360ff5aaa40f9c7e49fb32fa7", + "content-hash": "a18221e65c40b615cd1684a3fa8caa44", "packages": [ { "name": "doctrine/cache", @@ -6327,16 +6327,16 @@ "packages-dev": [ { "name": "phpstan/phpstan", - "version": "1.10.48", + "version": "1.10.49", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "087ed4b5f4a7a6e8f3bbdfbfe98ce5c181380bc6" + "reference": "9367ba4c4f6ad53e9efb594d74a8941563caccf6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/087ed4b5f4a7a6e8f3bbdfbfe98ce5c181380bc6", - "reference": "087ed4b5f4a7a6e8f3bbdfbfe98ce5c181380bc6", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9367ba4c4f6ad53e9efb594d74a8941563caccf6", + "reference": "9367ba4c4f6ad53e9efb594d74a8941563caccf6", "shasum": "" }, "require": { @@ -6385,7 +6385,7 @@ "type": "tidelift" } ], - "time": "2023-12-08T14:34:28+00:00" + "time": "2023-12-12T10:05:12+00:00" }, { "name": "phpstan/phpstan-doctrine", @@ -6827,7 +6827,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^8.1", + "php": "^8.3", "ext-gd": "*", "ext-intl": "*", "ext-json": "*", diff --git a/src/Controller/Forum/BaseForumController.php b/src/Controller/Forum/BaseForumController.php index 80d10e5b..d6a57f5d 100644 --- a/src/Controller/Forum/BaseForumController.php +++ b/src/Controller/Forum/BaseForumController.php @@ -4,7 +4,7 @@ namespace FrankProjects\UltimateWarfare\Controller\Forum; -use RuntimeException; +use FrankProjects\UltimateWarfare\Exception\ForumDisabledException; use FrankProjects\UltimateWarfare\Controller\BaseController; use FrankProjects\UltimateWarfare\Entity\User; @@ -23,7 +23,7 @@ public function getGameUser(): ?User public function ensureForumEnabled(): void { if ($this->getParameter('app.uw_forum_enabled') !== true) { - throw new RuntimeException('Forum is disabled!'); + throw new ForumDisabledException(); } } } diff --git a/src/Controller/Forum/CategoryController.php b/src/Controller/Forum/CategoryController.php index 449a1b18..3228524a 100644 --- a/src/Controller/Forum/CategoryController.php +++ b/src/Controller/Forum/CategoryController.php @@ -5,6 +5,7 @@ namespace FrankProjects\UltimateWarfare\Controller\Forum; use FrankProjects\UltimateWarfare\Entity\Category; +use FrankProjects\UltimateWarfare\Exception\ForumDisabledException; use FrankProjects\UltimateWarfare\Form\Forum\CategoryType; use FrankProjects\UltimateWarfare\Repository\CategoryRepository; use FrankProjects\UltimateWarfare\Repository\TopicRepository; @@ -32,7 +33,11 @@ public function __construct( public function category(int $categoryId): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $category = $this->categoryRepository->find($categoryId); $topics = $this->topicRepository->getByCategorySortedByStickyAndDate($category); @@ -49,7 +54,11 @@ public function category(int $categoryId): Response public function create(Request $request): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $category = new Category(); $form = $this->createForm(CategoryType::class, $category); @@ -77,7 +86,11 @@ public function create(Request $request): Response public function edit(Request $request, int $categoryId): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $category = $this->categoryRepository->find($categoryId); @@ -111,7 +124,11 @@ public function edit(Request $request, int $categoryId): Response public function remove(int $categoryId): RedirectResponse { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->redirectToRoute('Forum'); + } $category = $this->categoryRepository->find($categoryId); diff --git a/src/Controller/Forum/ForumController.php b/src/Controller/Forum/ForumController.php index 9621f5be..ac63de93 100644 --- a/src/Controller/Forum/ForumController.php +++ b/src/Controller/Forum/ForumController.php @@ -4,6 +4,7 @@ namespace FrankProjects\UltimateWarfare\Controller\Forum; +use FrankProjects\UltimateWarfare\Exception\ForumDisabledException; use FrankProjects\UltimateWarfare\Repository\CategoryRepository; use Symfony\Component\HttpFoundation\Response; @@ -13,7 +14,7 @@ public function index(CategoryRepository $categoryRepository): Response { try { $this->ensureForumEnabled(); - } catch (\Exception $exception) { + } catch (ForumDisabledException) { return $this->render('forum/forum_disabled.html.twig'); } diff --git a/src/Controller/Forum/PostController.php b/src/Controller/Forum/PostController.php index 1ad91e12..79b2365e 100644 --- a/src/Controller/Forum/PostController.php +++ b/src/Controller/Forum/PostController.php @@ -4,6 +4,7 @@ namespace FrankProjects\UltimateWarfare\Controller\Forum; +use FrankProjects\UltimateWarfare\Exception\ForumDisabledException; use FrankProjects\UltimateWarfare\Form\Forum\PostType; use FrankProjects\UltimateWarfare\Repository\PostRepository; use FrankProjects\UltimateWarfare\Service\Action\PostActionService; @@ -27,7 +28,11 @@ public function __construct( public function remove(int $postId): RedirectResponse { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->redirectToRoute('Forum'); + } $post = $this->postRepository->find($postId); @@ -45,12 +50,16 @@ public function remove(int $postId): RedirectResponse $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } public function edit(Request $request, int $postId): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $post = $this->postRepository->find($postId); @@ -70,7 +79,7 @@ public function edit(Request $request, int $postId): Response $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } return $this->render( diff --git a/src/Controller/Forum/TopicController.php b/src/Controller/Forum/TopicController.php index 1ae68128..4573a503 100644 --- a/src/Controller/Forum/TopicController.php +++ b/src/Controller/Forum/TopicController.php @@ -6,6 +6,7 @@ use FrankProjects\UltimateWarfare\Entity\Topic; use FrankProjects\UltimateWarfare\Entity\Post; +use FrankProjects\UltimateWarfare\Exception\ForumDisabledException; use FrankProjects\UltimateWarfare\Form\Forum\PostType; use FrankProjects\UltimateWarfare\Form\Forum\TopicType; use FrankProjects\UltimateWarfare\Repository\CategoryRepository; @@ -38,7 +39,11 @@ public function __construct( public function topic(Request $request, int $topicId): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $topic = $this->topicRepository->find($topicId); @@ -71,7 +76,11 @@ public function topic(Request $request, int $topicId): Response public function create(Request $request, int $categoryId): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $category = $this->categoryRepository->find($categoryId); @@ -92,7 +101,7 @@ public function create(Request $request, int $categoryId): Response $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } return $this->render( @@ -107,7 +116,11 @@ public function create(Request $request, int $categoryId): Response public function remove(int $topicId): RedirectResponse { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->redirectToRoute('Forum'); + } $topic = $this->topicRepository->find($topicId); @@ -130,12 +143,16 @@ public function remove(int $topicId): RedirectResponse $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Category', ['categoryId' => $category->getId()], 302); + return $this->redirectToRoute('Forum/Category', ['categoryId' => $category->getId()]); } public function edit(Request $request, int $topicId): Response { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->render('forum/forum_disabled.html.twig'); + } $topic = $this->topicRepository->find($topicId); @@ -147,7 +164,7 @@ public function edit(Request $request, int $topicId): Response $user = $this->getGameUser(); if ($user == null) { $this->addFlash('error', 'Not logged in!'); - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } $form = $this->createForm(TopicType::class, $topic); @@ -160,7 +177,7 @@ public function edit(Request $request, int $topicId): Response $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } return $this->render( @@ -173,9 +190,13 @@ public function edit(Request $request, int $topicId): Response ); } - public function sticky(Request $request, int $topicId): RedirectResponse + public function sticky(int $topicId): RedirectResponse { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->redirectToRoute('Forum'); + } $topic = $this->topicRepository->find($topicId); @@ -187,7 +208,7 @@ public function sticky(Request $request, int $topicId): RedirectResponse $user = $this->getGameUser(); if ($user == null) { $this->addFlash('error', 'Not logged in!'); - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } try { @@ -197,12 +218,16 @@ public function sticky(Request $request, int $topicId): RedirectResponse $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topicId], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topicId]); } - public function unsticky(Request $request, int $topicId): RedirectResponse + public function unsticky(int $topicId): RedirectResponse { - $this->ensureForumEnabled(); + try { + $this->ensureForumEnabled(); + } catch (ForumDisabledException) { + return $this->redirectToRoute('Forum'); + } $topic = $this->topicRepository->find($topicId); @@ -214,7 +239,7 @@ public function unsticky(Request $request, int $topicId): RedirectResponse $user = $this->getGameUser(); if ($user == null) { $this->addFlash('error', 'Not logged in!'); - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topic->getId()]); } try { @@ -224,6 +249,6 @@ public function unsticky(Request $request, int $topicId): RedirectResponse $this->addFlash('error', $e->getMessage()); } - return $this->redirectToRoute('Forum/Topic', ['topicId' => $topicId], 302); + return $this->redirectToRoute('Forum/Topic', ['topicId' => $topicId]); } } diff --git a/src/Exception/ForumDisabledException.php b/src/Exception/ForumDisabledException.php new file mode 100644 index 00000000..5abf1dcf --- /dev/null +++ b/src/Exception/ForumDisabledException.php @@ -0,0 +1,10 @@ +