diff --git a/phpstan.neon b/phpstan.neon index 6f1a10d..6c7ce11 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,6 +10,10 @@ parameters: # Test dependencies - 'tests/Application/**/*' + # Menu Provider + - 'src/Menu/BlogUrlProvider.php' + - 'src/Menu/CaseStudyUrlProvider.php' + ignoreErrors: # Replace checkMissingIterableValueType parameter to avoid deprecated warning - diff --git a/src/Menu/BlogUrlProvider.php b/src/Menu/BlogUrlProvider.php new file mode 100644 index 0000000..6b033d9 --- /dev/null +++ b/src/Menu/BlogUrlProvider.php @@ -0,0 +1,68 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusBlogPlugin\Menu; + +use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface; +use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface; +use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider; +use Symfony\Component\Routing\RouterInterface; +use Webmozart\Assert\Assert; + +class BlogUrlProvider extends AbstractUrlProvider +{ + public const PROVIDER_CODE = 'blog'; + + protected string $code = self::PROVIDER_CODE; + + protected string $icon = 'newspaper'; + + protected int $priority = 30; + + public function __construct( + RouterInterface $router, + private ArticleRepositoryInterface $articleRepository, + ) { + parent::__construct($router); + } + + protected function getResults(string $locale, string $search = ''): iterable + { + $queryBuilder = $this->articleRepository->createListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE) + ->andWhere('ba.enabled = true') + ->andWhere('ba.state = :state') + ->setParameter('state', ArticleInterface::STATE_PUBLISHED) + ; + + if (!empty($search)) { + $queryBuilder + ->andWhere('translation.title LIKE :search OR translation.slug LIKE :search') + ->setParameter('search', '%' . $search . '%') + ; + } + + $queryBuilder->setMaxResults($this->getMaxResults()); + + /** @phpstan-ignore-next-line */ + return $queryBuilder->getQuery()->getResult(); + } + + protected function addItemFromResult(object $result, string $locale): void + { + Assert::isInstanceOf($result, ArticleInterface::class); + /** @var ArticleInterface $result */ + $result->setCurrentLocale($locale); + $this->addItem( + (string) $result->getTitle(), + $this->router->generate('monsieurbiz_blog_article_show', ['slug' => $result->getSlug(), '_locale' => $locale]) + ); + } +} diff --git a/src/Menu/CaseStudyUrlProvider.php b/src/Menu/CaseStudyUrlProvider.php new file mode 100644 index 0000000..77701b5 --- /dev/null +++ b/src/Menu/CaseStudyUrlProvider.php @@ -0,0 +1,68 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusBlogPlugin\Menu; + +use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface; +use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface; +use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider; +use Symfony\Component\Routing\RouterInterface; +use Webmozart\Assert\Assert; + +class CaseStudyUrlProvider extends AbstractUrlProvider +{ + public const PROVIDER_CODE = 'case_study'; + + protected string $code = self::PROVIDER_CODE; + + protected string $icon = 'crosshairs'; + + protected int $priority = 20; + + public function __construct( + RouterInterface $router, + private ArticleRepositoryInterface $articleRepository, + ) { + parent::__construct($router); + } + + protected function getResults(string $locale, string $search = ''): iterable + { + $queryBuilder = $this->articleRepository->createListQueryBuilderByType($locale, ArticleInterface::CASE_STUDY_TYPE) + ->andWhere('ba.enabled = true') + ->andWhere('ba.state = :state') + ->setParameter('state', ArticleInterface::STATE_PUBLISHED) + ; + + if (!empty($search)) { + $queryBuilder + ->andWhere('translation.title LIKE :search OR translation.slug LIKE :search') + ->setParameter('search', '%' . $search . '%') + ; + } + + $queryBuilder->setMaxResults($this->getMaxResults()); + + /** @phpstan-ignore-next-line */ + return $queryBuilder->getQuery()->getResult(); + } + + protected function addItemFromResult(object $result, string $locale): void + { + Assert::isInstanceOf($result, ArticleInterface::class); + /** @var ArticleInterface $result */ + $result->setCurrentLocale($locale); + $this->addItem( + (string) $result->getTitle(), + $this->router->generate('monsieurbiz_case_studies_article_show', ['slug' => $result->getSlug(), '_locale' => $locale]) + ); + } +}