Skip to content

Commit 8be298d

Browse files
committed
Add blog and case study URL providers for menu
1 parent 867ec52 commit 8be298d

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ parameters:
1010
# Test dependencies
1111
- 'tests/Application/**/*'
1212

13+
# Menu Provider
14+
- 'src/Menu/BlogUrlProvider.php'
15+
- 'src/Menu/CaseStudyUrlProvider.php'
16+
1317
ignoreErrors:
1418
# Replace checkMissingIterableValueType parameter to avoid deprecated warning
1519
-

src/Menu/BlogUrlProvider.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz's Blog plugin for Sylius.
5+
* (c) Monsieur Biz <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MonsieurBiz\SyliusBlogPlugin\Menu;
13+
14+
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface;
15+
use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface;
16+
use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider;
17+
use Symfony\Component\Routing\RouterInterface;
18+
use Webmozart\Assert\Assert;
19+
20+
class BlogUrlProvider extends AbstractUrlProvider
21+
{
22+
public const PROVIDER_CODE = 'blog';
23+
24+
protected string $code = self::PROVIDER_CODE;
25+
26+
protected string $icon = 'newspaper';
27+
28+
protected int $priority = 30;
29+
30+
public function __construct(
31+
RouterInterface $router,
32+
private ArticleRepositoryInterface $articleRepository,
33+
) {
34+
parent::__construct($router);
35+
}
36+
37+
protected function getResults(string $locale, string $search = ''): iterable
38+
{
39+
$queryBuilder = $this->articleRepository->createListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE)
40+
->andWhere('ba.enabled = true')
41+
->andWhere('ba.state = :state')
42+
->setParameter('state', ArticleInterface::STATE_PUBLISHED)
43+
;
44+
45+
if (!empty($search)) {
46+
$queryBuilder
47+
->andWhere('translation.title LIKE :search OR translation.slug LIKE :search')
48+
->setParameter('search', '%' . $search . '%')
49+
;
50+
}
51+
52+
$queryBuilder->setMaxResults($this->getMaxResults());
53+
54+
/** @phpstan-ignore-next-line */
55+
return $queryBuilder->getQuery()->getResult();
56+
}
57+
58+
protected function addItemFromResult(object $result, string $locale): void
59+
{
60+
Assert::isInstanceOf($result, ArticleInterface::class);
61+
/** @var ArticleInterface $result */
62+
$result->setCurrentLocale($locale);
63+
$this->addItem(
64+
(string) $result->getTitle(),
65+
$this->router->generate('monsieurbiz_blog_article_show', ['slug' => $result->getSlug(), '_locale' => $locale])
66+
);
67+
}
68+
}

src/Menu/CaseStudyUrlProvider.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz's Blog plugin for Sylius.
5+
* (c) Monsieur Biz <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MonsieurBiz\SyliusBlogPlugin\Menu;
13+
14+
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface;
15+
use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface;
16+
use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider;
17+
use Symfony\Component\Routing\RouterInterface;
18+
use Webmozart\Assert\Assert;
19+
20+
class CaseStudyUrlProvider extends AbstractUrlProvider
21+
{
22+
public const PROVIDER_CODE = 'case_study';
23+
24+
protected string $code = self::PROVIDER_CODE;
25+
26+
protected string $icon = 'crosshairs';
27+
28+
protected int $priority = 20;
29+
30+
public function __construct(
31+
RouterInterface $router,
32+
private ArticleRepositoryInterface $articleRepository,
33+
) {
34+
parent::__construct($router);
35+
}
36+
37+
protected function getResults(string $locale, string $search = ''): iterable
38+
{
39+
$queryBuilder = $this->articleRepository->createListQueryBuilderByType($locale, ArticleInterface::CASE_STUDY_TYPE)
40+
->andWhere('ba.enabled = true')
41+
->andWhere('ba.state = :state')
42+
->setParameter('state', ArticleInterface::STATE_PUBLISHED)
43+
;
44+
45+
if (!empty($search)) {
46+
$queryBuilder
47+
->andWhere('translation.title LIKE :search OR translation.slug LIKE :search')
48+
->setParameter('search', '%' . $search . '%')
49+
;
50+
}
51+
52+
$queryBuilder->setMaxResults($this->getMaxResults());
53+
54+
/** @phpstan-ignore-next-line */
55+
return $queryBuilder->getQuery()->getResult();
56+
}
57+
58+
protected function addItemFromResult(object $result, string $locale): void
59+
{
60+
Assert::isInstanceOf($result, ArticleInterface::class);
61+
/** @var ArticleInterface $result */
62+
$result->setCurrentLocale($locale);
63+
$this->addItem(
64+
(string) $result->getTitle(),
65+
$this->router->generate('monsieurbiz_case_studies_article_show', ['slug' => $result->getSlug(), '_locale' => $locale])
66+
);
67+
}
68+
}

0 commit comments

Comments
 (0)