|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Light\Blog\Handler; |
| 6 | + |
| 7 | +use Laminas\Diactoros\Response\HtmlResponse; |
| 8 | +use Light\App\Helper\Paginator; |
| 9 | +use Light\Blog\Repository\AuthorRepository; |
| 10 | +use Light\Blog\Repository\CategoryRepository; |
| 11 | +use Light\Blog\Repository\PostRepository; |
| 12 | +use Mezzio\Template\TemplateRendererInterface; |
| 13 | +use Psr\Http\Message\ResponseInterface; |
| 14 | +use Psr\Http\Message\ServerRequestInterface; |
| 15 | +use Psr\Http\Server\RequestHandlerInterface; |
| 16 | + |
| 17 | +class GetAuthorResourceHandler implements RequestHandlerInterface |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + protected TemplateRendererInterface $template, |
| 21 | + protected AuthorRepository $authorRepository, |
| 22 | + protected PostRepository $postRepository, |
| 23 | + protected CategoryRepository $categoryRepository, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 28 | + { |
| 29 | + $authorSlug = $request->getAttribute('slug'); |
| 30 | + $author = $this->authorRepository->getAuthorResource($authorSlug); |
| 31 | + $categories = $this->categoryRepository->getCategories(); |
| 32 | + if (! $author) { |
| 33 | + return new HtmlResponse('Author not found', 404); |
| 34 | + } |
| 35 | + |
| 36 | + $queryParams = $request->getQueryParams(); |
| 37 | + $params = Paginator::getParams($queryParams, 'posts.postDate'); |
| 38 | + $data = Paginator::wrapper( |
| 39 | + $this->postRepository->getArticleByAuthor($author, $params), |
| 40 | + $params |
| 41 | + ); |
| 42 | + $authorPost = $this->postRepository->getArticleByAuthor($author, $params); |
| 43 | + |
| 44 | + return new HtmlResponse( |
| 45 | + $this->template->render('page::author-resource', [ |
| 46 | + 'author' => $author, |
| 47 | + 'categories' => $categories, |
| 48 | + 'data' => $data, |
| 49 | + ]) |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
0 commit comments