From 044df00982a4cbdab1466ae23a548e1a4b3a460b Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Sun, 1 Jun 2025 20:34:39 -0500 Subject: [PATCH] fix: broad implementation of broadcaster --- src/Controller/AlertController.php | 19 +++------ src/Controller/TorrentioController.php | 18 +++----- .../Framework/Controller/ApiController.php | 32 +++++---------- .../Controller/Web/PreferencesController.php | 41 +++++++------------ 4 files changed, 36 insertions(+), 74 deletions(-) diff --git a/src/Controller/AlertController.php b/src/Controller/AlertController.php index 62068bb..4a7816e 100644 --- a/src/Controller/AlertController.php +++ b/src/Controller/AlertController.php @@ -2,34 +2,25 @@ namespace App\Controller; +use App\Util\Broadcaster; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; use Symfony\Component\Routing\Attribute\Route; -use Twig\Environment; final class AlertController extends AbstractController { public function __construct( - #[Autowire(service: 'twig')] private readonly Environment $renderer, - private readonly HubInterface $hub, + private readonly Broadcaster $broadcaster, ) {} #[Route('/alert', name: 'app_alert')] public function index(): Response { - $update = new Update( - 'alerts', - $this->renderer->render('Alert.stream.html.twig', [ - 'alert_id' => 1, - 'title' => 'Added to queue', - 'message' => 'This is a testy test!', - ]) + $this->broadcaster->alert( + 'Added to queue', + 'This is a testy test!' ); - $this->hub->publish($update); return $this->json([ 'Success' => 'Published' ]); diff --git a/src/Controller/TorrentioController.php b/src/Controller/TorrentioController.php index 8218274..ad40ee7 100644 --- a/src/Controller/TorrentioController.php +++ b/src/Controller/TorrentioController.php @@ -6,12 +6,11 @@ use App\Torrentio\Action\Handler\GetMovieOptionsHandler; use App\Torrentio\Action\Handler\GetTvShowOptionsHandler; use App\Torrentio\Action\Input\GetMovieOptionsInput; use App\Torrentio\Action\Input\GetTvShowOptionsInput; +use App\Util\Broadcaster; use Carbon\Carbon; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; use Symfony\Component\Routing\Attribute\Route; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; @@ -21,8 +20,7 @@ final class TorrentioController extends AbstractController public function __construct( private readonly GetMovieOptionsHandler $getMovieOptionsHandler, private readonly GetTvShowOptionsHandler $getTvShowOptionsHandler, - private readonly HubInterface $hub, - private readonly \Twig\Environment $renderer, + private readonly Broadcaster $broadcaster, ) {} #[Route('/torrentio/movies/{tmdbId}/{imdbId}', name: 'app_torrentio_movies')] @@ -75,14 +73,10 @@ final class TorrentioController extends AbstractController ); $cache->delete($cacheId); - $this->hub->publish(new Update( - $request->getSession()->get('mercure_alert_topic'), - $this->renderer->render('Alert.stream.html.twig', [ - 'alert_id' => uniqid(), - 'title' => 'Success', - 'message' => 'Torrentio cache Cleared.', - ]) - )); + $this->broadcaster->alert( + title: 'Success', + message: 'Torrentio cache Cleared.' + ); return $cache->get($cacheId, function (ItemInterface $item) use ($input) { $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); diff --git a/src/Download/Framework/Controller/ApiController.php b/src/Download/Framework/Controller/ApiController.php index 3ee6651..b32edca 100644 --- a/src/Download/Framework/Controller/ApiController.php +++ b/src/Download/Framework/Controller/ApiController.php @@ -6,11 +6,9 @@ use App\Download\Action\Handler\DeleteDownloadHandler; use App\Download\Action\Input\DeleteDownloadInput; use App\Download\Action\Input\DownloadMediaInput; use App\Download\Framework\Repository\DownloadRepository; +use App\Util\Broadcaster; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Routing\Attribute\Route; @@ -19,12 +17,11 @@ class ApiController extends AbstractController public function __construct( private DownloadRepository $downloadRepository, private MessageBusInterface $bus, - private readonly HubInterface $hub, + private readonly Broadcaster $broadcaster, ) {} #[Route('/api/download', name: 'api_download', methods: ['POST'])] public function download( - Request $request, DownloadMediaInput $input, ): Response { $download = $this->downloadRepository->insert( @@ -47,33 +44,24 @@ class ApiController extends AbstractController return $this->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); } - $this->hub->publish(new Update( - $request->getSession()->get('mercure_alert_topic'), - $this->renderView('broadcast/Alert.stream.html.twig', [ - 'alert_id' => uniqid(), - 'title' => 'Success', - 'message' => '"' . $input->title . '" added to Queue', - ]) - )); + $this->broadcaster->alert( + title: 'Success', + message: "$input->title added to Queue." + ); return $this->json(['status' => 200, 'message' => 'Added to Queue']); } #[Route('/api/download/{downloadId}', name: 'api_download_delete', methods: ['DELETE'])] public function deleteDownload( - Request $request, DeleteDownloadInput $input, DeleteDownloadHandler $handler, ): Response { $result = $handler->handle($input->toCommand()); - $this->hub->publish(new Update( - $request->getSession()->get('mercure_alert_topic'), - $this->renderView('broadcast/Alert.stream.html.twig', [ - 'alert_id' => uniqid(), - 'title' => 'Success', - 'message' => '"' . $result->download->getTitle() . '" has been deleted.', - ]) - )); + $this->broadcaster->alert( + title: 'Success', + message: "{$result->download->getTitle()} has been deleted.", + ); return $this->json(['status' => 200, 'message' => 'Download Deleted']); } diff --git a/src/User/Framework/Controller/Web/PreferencesController.php b/src/User/Framework/Controller/Web/PreferencesController.php index 6915665..49c38dc 100644 --- a/src/User/Framework/Controller/Web/PreferencesController.php +++ b/src/User/Framework/Controller/Web/PreferencesController.php @@ -9,23 +9,20 @@ use App\User\Action\Handler\SaveUserMediaPreferencesHandler; use App\User\Action\Input\SaveUserDownloadPreferencesInput; use App\User\Action\Input\SaveUserMediaPreferencesInput; use App\User\Framework\Repository\PreferencesRepository; +use App\Util\Broadcaster; use App\Util\CountryLanguages; use App\Util\ProviderList; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; use Symfony\Component\Routing\Attribute\Route; class PreferencesController extends AbstractController { public function __construct( private readonly PreferencesRepository $preferencesRepository, - private readonly SaveUserMediaPreferencesHandler $saveUserMediaPreferencesHandler, - private readonly HubInterface $hub, - private readonly SaveUserDownloadPreferencesHandler $saveUserDownloadPreferencesHandler, + private readonly Broadcaster $broadcaster, ) {} + #[Route('/user/preferences', 'app_user_preferences', methods: ['GET'])] public function mediaPreferences(): Response { @@ -48,25 +45,21 @@ class PreferencesController extends AbstractController #[Route('/user/preferences/media', 'app_save_media_preferences', methods: ['POST'])] public function saveMediaPreferences( - Request $request, SaveUserMediaPreferencesInput $input, + SaveUserMediaPreferencesHandler $saveUserMediaPreferencesHandler, ): Response { - $this->saveUserMediaPreferencesHandler->handle($input->toCommand()); + $saveUserMediaPreferencesHandler->handle($input->toCommand()); $mediaPreferences = $this->getUser()->getMediaPreferences(); $downloadPreferences = $this->getUser()->getDownloadPreferences(); $languages = CountryLanguages::$languages; sort($languages); - $this->hub->publish(new Update( - $request->getSession()->get('mercure_alert_topic'), - $this->renderView('broadcast/Alert.stream.html.twig', [ - 'alert_id' => uniqid(), - 'title' => 'Success', - 'message' => 'Your media preferences have been saved.', - ]) - )); + $this->broadcaster->alert( + title: 'Success', + message: 'Your media preferences have been saved.' + ); return $this->render( 'user/preferences.html.twig', @@ -82,24 +75,20 @@ class PreferencesController extends AbstractController #[Route('/user/preferences/download', 'app_save_download_preferences', methods: ['POST'])] public function saveDownloadPreferences( - Request $request, SaveUserDownloadPreferencesInput $input, + SaveUserDownloadPreferencesHandler $saveUserDownloadPreferencesHandler, ): Response { - $downloadPreferences = $this->saveUserDownloadPreferencesHandler->handle($input->toCommand())->downloadPreferences; + $downloadPreferences = $saveUserDownloadPreferencesHandler->handle($input->toCommand())->downloadPreferences; $mediaPreferences = $this->getUser()->getMediaPreferences(); $languages = CountryLanguages::$languages; sort($languages); - $this->hub->publish(new Update( - $request->getSession()->get('mercure_alert_topic'), - $this->renderView('broadcast/Alert.stream.html.twig', [ - 'alert_id' => uniqid(), - 'title' => 'Success', - 'message' => 'Your download preferences have been saved.', - ]) - )); + $this->broadcaster->alert( + title: 'Success', + message: 'Your download preferences have been saved.' + ); return $this->render( 'user/preferences.html.twig',