baleen/src/Controllers/FeedController.php
2024-12-31 16:32:00 +00:00

69 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Lewisdale\App\Controllers;
use Doctrine\Persistence\ObjectRepository;
use Lewisdale\App\Models\Repositories\FeedRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Slim\Views\Twig;
class FeedController
{
public function __construct(
private readonly Twig $view,
private readonly LoggerInterface $logger,
private readonly FeedRepository $feedRepository
) {}
public function get(ServerRequestInterface $request, ResponseInterface $response)
{
$this->logger->info("FeedController::get() called");
$feeds = $this->feedRepository->findBy([], ['title' => 'ASC']);
return $this->view->render($response, 'feed/index.twig.html', ['feeds' => $feeds]);
}
public function get_feed(ServerRequestInterface $request, ResponseInterface $response)
{
$this->logger->info("FeedController::get_feed() called");
$feed = $this->feedRepository->find($request->getAttribute('id'));
if (empty($feed)) {
return $response->withStatus(404);
}
$filtered = $feed->get_filtered_feed();
$body = $response->getBody();
$body->write($filtered->asXML());
$this->feedRepository->save($feed);
return $response->withHeader('Content-Type', 'application/rss+xml')
->withBody($body)
->withStatus(200);
}
public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$this->logger->info("FeedController::create() called");
$body = $request->getParsedBody();
return $this->view->render($response, 'create.twig.html');
}
public function delete(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$this->logger->info("FeedController::delete() called");
$feed = $this->feedRepository->find($request->getAttribute('id'));
if (empty($feed)) {
return $response->withStatus(404);
}
$this->feedRepository->delete($feed);
return $response->withStatus(201)->withHeader('Location', '/feed');
}
}