Skip to content

Commit

Permalink
Add blog and case study URL providers for menu
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Aug 23, 2024
1 parent 867ec52 commit 8be298d
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down
68 changes: 68 additions & 0 deletions src/Menu/BlogUrlProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <sylius@monsieurbiz.com>
* 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])
);
}
}
68 changes: 68 additions & 0 deletions src/Menu/CaseStudyUrlProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <sylius@monsieurbiz.com>
* 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])
);
}
}

0 comments on commit 8be298d

Please sign in to comment.