Add a page handler class.

This commit is contained in:
Dave Smith-Hayes 2025-04-23 20:45:33 -04:00
parent bec39c3c8b
commit 7e0285f9b5
7 changed files with 49 additions and 34 deletions

View File

@ -0,0 +1,7 @@
<?php
namespace Slovocast\Exception;
use Exception;
class TemplateNotSetException extends Exception { }

View File

@ -2,14 +2,9 @@
namespace Slovocast\Handler\Channel\Page; namespace Slovocast\Handler\Channel\Page;
use Odan\Session\SessionInterface; use Slovocast\Handler\Page;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Handler\Handler;
class CreateChannelPage extends Handler class CreateChannelPage extends Page
{ {
public function handle(): Response protected string $template = "channel/create.twig";
{
return $this->render('channel/create.twig');
}
} }

24
app/src/Handler/Page.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Slovocast\Handler;
use Psr\Http\Message\ResponseInterface;
use Slovocast\Exception\TemplateNotSetException;
abstract class Page extends Handler
{
protected string $template = '';
protected array $pageProps = [];
/**
* @throws TemplateNotSetException
*/
public function handle(): ResponseInterface
{
if (!$this->template) {
throw new TemplateNotSetException("No template set.");
}
return $this->render($this->template, $this->pageProps);
}
}

View File

@ -8,10 +8,12 @@ use Psr\Log\LoggerInterface;
use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Slovocast\Domain\Repository\User\UserRepositoryInterface;
use Slovocast\Exception\EntityNotFoundException; use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Handler\Handler; use Slovocast\Handler\Page;
class DashboardPage extends Handler class DashboardPage extends Page
{ {
protected string $template = "dashboard";
public function __construct( public function __construct(
protected UserRepositoryInterface $userRepository, protected UserRepositoryInterface $userRepository,
protected ChannelRepositoryInterface $channelRepository, protected ChannelRepositoryInterface $channelRepository,
@ -33,6 +35,7 @@ class DashboardPage extends Handler
$channel = null; $channel = null;
} }
return $this->render('dashboard.twig', [ 'channel' => $channel ]); $this->pageProps = [ 'channel' => $channel ];
return parent::handle();
} }
} }

View File

@ -2,13 +2,9 @@
namespace Slovocast\Handler\Page; namespace Slovocast\Handler\Page;
use Psr\Http\Message\ResponseInterface as Response; use Slovocast\Handler\Page;
use Slovocast\Handler\Handler;
class HomePage extends Handler class HomePage extends Page
{ {
public function handle(): Response protected string $template = "home.twig";
{
return $this->render('home.twig');
}
} }

View File

@ -2,14 +2,9 @@
namespace Slovocast\Handler\User\Page; namespace Slovocast\Handler\User\Page;
use Odan\Session\SessionInterface; use Slovocast\Handler\Page;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Handler\Handler;
class LoginUserPage extends Handler class LoginUserPage extends Page
{ {
public function handle(): Response protected string $template = "user/login.twig";
{
return $this->render('user/login.twig');
}
} }

View File

@ -2,14 +2,9 @@
namespace Slovocast\Handler\User\Page; namespace Slovocast\Handler\User\Page;
use Odan\Session\SessionInterface; use Slovocast\Handler\Page;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Handler\Handler;
class RegisterUserPage extends Handler class RegisterUserPage extends Page
{ {
public function handle(): Response protected string $template = "user/register.twig";
{
return $this->render('user/register.twig');
}
} }