From 5e4a2289a14785efc5efa7247c089a9348c1fcf1 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Fri, 6 Dec 2024 20:24:07 +0000 Subject: [PATCH] Add a flash manager but it still doesn't work properly on the twig templates. --- app/src/Bootstrap.php | 6 +-- app/src/Controller/HealthCheck.php | 11 +++++- app/src/Controller/User/LoginUserAction.php | 12 +++--- app/src/Controller/User/LogoutUserAction.php | 6 ++- .../Controller/User/RegisterUserAction.php | 4 +- .../Api/Session/FlashMessagesInterface.php | 2 +- .../Infrastructure/Session/FlashMessages.php | 39 ++++++++++--------- app/src/Middlewares.php | 4 +- app/templates/channel/create.twig | 1 + 9 files changed, 47 insertions(+), 38 deletions(-) diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 65ba985..92a5eaa 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -16,6 +16,7 @@ use Monolog\Logger; use Odan\Session\PhpSession; use Odan\Session\SessionInterface; use Odan\Session\SessionManagerInterface; +use Odan\Session\FlashInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -27,7 +28,6 @@ use Slim\App; use Slim\Factory\AppFactory; use Slim\Psr7\Factory\ResponseFactory; use Slovocast\Infrastructure\Session\FlashMessages; -use Slovocast\Infrastructure\Session\FlashMessagesInterface; use Slovocast\Middleware\SessionMiddleware; use Twig\Error\LoaderError; @@ -133,13 +133,13 @@ class Bootstrap 'session' => function (ContainerInterface $container) { return $container->get(SessionInterface::class); }, - FlashMessagesInterface::class => function (ContainerInterface $container) { + FlashInterface::class => function (ContainerInterface $container) { return new FlashMessages( $container->get(SessionInterface::class) ); }, 'flash' => function (ContainerInterface $container) { - return $container->get(FlashMessagesInterface::class); + return $container->get(FlashInterface::class); }, SessionMiddleware::class => function (ContainerInterface $container) { return new SessionMiddleware( diff --git a/app/src/Controller/HealthCheck.php b/app/src/Controller/HealthCheck.php index 5170d64..a8875f0 100644 --- a/app/src/Controller/HealthCheck.php +++ b/app/src/Controller/HealthCheck.php @@ -2,6 +2,7 @@ namespace Slovocast\Controller; +use Odan\Session\SessionInterface; use Slovocast\Controller\Controller; use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Psr\Http\Message\ResponseInterface as Response; @@ -9,7 +10,8 @@ use Psr\Http\Message\ResponseInterface as Response; class HealthCheck extends Controller { public function __construct( - protected DatabaseHandlerInterface $database + protected DatabaseHandlerInterface $database, + protected SessionInterface $session ) { } public function handle(): Response @@ -17,9 +19,16 @@ class HealthCheck extends Controller $statement = $this->database->getConnection()->query('SELECT 1'); $results = $statement->fetchAll(); + if (!$this->session->has("healthcheck")) { + $this->session->set("healthcheck", true); + } + + $activeSession = $this->session->get("healthcheck"); + return $this->json([ 'http' => true, 'database' => (bool) count($results), + 'active_session' => $activeSession, ]); } } diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php index 2142923..649c04d 100644 --- a/app/src/Controller/User/LoginUserAction.php +++ b/app/src/Controller/User/LoginUserAction.php @@ -2,6 +2,7 @@ namespace Slovocast\Controller\User; +use Odan\Session\FlashInterface; use Odan\Session\SessionInterface; use Psr\Http\Message\ResponseInterface as Response; use Psr\Log\LoggerInterface; @@ -16,6 +17,7 @@ class LoginUserAction extends Controller private UserAuthorizationInterface $auth, private UserRepositoryInterface $userRepository, private SessionInterface $session, + private FlashInterface $flash, private LoggerInterface $logger, ) { } @@ -27,24 +29,20 @@ class LoginUserAction extends Controller $user = $this->userRepository->getFromEmail($credentials['email']); } catch (EntityNotFoundException $e) { $this->logger->error("Unable to login user."); - $this->session - ->getFlash() - ->add('error', "Unable to login user."); + $this->flash->add('error', "Unable to login user."); return $this->render('user/login.twig')->withStatus(400); } if (!$this->auth->verify($credentials['password'], $user->getPassword())) { $this->logger->error("Unable to verify user password."); - $this->session - ->getFlash() - ->add('error', "Unable to login user."); + $this->flash->add('error', "Unable to login user."); return $this->render('user/login.twig')->withStatus(400); } // start the session - $this->session->getFlash()->add('success', "Successfully logged in."); + $this->flash->add('success', "Successfully logged in."); $this->session->set('authenticated', true); $this->session->set('user', $user->toArray()); return $this->redirect('/dashboard', 302); diff --git a/app/src/Controller/User/LogoutUserAction.php b/app/src/Controller/User/LogoutUserAction.php index f114bc6..2530cae 100644 --- a/app/src/Controller/User/LogoutUserAction.php +++ b/app/src/Controller/User/LogoutUserAction.php @@ -2,6 +2,7 @@ namespace Slovocast\Controller\User; +use Odan\Session\FlashInterface; use Slovocast\Controller\Controller; use Psr\Http\Message\ResponseInterface as Response; use Odan\Session\SessionInterface; @@ -9,7 +10,8 @@ use Odan\Session\SessionInterface; class LogoutUserAction extends Controller { public function __construct( - protected SessionInterface $session + protected SessionInterface $session, + protected FlashInterface $flash ) { } @@ -23,7 +25,7 @@ class LogoutUserAction extends Controller $this->session->delete('user'); } - $this->session->getFlash()->add('notice', "Successfully logged out."); + $this->flash->add('notice', "Successfully logged out."); return $this->redirect('/', 302); } } diff --git a/app/src/Controller/User/RegisterUserAction.php b/app/src/Controller/User/RegisterUserAction.php index b408c38..5de3656 100644 --- a/app/src/Controller/User/RegisterUserAction.php +++ b/app/src/Controller/User/RegisterUserAction.php @@ -29,9 +29,7 @@ class RegisterUserAction extends Controller if ($success) { return $this->render('user/success.twig'); } else { - $this->session - ->getFlash() - ->add('error', "Unable to register user."); + $this->flash->add('error', "Unable to register user."); return $this->render('user/register.twig')->withStatus(400); } } diff --git a/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php b/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php index e1b9f2e..4555033 100644 --- a/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php +++ b/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php @@ -25,7 +25,7 @@ interface FlashMessagesInterface /** * @return array> */ - public function getAll(): array; + public function all(): array; /** * @param string|null diff --git a/app/src/Infrastructure/Session/FlashMessages.php b/app/src/Infrastructure/Session/FlashMessages.php index d180207..619f447 100644 --- a/app/src/Infrastructure/Session/FlashMessages.php +++ b/app/src/Infrastructure/Session/FlashMessages.php @@ -3,8 +3,9 @@ namespace Slovocast\Infrastructure\Session; use Odan\Session\SessionInterface; +use Odan\Session\FlashInterface; -class FlashMessages implements FlashMessagesInterface +class FlashMessages implements FlashInterface { const LEVEL_ERROR = "error"; const LEVEL_NOTICE = "notice"; @@ -15,9 +16,7 @@ class FlashMessages implements FlashMessagesInterface public function __construct( protected SessionInterface $session ) { - if (!$this->session->has($this->key)) { - $this->session->set($this->key, []); - } + $this->session->set($this->key, []); } /** @@ -36,11 +35,23 @@ class FlashMessages implements FlashMessagesInterface { $this->session->set($this->key, $messages); } + + public function has(string $key): bool + { + return array_key_exists($key, $this->getMessages()); + } - public function add(string $level, string $message): void + public function set(string $key, array $messages): void { $messages = $this->getMessages(); - $messages[$level][] = $message; + $messages[$key] = $messages; + $this->saveMessages($messages); + } + + public function add(string $key, string $message): void + { + $messages = $this->getMessages(); + $messages[$key][] = $message; $this->saveMessages($messages); } @@ -58,25 +69,15 @@ class FlashMessages implements FlashMessagesInterface return $buffer; } - public function getAll(): array + public function all(): array { $messages = $this->getMessages(); $this->clear(); return $messages; } - public function clear(?string $level = null): void + public function clear(): void { - if (!$level) { - $this->session->set($this->key, []); - } else { - $messages = $this->getMessages(); - - if (array_key_exists($level)) { - unset($messages[$level]); - } - - $this->saveMessages($messages); - } + $this->session->set($this->key, []); } } diff --git a/app/src/Middlewares.php b/app/src/Middlewares.php index 8796530..1f3228d 100644 --- a/app/src/Middlewares.php +++ b/app/src/Middlewares.php @@ -3,6 +3,7 @@ namespace Slovocast; use League\Config\Configuration; +use Odan\Session\FlashInterface; use Odan\Session\SessionInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -10,7 +11,6 @@ use Psr\Container\NotFoundExceptionInterface; use Slim\App; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; -use Slovocast\Infrastructure\Session\FlashMessagesInterface; use Slovocast\Middleware\SessionMiddleware; use Twig\Error\LoaderError; @@ -48,7 +48,7 @@ class Middlewares $twig->getEnvironment()->addGlobal('site_description', $config->get('site.description')); $session = $container->get(SessionInterface::class); - $flash = $container->get(FlashMessagesInterface::class); + $flash = $container->get(FlashInterface::class); $twig->getEnvironment()->addGlobal('session', $session); $twig->getEnvironment()->addGlobal('flash', $flash); diff --git a/app/templates/channel/create.twig b/app/templates/channel/create.twig index 7cbc00f..12f9dee 100644 --- a/app/templates/channel/create.twig +++ b/app/templates/channel/create.twig @@ -14,6 +14,7 @@