Skip to content

Commit

Permalink
Merge pull request #182 from frank9999/fixdeprecationAlias
Browse files Browse the repository at this point in the history
Add ForumDisabledException to better handle disabled forum
  • Loading branch information
frank9999 authored Dec 13, 2023
2 parents 1e2409c + b17f277 commit cc12e3b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 34 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Controller/Forum/BaseForumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
}
}
25 changes: 21 additions & 4 deletions src/Controller/Forum/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/Forum/ForumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace FrankProjects\UltimateWarfare\Controller\Forum;

use FrankProjects\UltimateWarfare\Exception\ForumDisabledException;
use FrankProjects\UltimateWarfare\Repository\CategoryRepository;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -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');
}

Expand Down
17 changes: 13 additions & 4 deletions src/Controller/Forum/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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(
Expand Down
57 changes: 41 additions & 16 deletions src/Controller/Forum/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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);

Expand All @@ -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 {
Expand All @@ -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);

Expand All @@ -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 {
Expand All @@ -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]);
}
}
10 changes: 10 additions & 0 deletions src/Exception/ForumDisabledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace FrankProjects\UltimateWarfare\Exception;

final class ForumDisabledException extends \Exception
{
protected $message = 'Forum is disabled!'; // @phpstan-ignore-line
}

0 comments on commit cc12e3b

Please sign in to comment.