Skip to content

Commit 4fe6507

Browse files
committed
TASK: introduce ProductsProvider class and extend filter logic
1 parent 01cc2ef commit 4fe6507

File tree

6 files changed

+102
-67
lines changed

6 files changed

+102
-67
lines changed

config/services/renderer.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<services>
55
<defaults public="true" />
66

7+
<service id="sylius_cms.products_provider" class="Sylius\CmsPlugin\Provider\ProductsProvider">
8+
<argument type="service" id="sylius.repository.product" />
9+
<argument type="service" id="sylius.repository.taxon" />
10+
<argument type="service" id="sylius.context.channel" />
11+
</service>
12+
713
<service id="sylius_cms.content_element_renderer_strategy" class="Sylius\CmsPlugin\Renderer\ContentElementRendererStrategy">
814
<argument type="service" id="sylius_cms.twig.parser.content" />
915
<argument type="service" id="sylius.context.locale" />
@@ -45,7 +51,7 @@
4551
</service>
4652

4753
<service id="sylius_cms.content_element.products_carousel" class="Sylius\CmsPlugin\Renderer\ContentElement\ProductsCarouselContentElementRenderer">
48-
<argument type="service" id="sylius.repository.product" />
54+
<argument type="service" id="sylius_cms.products_provider" />
4955
<tag
5056
name="sylius_cms.content_element"
5157
template="@SyliusCmsPlugin/shop/content_element/elements/products_carousel.html.twig"
@@ -54,16 +60,15 @@
5460
</service>
5561

5662
<service id="sylius_cms.content_element.products_carousel_by_taxon" class="Sylius\CmsPlugin\Renderer\ContentElement\ProductsCarouselByTaxonContentElementRenderer">
57-
<argument type="service" id="sylius.repository.product" />
58-
<argument type="service" id="sylius.repository.taxon" />
63+
<argument type="service" id="sylius_cms.products_provider" />
5964
<tag
6065
name="sylius_cms.content_element"
6166
template="@SyliusCmsPlugin/shop/content_element/elements/products_carousel.html.twig"
6267
/>
6368
</service>
6469

6570
<service id="sylius_cms.content_element.products_grid" class="Sylius\CmsPlugin\Renderer\ContentElement\ProductsGridContentElementRenderer">
66-
<argument type="service" id="sylius.repository.product" />
71+
<argument type="service" id="sylius_cms.products_provider" />
6772
<tag
6873
name="sylius_cms.content_element"
6974
template="@SyliusCmsPlugin/shop/content_element/elements/products_grid.html.twig"
@@ -72,8 +77,7 @@
7277
</service>
7378

7479
<service id="sylius_cms.content_element.products_grid_by_taxon" class="Sylius\CmsPlugin\Renderer\ContentElement\ProductsGridByTaxonContentElementRenderer">
75-
<argument type="service" id="sylius.repository.product" />
76-
<argument type="service" id="sylius.repository.taxon" />
80+
<argument type="service" id="sylius_cms.products_provider" />
7781
<tag
7882
name="sylius_cms.content_element"
7983
template="@SyliusCmsPlugin/shop/content_element/elements/products_grid.html.twig"

src/Provider/ProductsProvider.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius CMS Plugin package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\CmsPlugin\Provider;
15+
16+
use Sylius\Component\Channel\Context\ChannelContextInterface;
17+
use Sylius\Component\Core\Model\ProductInterface;
18+
use Sylius\Component\Core\Model\TaxonInterface;
19+
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
20+
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
21+
22+
final class ProductsProvider
23+
{
24+
/**
25+
* @param ProductRepositoryInterface<ProductInterface> $productRepository
26+
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
27+
* @param ChannelContextInterface $channelContext
28+
*/
29+
public function __construct(
30+
private ProductRepositoryInterface $productRepository,
31+
private TaxonRepositoryInterface $taxonRepository,
32+
private ChannelContextInterface $channelContext,
33+
) {
34+
}
35+
36+
private function filterProducts(array $products): array
37+
{
38+
$currentChannel = $this->channelContext->getChannel();
39+
$filteredProducts = [];
40+
41+
foreach ($products as $product) {
42+
if (
43+
$product->isEnabled() &&
44+
$product->hasChannel($currentChannel)
45+
) {
46+
$filteredProducts[] = $product;
47+
}
48+
}
49+
50+
return $filteredProducts;
51+
}
52+
53+
public function getProductsByTaxonCode(string $taxonCode): array
54+
{
55+
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);
56+
if (null === $taxon) {
57+
return [];
58+
}
59+
60+
$products = $this->productRepository->findByTaxon($taxon);
61+
return $this->filterProducts($products);
62+
}
63+
64+
public function getProductsByCodes(array $productCodes): array
65+
{
66+
$products = $this->productRepository->findBy(['code' => $productCodes]);
67+
if (null === $products) {
68+
return [];
69+
}
70+
return $this->filterProducts($products);
71+
}
72+
}

src/Renderer/ContentElement/ProductsCarouselByTaxonContentElementRenderer.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515

1616
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
1717
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsCarouselByTaxonContentElementType;
18-
use Sylius\Component\Core\Model\ProductInterface;
19-
use Sylius\Component\Core\Model\TaxonInterface;
20-
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
21-
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
18+
use Sylius\CmsPlugin\Provider\ProductsProvider;
2219

