77 lines
1.8 KiB
PHP
77 lines
1.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\UrlGenerator;
|
||
|
|
||
|
use App\Entity\Blog\Category;
|
||
|
use App\Repository\Blog\CategoryRepositoryQuery;
|
||
|
use App\Repository\Blog\PostRepositoryQuery;
|
||
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||
|
|
||
|
/**
|
||
|
* class PostGenerator.
|
||
|
*
|
||
|
* @author Simon Vieille <simon@deblan.fr>
|
||
|
*/
|
||
|
class PostGenerator
|
||
|
{
|
||
|
protected PostRepositoryQuery $postQuery;
|
||
|
protected CategoryRepositoryQuery $categoryQuery;
|
||
|
protected UrlGeneratorInterface $urlGenerator;
|
||
|
|
||
|
public function __construct(
|
||
|
PostRepositoryQuery $postQuery,
|
||
|
CategoryRepositoryQuery $categoryQuery,
|
||
|
UrlGeneratorInterface $urlGenerator
|
||
|
) {
|
||
|
$this->postQuery = $postQuery;
|
||
|
$this->categoryQuery = $categoryQuery;
|
||
|
$this->urlGenerator = $urlGenerator;
|
||
|
}
|
||
|
|
||
|
public function post(): array
|
||
|
{
|
||
|
$entities = $this->postQuery->create()
|
||
|
->published()
|
||
|
->find()
|
||
|
;
|
||
|
|
||
|
$urls = [];
|
||
|
|
||
|
foreach ($entities as $entity) {
|
||
|
$urls[] = $this->urlGenerator->generate(
|
||
|
'blog_menu_post',
|
||
|
[
|
||
|
'post' => $entity->getId(),
|
||
|
'slug' => $entity->getSlug(),
|
||
|
],
|
||
|
UrlGeneratorInterface::ABSOLUTE_URL
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return $urls;
|
||
|
}
|
||
|
|
||
|
public function category(): array
|
||
|
{
|
||
|
$entities = $this->categoryQuery->create()
|
||
|
->andWhere('.isActive = 1')
|
||
|
->find()
|
||
|
;
|
||
|
|
||
|
$urls = [];
|
||
|
|
||
|
foreach ($entities as $entity) {
|
||
|
$urls[] = $this->urlGenerator->generate(
|
||
|
'blog_menu_category',
|
||
|
[
|
||
|
'category' => $entity->getId(),
|
||
|
'slug' => $entity->getSlug(),
|
||
|
],
|
||
|
UrlGeneratorInterface::ABSOLUTE_URL
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return $urls;
|
||
|
}
|
||
|
}
|