diff --git a/src/Controller/Admin/Debug/ClearCacheAction.php b/src/Controller/Admin/Debug/ClearCacheAction.php index ba327dd4a..2d9dda0db 100644 --- a/src/Controller/Admin/Debug/ClearCacheAction.php +++ b/src/Controller/Admin/Debug/ClearCacheAction.php @@ -7,7 +7,6 @@ namespace App\Controller\Admin\Debug; use App\Console\Application; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class ClearCacheAction @@ -26,7 +25,7 @@ final class ClearCacheAction ); // Flash an update to ensure the session is recreated. - $request->getFlash()->addMessage($resultOutput, Flash::SUCCESS); + $request->getFlash()->success($resultOutput); return $response->withRedirect($request->getRouter()->fromHere('admin:debug:index')); } diff --git a/src/Controller/Admin/Debug/ClearQueueAction.php b/src/Controller/Admin/Debug/ClearQueueAction.php index f1dd917b8..132a2dd0b 100644 --- a/src/Controller/Admin/Debug/ClearQueueAction.php +++ b/src/Controller/Admin/Debug/ClearQueueAction.php @@ -8,7 +8,6 @@ use App\Http\Response; use App\Http\ServerRequest; use App\MessageQueue\QueueManagerInterface; use App\MessageQueue\QueueNames; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class ClearQueueAction @@ -32,10 +31,7 @@ final class ClearQueueAction } // Flash an update to ensure the session is recreated. - $request->getFlash()->addMessage( - __('Message queue cleared.'), - Flash::SUCCESS - ); + $request->getFlash()->success(__('Message queue cleared.')); return $response->withRedirect($request->getRouter()->fromHere('admin:debug:index')); } diff --git a/src/Controller/Frontend/Account/ForgotPasswordAction.php b/src/Controller/Frontend/Account/ForgotPasswordAction.php index 2af7d0422..506e86963 100644 --- a/src/Controller/Frontend/Account/ForgotPasswordAction.php +++ b/src/Controller/Frontend/Account/ForgotPasswordAction.php @@ -10,7 +10,6 @@ use App\Http\Response; use App\Http\ServerRequest; use App\RateLimit; use App\Service\Mail; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class ForgotPasswordAction @@ -38,7 +37,7 @@ final class ForgotPasswordAction try { $this->rateLimit->checkRequestRateLimit($request, 'forgot', 30, 3); } catch (RateLimitExceededException) { - $flash->addMessage( + $flash->error( sprintf( '%s
%s', __('Too many forgot password attempts'), @@ -47,7 +46,6 @@ final class ForgotPasswordAction . '30 seconds and try again.' ) ), - Flash::ERROR ); return $response->withRedirect($request->getUri()->getPath()); @@ -75,7 +73,7 @@ final class ForgotPasswordAction $this->mail->send($email); } - $flash->addMessage( + $flash->success( sprintf( '%s
%s', __('Account recovery e-mail sent.'), @@ -84,7 +82,6 @@ final class ForgotPasswordAction . 'for a password reset message.' ) ), - Flash::SUCCESS ); return $response->withRedirect($request->getRouter()->named('account:login')); diff --git a/src/Controller/Frontend/Account/LoginAction.php b/src/Controller/Frontend/Account/LoginAction.php index b8df10d31..0a93d6a29 100644 --- a/src/Controller/Frontend/Account/LoginAction.php +++ b/src/Controller/Frontend/Account/LoginAction.php @@ -9,7 +9,6 @@ use App\Exception\RateLimitExceededException; use App\Http\Response; use App\Http\ServerRequest; use App\RateLimit; -use App\Session\Flash; use Doctrine\ORM\EntityManagerInterface; use Mezzio\Session\SessionCookiePersistenceInterface; use Psr\Http\Message\ResponseInterface; @@ -55,13 +54,12 @@ final class LoginAction try { $this->rateLimit->checkRequestRateLimit($request, 'login', 30, 5); } catch (RateLimitExceededException) { - $flash->addMessage( + $flash->error( sprintf( '%s
%s', __('Too many login attempts'), __('You have attempted to log in too many times. Please wait 30 seconds and try again.') ), - Flash::ERROR ); return $response->withRedirect($request->getUri()->getPath()); @@ -93,20 +91,18 @@ final class LoginAction // Redirect to complete setup if it's not completed yet. if (!$settings->isSetupComplete()) { - $flash->addMessage( + $flash->success( sprintf( '%s
%s', __('Logged in successfully.'), __('Complete the setup process to get started.') ), - Flash::SUCCESS ); return $response->withRedirect($request->getRouter()->named('setup:index')); } - $flash->addMessage( + $flash->success( '' . __('Logged in successfully.') . '
' . $user->getEmail(), - Flash::SUCCESS ); $referrer = $session->get('login_referrer'); @@ -115,9 +111,8 @@ final class LoginAction ); } - $flash->addMessage( + $flash->error( '' . __('Login unsuccessful') . '
' . __('Your credentials could not be verified.'), - Flash::ERROR ); return $response->withRedirect((string)$request->getUri()); diff --git a/src/Controller/Frontend/Account/MasqueradeAction.php b/src/Controller/Frontend/Account/MasqueradeAction.php index b9eb95e17..45f65f9e5 100644 --- a/src/Controller/Frontend/Account/MasqueradeAction.php +++ b/src/Controller/Frontend/Account/MasqueradeAction.php @@ -8,7 +8,6 @@ use App\Entity; use App\Exception\NotFoundException; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class MasqueradeAction @@ -37,9 +36,8 @@ final class MasqueradeAction $auth = $request->getAuth(); $auth->masqueradeAsUser($user); - $request->getFlash()->addMessage( + $request->getFlash()->success( '' . __('Logged in successfully.') . '
' . $user->getEmail(), - Flash::SUCCESS ); return $response->withRedirect($request->getRouter()->named('dashboard')); diff --git a/src/Controller/Frontend/Account/RecoverAction.php b/src/Controller/Frontend/Account/RecoverAction.php index dace096b2..d01f681c8 100644 --- a/src/Controller/Frontend/Account/RecoverAction.php +++ b/src/Controller/Frontend/Account/RecoverAction.php @@ -7,7 +7,6 @@ namespace App\Controller\Frontend\Account; use App\Entity; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use Doctrine\ORM\EntityManagerInterface; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; @@ -30,12 +29,11 @@ final class RecoverAction $flash = $request->getFlash(); if (!$user instanceof Entity\User) { - $flash->addMessage( + $flash->error( sprintf( '%s', __('Invalid token specified.'), ), - Flash::ERROR ); return $response->withRedirect($request->getRouter()->named('account:login')); @@ -64,13 +62,12 @@ final class RecoverAction $this->loginTokenRepo->revokeForUser($user); - $flash->addMessage( + $flash->success( sprintf( '%s
%s', __('Logged in using account recovery token'), __('Your password has been updated.') ), - Flash::SUCCESS ); return $response->withRedirect($request->getRouter()->named('dashboard')); diff --git a/src/Controller/Frontend/Account/TwoFactorAction.php b/src/Controller/Frontend/Account/TwoFactorAction.php index e0664675d..eef684003 100644 --- a/src/Controller/Frontend/Account/TwoFactorAction.php +++ b/src/Controller/Frontend/Account/TwoFactorAction.php @@ -7,7 +7,6 @@ namespace App\Controller\Frontend\Account; use App\Entity\User; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class TwoFactorAction @@ -26,9 +25,8 @@ final class TwoFactorAction /** @var User $user */ $user = $auth->getUser(); - $flash->addMessage( + $flash->success( '' . __('Logged in successfully.') . '
' . $user->getEmail(), - Flash::SUCCESS ); $referrer = $request->getSession()->get('login_referrer'); @@ -39,9 +37,8 @@ final class TwoFactorAction return $response->withRedirect($request->getRouter()->named('dashboard')); } - $flash->addMessage( + $flash->error( '' . __('Login unsuccessful') . '
' . __('Your credentials could not be verified.'), - Flash::ERROR ); return $response->withRedirect((string)$request->getUri()); diff --git a/src/Controller/Frontend/PublicPages/PodcastEpisodeAction.php b/src/Controller/Frontend/PublicPages/PodcastEpisodeAction.php index 36b9bf786..3f26519c8 100644 --- a/src/Controller/Frontend/PublicPages/PodcastEpisodeAction.php +++ b/src/Controller/Frontend/PublicPages/PodcastEpisodeAction.php @@ -11,7 +11,6 @@ use App\Exception\PodcastNotFoundException; use App\Exception\StationNotFoundException; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class PodcastEpisodeAction @@ -53,7 +52,7 @@ final class PodcastEpisodeAction ); if (!($episode instanceof PodcastEpisode) || !$episode->isPublished()) { - $request->getFlash()->addMessage(__('Episode not found.'), Flash::ERROR); + $request->getFlash()->error(__('Episode not found.')); return $response->withRedirect($podcastEpisodesLink); } diff --git a/src/Controller/Frontend/PublicPages/PodcastEpisodesAction.php b/src/Controller/Frontend/PublicPages/PodcastEpisodesAction.php index 9529ffe0d..4f6019186 100644 --- a/src/Controller/Frontend/PublicPages/PodcastEpisodesAction.php +++ b/src/Controller/Frontend/PublicPages/PodcastEpisodesAction.php @@ -11,7 +11,6 @@ use App\Exception\PodcastNotFoundException; use App\Exception\StationNotFoundException; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use Psr\Http\Message\ResponseInterface; final class PodcastEpisodesAction @@ -65,7 +64,7 @@ final class PodcastEpisodesAction ); if (count($publishedEpisodes) === 0) { - $request->getFlash()->addMessage(__('No episodes found.'), Flash::ERROR); + $request->getFlash()->error(__('No episodes found.')); return $response->withRedirect($podcastsLink); } diff --git a/src/Controller/Frontend/SetupController.php b/src/Controller/Frontend/SetupController.php index 62b589980..e2794e9da 100644 --- a/src/Controller/Frontend/SetupController.php +++ b/src/Controller/Frontend/SetupController.php @@ -10,7 +10,6 @@ use App\Exception\NotLoggedInException; use App\Exception\ValidationException; use App\Http\Response; use App\Http\ServerRequest; -use App\Session\Flash; use App\Version; use App\VueComponent\StationFormComponent; use Doctrine\ORM\EntityManagerInterface; @@ -188,7 +187,7 @@ final class SetupController ServerRequest $request, Response $response ): ResponseInterface { - $request->getFlash()->addMessage('' . __('Setup has already been completed!') . '', Flash::ERROR); + $request->getFlash()->error('' . __('Setup has already been completed!') . ''); return $response->withRedirect($request->getRouter()->named('dashboard')); } diff --git a/src/Http/ErrorHandler.php b/src/Http/ErrorHandler.php index 9376902db..7013c44f5 100644 --- a/src/Http/ErrorHandler.php +++ b/src/Http/ErrorHandler.php @@ -175,7 +175,7 @@ final class ErrorHandler extends \Slim\Handlers\ErrorHandler $session = $sessionPersistence->initializeSessionFromRequest($this->request); $flash = new Flash($session); - $flash->addMessage(__('You must be logged in to access this page.'), Flash::ERROR); + $flash->error(__('You must be logged in to access this page.')); // Set referrer for login redirection. $session->set('login_referrer', $this->request->getUri()->getPath()); @@ -204,9 +204,8 @@ final class ErrorHandler extends \Slim\Handlers\ErrorHandler $session = $sessionPersistence->initializeSessionFromRequest($this->request); $flash = new Flash($session); - $flash->addMessage( + $flash->error( __('You do not have permission to access this portion of the site.'), - Flash::ERROR ); $response = $sessionPersistence->persistSession($session, $response); diff --git a/src/Notification/Check/BaseUrlCheck.php b/src/Notification/Check/BaseUrlCheck.php index 08a8fba66..c8b694d65 100644 --- a/src/Notification/Check/BaseUrlCheck.php +++ b/src/Notification/Check/BaseUrlCheck.php @@ -8,7 +8,7 @@ use App\Entity\Api\Notification; use App\Entity\Repository\SettingsRepository; use App\Enums\GlobalPermissions; use App\Event\GetNotifications; -use App\Session\Flash; +use App\Session\FlashLevels; final class BaseUrlCheck { @@ -57,7 +57,7 @@ final class BaseUrlCheck (string)$baseUriWithRequest ); $notification->body = implode(' ', $notificationBodyParts); - $notification->type = Flash::WARNING; + $notification->type = FlashLevels::Warning->value; $notification->actionLabel = __('System Settings'); $notification->actionUrl = $router->named('admin:settings:index'); diff --git a/src/Notification/Check/ProfilerAdvisorCheck.php b/src/Notification/Check/ProfilerAdvisorCheck.php index 20caea278..18b1ec24a 100644 --- a/src/Notification/Check/ProfilerAdvisorCheck.php +++ b/src/Notification/Check/ProfilerAdvisorCheck.php @@ -8,7 +8,7 @@ use App\Entity\Api\Notification; use App\Enums\GlobalPermissions; use App\Environment; use App\Event\GetNotifications; -use App\Session\Flash; +use App\Session\FlashLevels; final class ProfilerAdvisorCheck { @@ -39,7 +39,7 @@ final class ProfilerAdvisorCheck 'You can track the execution time and memory usage of any AzuraCast page or application ' . 'from the profiler page.', ); - $notification->type = Flash::INFO; + $notification->type = FlashLevels::Info->value; $notification->actionLabel = __('Profiler Control Panel'); $notification->actionUrl = '/?' . http_build_query( @@ -58,7 +58,7 @@ final class ProfilerAdvisorCheck 'This can have an adverse impact on system performance. ' . 'You should disable this when possible.' ); - $notification->type = Flash::WARNING; + $notification->type = FlashLevels::Warning->value; $event->addNotification($notification); } diff --git a/src/Notification/Check/RecentBackupCheck.php b/src/Notification/Check/RecentBackupCheck.php index 484b5c36f..00845ddaf 100644 --- a/src/Notification/Check/RecentBackupCheck.php +++ b/src/Notification/Check/RecentBackupCheck.php @@ -8,7 +8,7 @@ use App\Entity; use App\Enums\GlobalPermissions; use App\Environment; use App\Event\GetNotifications; -use App\Session\Flash; +use App\Session\FlashLevels; use Carbon\CarbonImmutable; final class RecentBackupCheck @@ -48,7 +48,7 @@ final class RecentBackupCheck $notification = new Entity\Api\Notification(); $notification->title = __('Installation Not Recently Backed Up'); $notification->body = __('This installation has not been backed up in the last two weeks.'); - $notification->type = Flash::INFO; + $notification->type = FlashLevels::Info->value; $router = $request->getRouter(); $notification->actionLabel = __('Backups'); diff --git a/src/Notification/Check/ServiceCheck.php b/src/Notification/Check/ServiceCheck.php index 3c81ba767..f48927197 100644 --- a/src/Notification/Check/ServiceCheck.php +++ b/src/Notification/Check/ServiceCheck.php @@ -8,7 +8,7 @@ use App\Entity\Api\Notification; use App\Enums\GlobalPermissions; use App\Event\GetNotifications; use App\Service\ServiceControl; -use App\Session\Flash; +use App\Session\FlashLevels; final class ServiceCheck { @@ -35,7 +35,7 @@ final class ServiceCheck $notification->body = __( 'One of the essential services on this installation is not currently running. Visit the system administration and check the system logs to find the cause of this issue.' ); - $notification->type = Flash::ERROR; + $notification->type = FlashLevels::Error->value; $router = $request->getRouter(); diff --git a/src/Notification/Check/SyncTaskCheck.php b/src/Notification/Check/SyncTaskCheck.php index 7ab52ced2..5c0556fda 100644 --- a/src/Notification/Check/SyncTaskCheck.php +++ b/src/Notification/Check/SyncTaskCheck.php @@ -7,7 +7,7 @@ namespace App\Notification\Check; use App\Entity; use App\Enums\GlobalPermissions; use App\Event\GetNotifications; -use App\Session\Flash; +use App\Session\FlashLevels; final class SyncTaskCheck { @@ -39,7 +39,7 @@ final class SyncTaskCheck $notification->body = __( 'Routine synchronization is currently disabled. Make sure to re-enable it to resume routine maintenance tasks.' ); - $notification->type = Flash::ERROR; + $notification->type = FlashLevels::Error->value; // phpcs:enable $event->addNotification($notification); @@ -54,7 +54,7 @@ final class SyncTaskCheck $notification->body = __( 'The routine synchronization task has not run recently. This may indicate an error with your installation.' ); - $notification->type = Flash::ERROR; + $notification->type = FlashLevels::Error->value; $router = $request->getRouter(); diff --git a/src/Notification/Check/UpdateCheck.php b/src/Notification/Check/UpdateCheck.php index f9a4c2f78..5f2f4ba18 100644 --- a/src/Notification/Check/UpdateCheck.php +++ b/src/Notification/Check/UpdateCheck.php @@ -8,7 +8,7 @@ use App\Entity; use App\Enums\GlobalPermissions; use App\Enums\ReleaseChannel; use App\Event\GetNotifications; -use App\Session\Flash; +use App\Session\FlashLevels; use App\Version; final class UpdateCheck @@ -61,7 +61,7 @@ final class UpdateCheck $notification = new Entity\Api\Notification(); $notification->title = __('New AzuraCast Release Version Available'); $notification->body = implode(' ', $notificationParts); - $notification->type = Flash::INFO; + $notification->type = FlashLevels::Info->value; $notification->actionLabel = $actionLabel; $notification->actionUrl = $actionUrl; @@ -86,7 +86,7 @@ final class UpdateCheck $notification = new Entity\Api\Notification(); $notification->title = __('New AzuraCast Updates Available'); $notification->body = implode(' ', $notificationParts); - $notification->type = Flash::INFO; + $notification->type = FlashLevels::Info->value; $notification->actionLabel = $actionLabel; $notification->actionUrl = $actionUrl; diff --git a/src/Session/Flash.php b/src/Session/Flash.php index d43f109ee..e6c561fbc 100644 --- a/src/Session/Flash.php +++ b/src/Session/Flash.php @@ -13,11 +13,6 @@ final class Flash { public const SESSION_KEY = 'flash'; - public const SUCCESS = 'success'; - public const WARNING = 'warning'; - public const ERROR = 'danger'; - public const INFO = 'info'; - private ?array $messages = null; public function __construct( @@ -25,43 +20,42 @@ final class Flash ) { } - /** - * Alias of addMessage. - * - * @param string $message - * @param string $level - * @param bool $saveInSession - */ - public function alert(string $message, string $level = self::INFO, bool $saveInSession = true): void - { - $this->addMessage($message, $level, $saveInSession); + public function success( + string $message, + bool $saveInSession = true + ): void { + $this->addMessage($message, FlashLevels::Success, $saveInSession); } - /** - * Add a message to the flash message queue. - * - * @param string $message - * @param string $level - * @param bool $saveInSession - */ - public function addMessage(string $message, string $level = self::INFO, bool $saveInSession = true): void - { - $colorChart = [ - 'green' => self::SUCCESS, - 'success' => self::SUCCESS, - 'yellow' => self::WARNING, - 'warning' => self::WARNING, - 'red' => self::ERROR, - 'error' => self::ERROR, - 'danger' => self::ERROR, - 'info' => self::INFO, - 'blue' => self::INFO, - 'default' => '', - ]; + public function warning( + string $message, + bool $saveInSession = true + ): void { + $this->addMessage($message, FlashLevels::Warning, $saveInSession); + } + public function error( + string $message, + bool $saveInSession = true + ): void { + $this->addMessage($message, FlashLevels::Error, $saveInSession); + } + + public function info( + string $message, + bool $saveInSession = true + ): void { + $this->addMessage($message, FlashLevels::Info, $saveInSession); + } + + public function addMessage( + string $message, + FlashLevels $level = FlashLevels::Info, + bool $saveInSession = true + ): void { $messageRow = [ 'text' => $message, - 'color' => $colorChart[$level] ?? $level, + 'color' => $level->value, ]; $this->getMessages(); diff --git a/src/Session/FlashLevels.php b/src/Session/FlashLevels.php new file mode 100644 index 000000000..3bedc8854 --- /dev/null +++ b/src/Session/FlashLevels.php @@ -0,0 +1,13 @@ +