2320
final class ProductsCarouselByTaxonContentElementRenderer extends AbstractContentElement
2421
{
2522
/**
26-
* @param ProductRepositoryInterface<ProductInterface> $productRepository
27-
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
23+
* @param ProductsProvider $productsProvider
2824
*/
2925
public function __construct(
30-
private ProductRepositoryInterface $productRepository,
31-
private TaxonRepositoryInterface $taxonRepository,
26+
private ProductsProvider $productsProvider,
3227
) {
3328
}
3429

@@ -41,21 +36,11 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
4136
{
4237
$taxonCode = $contentConfiguration->getConfiguration()['products_carousel_by_taxon'];
4338

44-
/** @var TaxonInterface|null $taxon */
45-
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);
46-
if (null === $taxon) {
47-
return '';
48-
}
49-
50-
$products = $this->productRepository->findByTaxon($taxon);
51-
52-
$enabledProducts = array_filter($products, function (ProductInterface $product): bool {
53-
return $product->isEnabled();
54-
});
39+
$products = $this->productsProvider->getProductsByTaxonCode($taxonCode);
5540

5641
return $this->twig->render('@SyliusCmsPlugin/shop/content_element/index.html.twig', [
5742
'content_element' => $this->template,
58-
'products' => $enabledProducts,
43+
'products' => $products,
5944
]);
6045
}
6146
}

src/Renderer/ContentElement/ProductsCarouselContentElementRenderer.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515

1616
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
1717
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsCarouselContentElementType;
18-
use Sylius\Component\Core\Model\ProductInterface;
19-
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
18+
use Sylius\CmsPlugin\Provider\ProductsProvider;
2019

2120
final class ProductsCarouselContentElementRenderer extends AbstractContentElement
2221
{
23-
/** @param ProductRepositoryInterface<ProductInterface> $productRepository */
22+
/** @param ProductsProvider $productsProvider */
2423
public function __construct(
25-
private ProductRepositoryInterface $productRepository,
24+
private ProductsProvider $productsProvider,
2625
) {
2726
}
2827

@@ -35,18 +34,12 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
3534
{
3635
$configuration = $contentConfiguration->getConfiguration();
3736
$productsCodes = $configuration['products_carousel']['products'];
38-
$products = $this->productRepository->findBy(['code' => $productsCodes]);
39-
if ([] === $products) {
40-
return '';
41-
}
4237

43-
$enabledProducts = array_filter($products, function (ProductInterface $product): bool {
44-
return $product->isEnabled();
45-
});
38+
$products = $this->productsProvider->getProductsByCodes($productsCodes);
4639

4740
return $this->twig->render('@SyliusCmsPlugin/shop/content_element/index.html.twig', [
4841
'content_element' => $this->template,
49-
'products' => $enabledProducts,
42+
'products' => $products,
5043
]);
5144
}
5245
}

src/Renderer/ContentElement/ProductsGridByTaxonContentElementRenderer.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515

1616
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
1717
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsGridByTaxonContentElementType;
18-
use Sylius\Component\Core\Model\ProductInterface;
19-
use Sylius\Component\Core\Model\TaxonInterface;
20-
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
21-
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
18+
use Sylius\CmsPlugin\Provider\ProductsProvider;
2219

2320
final class ProductsGridByTaxonContentElementRenderer extends AbstractContentElement
2421
{
2522
/**
26-
* @param ProductRepositoryInterface<ProductInterface> $productRepository
27-
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
23+
* @param ProductsProvider $productsProvider
2824
*/
2925
public function __construct(
30-
private ProductRepositoryInterface $productRepository,
31-
private TaxonRepositoryInterface $taxonRepository,
26+
private ProductsProvider $productsProvider,
3227
) {
3328
}
3429

@@ -41,21 +36,11 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
4136
{
4237
$taxonCode = $contentConfiguration->getConfiguration()['products_grid_by_taxon'];
4338

44-
/** @var TaxonInterface|null $taxon */
45-
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);
46-
if (null === $taxon) {
47-
return '';
48-
}
49-
50-
$products = $this->productRepository->findByTaxon($taxon);
51-
52-
$enabledProducts = array_filter($products, function (ProductInterface $product): bool {
53-
return $product->isEnabled();
54-
});
39+
$products = $this->productsProvider->getProductsByTaxonCode($taxonCode);
5540

5641
return $this->twig->render('@SyliusCmsPlugin/shop/content_element/index.html.twig', [
5742
'content_element' => $this->template,
58-
'products' => $enabledProducts,
43+
'products' => $products,
5944
]);
6045
}
6146
}

src/Renderer/ContentElement/ProductsGridContentElementRenderer.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515

1616
use Sylius\CmsPlugin\Entity\ContentConfigurationInterface;
1717
use Sylius\CmsPlugin\Form\Type\ContentElements\ProductsGridContentElementType;
18-
use Sylius\Component\Core\Model\ProductInterface;
19-
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
18+
use Sylius\CmsPlugin\Provider\ProductsProvider;
2019

2120
final class ProductsGridContentElementRenderer extends AbstractContentElement
2221
{
23-
/** @param ProductRepositoryInterface<ProductInterface> $productRepository */
22+
/** @param ProductsProvider $productsProvider */
2423
public function __construct(
25-
private ProductRepositoryInterface $productRepository,
24+
private ProductsProvider $productsProvider,
2625
) {
2726
}
2827

@@ -35,15 +34,12 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
3534
{
3635
$configuration = $contentConfiguration->getConfiguration();
3736
$productsCodes = $configuration['products_grid']['products'];
38-
$products = $this->productRepository->findBy(['code' => $productsCodes]);
3937

40-
$enabledProducts = array_filter($products, function (ProductInterface $product): bool {
41-
return $product->isEnabled();
42-
});
38+
$products = $this->productsProvider->getProductsByCodes($productsCodes);
4339

4440
return $this->twig->render('@SyliusCmsPlugin/shop/content_element/index.html.twig', [
4541
'content_element' => $this->template,
46-
'products' => $enabledProducts,
42+
'products' => $products,
4743
]);
4844
}
4945
}

0 commit comments

Comments
 (0)