Skip to content

Commit 34ad5f0

Browse files
committed
Improve Code Quality
1 parent d6e9da6 commit 34ad5f0

File tree

11 files changed

+212
-52
lines changed

11 files changed

+212
-52
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusElasticsearchPlugin\EntityRepository;
14+
15+
use Doctrine\ORM\EntityRepository;
16+
use Sylius\Component\Core\Model\ProductVariantInterface;
17+
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface as BaseProductVariantRepositoryInterface;
18+
use Sylius\Component\Product\Model\ProductOptionValueInterface;
19+
20+
final class ProductVariantRepository implements ProductVariantRepositoryInterface
21+
{
22+
/**
23+
* @var BaseProductVariantRepositoryInterface|EntityRepository
24+
*/
25+
private $baseProductVariantRepository;
26+
27+
/**
28+
* @param BaseProductVariantRepositoryInterface|EntityRepository $baseProductVariantRepository
29+
*/
30+
public function __construct(BaseProductVariantRepositoryInterface $baseProductVariantRepository)
31+
{
32+
$this->baseProductVariantRepository = $baseProductVariantRepository;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ProductVariantInterface
39+
{
40+
$productVariant = $this->baseProductVariantRepository->createQueryBuilder('o')
41+
->where(':optionValue MEMBER OF o.optionValues')
42+
->setParameter('optionValue', $productOptionValue)
43+
->getQuery()
44+
->getOneOrNullResult()
45+
;
46+
47+
if (null === $productVariant) {
48+
throw new \UnexpectedValueException('Product variant was not found!');
49+
}
50+
51+
return $productVariant;
52+
}
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusElasticsearchPlugin\EntityRepository;
14+
15+
use Sylius\Component\Core\Model\ProductVariantInterface;
16+
use Sylius\Component\Product\Model\ProductOptionValueInterface;
17+
18+
interface ProductVariantRepositoryInterface
19+
{
20+
/**
21+
* @param ProductOptionValueInterface $productOptionValue
22+
*
23+
* @return ProductVariantInterface
24+
*/
25+
public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ProductVariantInterface;
26+
}

src/Form/Type/ChoiceMapper/ProductAttributesMapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public function __construct(
4444
ProductAttributeValueRepositoryInterface $productAttributeValueRepository,
4545
LocaleContextInterface $localeContext,
4646
StringFormatterInterface $stringFormatter
47-
)
48-
{
47+
) {
4948
$this->productAttributeValueRepository = $productAttributeValueRepository;
5049
$this->localeContext = $localeContext;
5150
$this->stringFormatter = $stringFormatter;

src/PropertyBuilder/AttributeBuilder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface;
1616
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
17-
use BitBag\SyliusElasticsearchPlugin\PropertyValueResolver\AttributeValueResolverInterface;
1817
use Elastica\Document;
1918
use FOS\ElasticaBundle\Event\TransformEvent;
2019
use Sylius\Component\Core\Model\ProductInterface;

src/PropertyBuilder/AttributeTaxonsBuilder.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@
1212

1313
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
1414

15+
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
1516
use FOS\ElasticaBundle\Event\TransformEvent;
1617
use Sylius\Component\Attribute\Model\AttributeInterface;
1718
use Sylius\Component\Core\Model\ProductInterface;
18-
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
19+
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
20+
use Sylius\Component\Product\Repository\ProductAttributeValueRepositoryInterface;
1921

2022
final class AttributeTaxonsBuilder extends AbstractBuilder
2123
{
2224
/**
23-
* @var ProductRepositoryInterface
25+
* @var ProductAttributeValueRepositoryInterface
2426
*/
25-
private $productRepository;
27+
private $productAttributeValueRepository;
28+
29+
/**
30+
* @var ProductTaxonsMapperInterface
31+
*/
32+
private $productTaxonsMapper;
2633

2734
/**
2835
* @var string
@@ -40,18 +47,21 @@ final class AttributeTaxonsBuilder extends AbstractBuilder
4047
private $excludedAttributes;
4148

4249
/**
43-
* @param ProductRepositoryInterface $productRepository
50+
* @param ProductAttributeValueRepositoryInterface $productAttributeValueRepository
51+
* @param ProductTaxonsMapperInterface $productTaxonsMapper
4452
* @param string $attributeProperty
4553
* @param string $taxonsProperty
4654
* @param array $excludedAttributes
4755
*/
4856
public function __construct(
49-
ProductRepositoryInterface $productRepository,
57+
ProductAttributeValueRepositoryInterface $productAttributeValueRepository,
58+
ProductTaxonsMapperInterface $productTaxonsMapper,
5059
string $attributeProperty,
5160
string $taxonsProperty,
5261
array $excludedAttributes = []
5362
) {
54-
$this->productRepository = $productRepository;
63+
$this->productAttributeValueRepository = $productAttributeValueRepository;
64+
$this->productTaxonsMapper = $productTaxonsMapper;
5565
$this->attributeProperty = $attributeProperty;
5666
$this->taxonsProperty = $taxonsProperty;
5767
$this->excludedAttributes = $excludedAttributes;
@@ -64,28 +74,22 @@ public function buildProperty(TransformEvent $event): void
6474
{
6575
$documentAttribute = $event->getObject();
6676

67-
if (
68-
!$documentAttribute instanceof AttributeInterface ||
69-
in_array($documentAttribute->getCode(), $this->excludedAttributes)
77+
if (!$documentAttribute instanceof AttributeInterface
78+
|| in_array($documentAttribute->getCode(), $this->excludedAttributes)
7079
) {
7180
return;
7281
}
7382

7483
$document = $event->getDocument();
75-
$products = $this->productRepository->findAll();
84+
$productAttributes = $this->productAttributeValueRepository->findAll();
7685
$taxons = [];
7786

78-
/** @var ProductInterface $product */
79-
foreach ($products as $product) {
80-
foreach ($product->getAttributes() as $attributeValue) {
81-
if ($documentAttribute === $attributeValue->getAttribute()) {
82-
foreach ($product->getTaxons() as $taxon) {
83-
$code = $taxon->getCode();
84-
if (!in_array($code, $taxons)) {
85-
$taxons[] = $code;
86-
}
87-
}
88-
}
87+
/** @var ProductAttributeValueInterface $attributeValue */
88+
foreach ($productAttributes as $attributeValue) {
89+
if ($documentAttribute === $attributeValue->getAttribute()) {
90+
/** @var ProductInterface $product */
91+
$product = $attributeValue->getProduct();
92+
$taxons = $this->productTaxonsMapper->mapToUniqueCodes($product);
8993
}
9094
}
9195

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper;
14+
15+
use Sylius\Component\Core\Model\ProductInterface;
16+
17+
final class ProductTaxonsMapper implements ProductTaxonsMapperInterface
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function mapToUniqueCodes(ProductInterface $product): array
23+
{
24+
$taxons = [];
25+
26+
foreach ($product->getTaxons() as $taxon) {
27+
$taxons[] = $taxon->getCode();
28+
}
29+
30+
return array_unique($taxons);
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper;
14+
15+
use Sylius\Component\Core\Model\ProductInterface;
16+
17+
interface ProductTaxonsMapperInterface
18+
{
19+
/**
20+
* @param ProductInterface $product
21+
*
22+
* @return array
23+
*/
24+
public function mapToUniqueCodes(ProductInterface $product): array;
25+
}

src/PropertyBuilder/OptionTaxonsBuilder.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,30 @@
1212

1313
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
1414

15+
use BitBag\SyliusElasticsearchPlugin\EntityRepository\ProductVariantRepositoryInterface;
16+
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
1517
use FOS\ElasticaBundle\Event\TransformEvent;
1618
use Sylius\Component\Core\Model\ProductInterface;
17-
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
1819
use Sylius\Component\Product\Model\ProductOptionInterface;
20+
use Sylius\Component\Product\Model\ProductOptionValueInterface;
21+
use Sylius\Component\Resource\Repository\RepositoryInterface;
1922

2023
final class OptionTaxonsBuilder extends AbstractBuilder
2124
{
2225
/**
23-
* @var ProductRepositoryInterface
26+
* @var RepositoryInterface
2427
*/
25-
private $productRepository;
28+
private $productOptionValueRepository;
29+
30+
/**
31+
* @var ProductVariantRepositoryInterface
32+
*/
33+
private $productVariantRepository;
34+
35+
/**
36+
* @var ProductTaxonsMapperInterface
37+
*/
38+
private $productTaxonsMapper;
2639

2740
/**
2841
* @var string
@@ -40,18 +53,24 @@ final class OptionTaxonsBuilder extends AbstractBuilder
4053
private $excludedOptions;
4154

4255
/**
43-
* @param ProductRepositoryInterface $productRepository
56+
* @param RepositoryInterface $productOptionValueRepository
57+
* @param ProductVariantRepositoryInterface $productVariantRepository
58+
* @param ProductTaxonsMapperInterface $productTaxonsMapper
4459
* @param string $optionProperty
4560
* @param string $taxonsProperty
4661
* @param array $excludedOptions
4762
*/
4863
public function __construct(
49-
ProductRepositoryInterface $productRepository,
64+
RepositoryInterface $productOptionValueRepository,
65+
ProductVariantRepositoryInterface $productVariantRepository,
66+
ProductTaxonsMapperInterface $productTaxonsMapper,
5067
string $optionProperty,
5168
string $taxonsProperty,
5269
array $excludedOptions = []
5370
) {
54-
$this->productRepository = $productRepository;
71+
$this->productOptionValueRepository = $productOptionValueRepository;
72+
$this->productVariantRepository = $productVariantRepository;
73+
$this->productTaxonsMapper = $productTaxonsMapper;
5574
$this->optionProperty = $optionProperty;
5675
$this->taxonsProperty = $taxonsProperty;
5776
$this->excludedOptions = $excludedOptions;
@@ -64,30 +83,23 @@ public function buildProperty(TransformEvent $event): void
6483
{
6584
$documentProductOption = $event->getObject();
6685

67-
if (
68-
!$documentProductOption instanceof ProductOptionInterface ||
69-
in_array($documentProductOption->getCode(), $this->excludedOptions)
86+
if (!$documentProductOption instanceof ProductOptionInterface
87+
|| in_array($documentProductOption->getCode(), $this->excludedOptions)
7088
) {
7189
return;
7290
}
7391

7492
$document = $event->getDocument();
75-
$products = $this->productRepository->findAll();
93+
$optionValues = $this->productOptionValueRepository->findAll();
7694
$taxons = [];
7795

78-
/** @var ProductInterface $product */
79-
foreach ($products as $product) {
80-
foreach ($product->getVariants() as $productVariant) {
81-
foreach ($productVariant->getOptionValues() as $productOptionValue) {
82-
if ($documentProductOption === $productOptionValue->getOption()) {
83-
foreach ($product->getTaxons() as $taxon) {
84-
$code = $taxon->getCode();
85-
if (!in_array($code, $taxons)) {
86-
$taxons[] = $code;
87-
}
88-
}
89-
}
90-
}
96+
/** @var ProductOptionValueInterface $optionValue */
97+
foreach ($optionValues as $optionValue) {
98+
$option = $optionValue->getOption();
99+
if ($documentProductOption === $option) {
100+
/** @var ProductInterface $product */
101+
$product = $this->productVariantRepository->findOneByOptionValue($optionValue)->getProduct();
102+
$taxons = $this->productTaxonsMapper->mapToUniqueCodes($product);
91103
}
92104
}
93105

src/QueryBuilder/HasPriceBetweenQueryBuilder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public function __construct(
4444
PriceNameResolverInterface $priceNameResolver,
4545
ConcatedNameResolverInterface $channelPricingNameResolver,
4646
ChannelContextInterface $channelContext
47-
)
48-
{
47+
) {
4948
$this->channelPricingNameResolver = $channelPricingNameResolver;
5049
$this->priceNameResolver = $priceNameResolver;
5150
$this->channelContext = $channelContext;
@@ -59,7 +58,7 @@ public function buildQuery(array $data): ?AbstractQuery
5958
$dataMinPrice = $data[$this->priceNameResolver->resolveMinPriceName()];
6059
$dataMaxPrice = $data[$this->priceNameResolver->resolveMaxPriceName()];
6160

62-
$minPrice = $dataMinPrice ? $this->getPriceFromString($dataMinPrice) : 0;
61+
$minPrice = $dataMinPrice ? $this->getPriceFromString($dataMinPrice) : 0;
6362
$maxPrice = $dataMaxPrice ? $this->getPriceFromString($dataMaxPrice) : PHP_INT_MAX;
6463

6564
$channelCode = $this->channelContext->getChannel()->getCode();

src/Resources/config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ services:
1515
class: BitBag\SyliusElasticsearchPlugin\Twig\Extension\UnsetArrayElementsExtension
1616
tags:
1717
- { name: twig.extension }
18+
19+
bitbag.sylius_elasticsearch_plugin.entity_repository.product_variant:
20+
class: BitBag\SyliusElasticsearchPlugin\EntityRepository\ProductVariantRepository
21+
arguments:
22+
- "@sylius.repository.product_variant"

0 commit comments

Comments
 (0)