Goal-Calibration/app/View/ViewController.php

58 lines
1.5 KiB
PHP

<?php
namespace App\View;
use App\Text\CreateTextDto;
use App\Text\TextRepository;
use App\Text\UseCases\CreateText;
use App\Text\UseCases\CreateTextRequest;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class ViewController
{
public function __construct(
private TextRepository $textRepository,
) {}
public function admin(Response $response): Response
{
$html = file_get_contents(__DIR__.'/../../views/templates/admin.php', true);
$response->getBody()->write($html);
return $response;
}
public function texts(Response $response): Response
{
$texts = $this->textRepository->getAll();
$textsList = '';
foreach ($texts as $text) {
$textsList .= '<li>' . htmlspecialchars($text->getName()) . '</li>';
}
$html = file_get_contents(__DIR__.'/../../views/templates/texts.php', true);
$html = str_replace('{{texts}}', $textsList, $html);
$response->getBody()->write($html);
return $response;
}
public function createText(
Request $request,
Response $response,
CreateText $createTextUseCase,
): Response {
$data = $request->getParsedBody();
$name = $data['name'] ?? '';
if (!empty($name)) {
$createTextUseCase->execute(new CreateTextRequest(
name: $name,
));
}
return $response->withHeader('Location', '/admin/texts')->withStatus(302);
}
}