Skip to content

Commit ab08d61

Browse files
committed
Issue 23 code improvement + add 404 if something happens and cannot generate feed
1 parent 7e4e698 commit ab08d61

8 files changed

Lines changed: 47 additions & 11 deletions

File tree

bin/generate-feed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ printf(
2020
$count === 1 ? '' : 's',
2121
$feedGenerator->getFeedFile(),
2222
PHP_EOL
23-
);
23+
);

config/autoload/app.global.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
return [
2424
'app' => $app,
2525
'feed' => [
26-
'file' => 'public/feed.xml',
26+
'path' => realpath(__DIR__ . '/../../public/feed.xml'),
2727
],
2828
'twig' => [
2929
'globals' => [

src/App/src/Factory/FeedGeneratorFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __invoke(ContainerInterface $container): FeedGenerator
2222

2323
return new FeedGenerator(
2424
$postRepository,
25-
$config['feed']['file'],
25+
$config['feed']['path'],
2626
rtrim($config['application']['url'] ?? '', '/') . '/',
2727
$config['app']['meta']['title'] ?? '',
2828
$config['app']['meta']['description'] ?? '',

src/App/src/Factory/GetFeedViewHandlerFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Light\App\Handler\GetFeedViewHandler;
88
use Light\App\Service\FeedGenerator;
9+
use Light\Blog\Repository\CategoryRepository;
10+
use Mezzio\Template\TemplateRendererInterface;
911
use Psr\Container\ContainerInterface;
1012

1113
use function assert;
@@ -17,9 +19,15 @@ class GetFeedViewHandlerFactory
1719
*/
1820
public function __invoke(ContainerInterface $container, string $requestedName): GetFeedViewHandler
1921
{
22+
$template = $container->get(TemplateRendererInterface::class);
23+
assert($template instanceof TemplateRendererInterface);
24+
25+
$categoryRepository = $container->get(CategoryRepository::class);
26+
assert($categoryRepository instanceof CategoryRepository);
27+
2028
$feedGenerator = $container->get(FeedGenerator::class);
2129
assert($feedGenerator instanceof FeedGenerator);
2230

23-
return new GetFeedViewHandler($feedGenerator);
31+
return new GetFeedViewHandler($template, $categoryRepository, $feedGenerator);
2432
}
2533
}

src/App/src/Fixture/PostLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function load(ObjectManager $manager): void
119119
$article->setExcerpt($excerpt);
120120
$changed = true;
121121
}
122-
if ($article->getTldr() !== $tlDr) {
122+
if ($article->getTlDr() !== $tlDr) {
123123
$article->setTldr($tlDr);
124124
$changed = true;
125125
}

src/App/src/Handler/GetFeedViewHandler.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
namespace Light\App\Handler;
66

7+
use Fig\Http\Message\StatusCodeInterface;
8+
use Laminas\Diactoros\Response\HtmlResponse;
79
use Laminas\Diactoros\Response\XmlResponse;
810
use Light\App\Service\FeedGenerator;
11+
use Light\Blog\Entity\Category;
12+
use Light\Blog\Repository\CategoryRepository;
13+
use Mezzio\Template\TemplateRendererInterface;
914
use Psr\Http\Message\ResponseInterface;
1015
use Psr\Http\Message\ServerRequestInterface;
1116
use Psr\Http\Server\RequestHandlerInterface;
17+
use Throwable;
1218

1319
use function file_get_contents;
1420
use function filesize;
@@ -17,6 +23,8 @@
1723
class GetFeedViewHandler implements RequestHandlerInterface
1824
{
1925
public function __construct(
26+
private readonly TemplateRendererInterface $template,
27+
private readonly CategoryRepository $categoryRepository,
2028
private readonly FeedGenerator $feedGenerator,
2129
) {
2230
}
@@ -26,13 +34,33 @@ public function handle(ServerRequestInterface $request): ResponseInterface
2634
$feedFile = $this->feedGenerator->getFeedFile();
2735

2836
if (! is_file($feedFile) || filesize($feedFile) === 0) {
29-
$this->feedGenerator->write();
37+
try {
38+
$this->feedGenerator->write();
39+
} catch (Throwable) {
40+
}
41+
}
42+
43+
if (! is_file($feedFile) || filesize($feedFile) === 0) {
44+
return $this->notFound($this->categoryRepository->getCategories());
3045
}
3146

3247
return new XmlResponse(
3348
(string) file_get_contents($feedFile),
34-
200,
35-
['content-type' => FeedGenerator::CONTENT_TYPE]
49+
StatusCodeInterface::STATUS_OK,
50+
['Content-Type' => FeedGenerator::CONTENT_TYPE]
51+
);
52+
}
53+
54+
/**
55+
* @param Category[] $categories
56+
*/
57+
private function notFound(array $categories): HtmlResponse
58+
{
59+
return new HtmlResponse(
60+
$this->template->render('error::404', [
61+
'categories' => $categories,
62+
]),
63+
StatusCodeInterface::STATUS_NOT_FOUND
3664
);
3765
}
3866
}

src/App/src/Service/FeedGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function write(): int
6262

6363
$this->appendText($dom, $item, 'title', $post->getTitle());
6464
$this->appendText($dom, $item, 'link', $link);
65-
$this->appendText($dom, $item, 'description', $post->getTldr() ?? $post->getExcerpt());
65+
$this->appendText($dom, $item, 'description', $post->getTlDr() ?? $post->getExcerpt());
6666
$this->appendText($dom, $item, 'pubDate', $post->getPostDate()->format(DateTimeInterface::RSS));
6767
$this->appendText(
6868
$dom,

src/Blog/src/Entity/Post.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ public function setExcerpt(string $excerpt): void
116116
$this->excerpt = $excerpt;
117117
}
118118

119-
public function getTldr(): ?string
119+
public function getTlDr(): ?string
120120
{
121121
return $this->tlDr;
122122
}
123123

124-
public function setTldr(?string $tlDr): void
124+
public function setTlDr(?string $tlDr): void
125125
{
126126
$this->tlDr = $tlDr;
127127
}

0 commit comments

Comments
 (0)