Skip to content

Commit c3773ff

Browse files
committed
Simplify redundant code
1 parent 1a7a285 commit c3773ff

16 files changed

+116
-122
lines changed

src/Form/Type/ChoiceMapper/ProductAttributesMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function mapToChoices(ProductAttributeInterface $productAttribute): array
5757
{
5858
$attributeValues = $this->productAttributeValueRepository->findBy(['attribute' => $productAttribute]);
5959
$choices = [];
60-
array_walk($attributeValues, function (ProductAttributeValueInterface $productAttributeValue) use (&$choices) {
60+
array_walk($attributeValues, function (ProductAttributeValueInterface $productAttributeValue) use (&$choices): void {
6161
$value = $productAttributeValue->getValue();
6262
$configuration = $productAttributeValue->getAttribute()->getConfiguration();
6363
if (is_array($value)

src/Form/Type/ChoiceMapper/ProductOptionsMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function mapToChoices(ProductOptionInterface $productOption): array
3939
$productOptionValues = $productOption->getValues()->toArray();
4040
$choices = [];
4141

42-
array_walk($productOptionValues, function (ProductOptionValueInterface $productOptionValue) use (&$choices) {
42+
array_walk($productOptionValues, function (ProductOptionValueInterface $productOptionValue) use (&$choices): void {
4343
$value = $productOptionValue->getValue();
4444
$choices[$value] = $this->stringFormatter->formatToLowercaseWithoutSpaces($value);
4545
});

src/PropertyBuilder/AbstractBuilder.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,29 @@
1616

1717
abstract class AbstractBuilder implements PropertyBuilderInterface
1818
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function buildProperty(TransformEvent $event, string $supportedModelClass, callable $callback): void
23+
{
24+
$model = $event->getObject();
25+
26+
if (!$model instanceof $supportedModelClass) {
27+
return;
28+
}
29+
30+
$document = $event->getDocument();
31+
32+
$callback($model, $document);
33+
}
34+
1935
/**
2036
* {@inheritdoc}
2137
*/
2238
public static function getSubscribedEvents(): array
2339
{
2440
return [
25-
TransformEvent::POST_TRANSFORM => 'buildProperty',
41+
TransformEvent::POST_TRANSFORM => 'consumeEvent',
2642
];
2743
}
2844
}

src/PropertyBuilder/AttributeBuilder.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,21 @@ final class AttributeBuilder extends AbstractBuilder
3737
public function __construct(
3838
ConcatedNameResolverInterface $attributeNameResolver,
3939
StringFormatterInterface $stringFormatter
40-
) {
40+
)
41+
{
4142
$this->attributeNameResolver = $attributeNameResolver;
4243
$this->stringFormatter = $stringFormatter;
4344
}
4445

4546
/**
4647
* @param TransformEvent $event
4748
*/
48-
public function buildProperty(TransformEvent $event): void
49+
public function consumeEvent(TransformEvent $event): void
4950
{
50-
/** @var ProductInterface $product */
51-
$product = $event->getObject();
52-
53-
if (!$product instanceof ProductInterface) {
54-
return;
55-
}
56-
57-
$document = $event->getDocument();
58-
59-
$this->resolveProductAttributes($product, $document);
51+
$this->buildProperty($event, ProductInterface::class,
52+
function (ProductInterface $product, Document $document): void {
53+
$this->resolveProductAttributes($product, $document);
54+
});
6055
}
6156

6257
/**
@@ -73,7 +68,7 @@ private function resolveProductAttributes(ProductInterface $product, Document $d
7368

7469
if (is_array($value)) {
7570
foreach ($value as $singleElement) {
76-
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement);
71+
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string)$singleElement);
7772
}
7873
} else {
7974
$value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;

src/PropertyBuilder/AttributeTaxonsBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct(
7070
/**
7171
* @param TransformEvent $event
7272
*/
73-
public function buildProperty(TransformEvent $event): void
73+
public function consumeEvent(TransformEvent $event): void
7474
{
7575
$documentAttribute = $event->getObject();
7676

src/PropertyBuilder/ChannelPricingBuilder.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
1414

1515
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
16+
use Elastica\Document;
1617
use FOS\ElasticaBundle\Event\TransformEvent;
1718
use Sylius\Component\Core\Model\ProductInterface;
1819
use Sylius\Component\Core\Model\ProductVariantInterface;
@@ -35,22 +36,18 @@ public function __construct(ConcatedNameResolverInterface $channelPricingNameRes
3536
/**
3637
* {@inheritdoc}
3738
*/
38-
public function buildProperty(TransformEvent $event): void
39+
public function consumeEvent(TransformEvent $event): void
3940
{
40-
$product = $event->getObject();
41+
$this->buildProperty($event, ProductInterface::class,
42+
function (ProductInterface $product, Document $document): void {
43+
/** @var ProductVariantInterface $productVariant */
44+
$productVariant = $product->getVariants()->first();
4145

42-
if (!$product instanceof ProductInterface) {
43-
return;
44-
}
46+
foreach ($productVariant->getChannelPricings() as $channelPricing) {
47+
$propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelPricing->getChannelCode());
4548

46-
$document = $event->getDocument();
47-
/** @var ProductVariantInterface $productVariant */
48-
$productVariant = $product->getVariants()->first();
49-
50-
foreach ($productVariant->getChannelPricings() as $channelPricing) {
51-
$propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelPricing->getChannelCode());
52-
53-
$document->set($propertyName, $channelPricing->getPrice());
54-
}
49+
$document->set($propertyName, $channelPricing->getPrice());
50+
}
51+
});
5552
}
5653
}

src/PropertyBuilder/ChannelsBuilder.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
1414

15+
use Elastica\Document;
1516
use FOS\ElasticaBundle\Event\TransformEvent;
1617
use Sylius\Component\Core\Model\ProductInterface;
1718

@@ -33,21 +34,18 @@ public function __construct(string $channelsProperty)
3334
/**
3435
* @param TransformEvent $event
3536
*/
36-
public function buildProperty(TransformEvent $event): void
37+
public function consumeEvent(TransformEvent $event): void
3738
{
38-
$product = $event->getObject();
39+
$this->buildProperty($event, ProductInterface::class,
40+
function (ProductInterface $product, Document $document): void {
41+
$channels = [];
3942

40-
if (!$product instanceof ProductInterface) {
41-
return;
42-
}
43+
foreach ($product->getChannels() as $channel) {
44+
$channels[] = $channel->getCode();
45+
}
4346

44-
$channels = [];
45-
46-
/** @var ProductInterface $product */
47-
foreach ($product->getChannels() as $channel) {
48-
$channels[] = $channel->getCode();
49-
}
50-
51-
$event->getDocument()->set($this->channelsProperty, $channels);
47+
$document->set($this->channelsProperty, $channels);
48+
}
49+
);
5250
}
5351
}

src/PropertyBuilder/OptionBuilder.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,12 @@ public function __construct(ConcatedNameResolverInterface $optionNameResolver, S
4343
/**
4444
* @param TransformEvent $event
4545
*/
46-
public function buildProperty(TransformEvent $event): void
46+
public function consumeEvent(TransformEvent $event): void
4747
{
48-
/** @var ProductInterface $product */
49-
$product = $event->getObject();
50-
51-
if (!$product instanceof ProductInterface) {
52-
return;
53-
}
54-
55-
$document = $event->getDocument();
56-
57-
$this->resolveProductOptions($product, $document);
48+
$this->buildProperty($event, ProductInterface::class,
49+
function (ProductInterface $product, Document $document): void {
50+
$this->resolveProductOptions($product, $document);
51+
});
5852
}
5953

6054
/**
@@ -67,16 +61,11 @@ private function resolveProductOptions(ProductInterface $product, Document $docu
6761
foreach ($productVariant->getOptionValues() as $productOptionValue) {
6862
$optionCode = $productOptionValue->getOption()->getCode();
6963
$index = $this->optionNameResolver->resolvePropertyName($optionCode);
70-
71-
if (!$document->has($index)) {
72-
$document->set($index, []);
73-
}
74-
75-
$reference = $document->get($index);
64+
$options = $document->has($index) ? $document->get($index) : [];
7665
$value = $this->stringFormatter->formatToLowercaseWithoutSpaces($productOptionValue->getValue());
77-
$reference[] = $value;
66+
$options[] = $value;
7867

79-
$document->set($index, array_unique($reference));
68+
$document->set($index, array_unique($options));
8069
}
8170
}
8271
}

src/PropertyBuilder/OptionTaxonsBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct(
7979
/**
8080
* @param TransformEvent $event
8181
*/
82-
public function buildProperty(TransformEvent $event): void
82+
public function consumeEvent(TransformEvent $event): void
8383
{
8484
$documentProductOption = $event->getObject();
8585

src/PropertyBuilder/ProductCreatedAtPropertyBuilder.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
1414

15+
use Elastica\Document;
1516
use FOS\ElasticaBundle\Event\TransformEvent;
1617
use Sylius\Component\Core\Model\ProductInterface;
1718

@@ -33,18 +34,14 @@ public function __construct(string $createdAtProperty)
3334
/**
3435
* {@inheritdoc}
3536
*/
36-
public function buildProperty(TransformEvent $event): void
37+
public function consumeEvent(TransformEvent $event): void
3738
{
38-
/** @var ProductInterface $product */
39-
$product = $event->getObject();
39+
$this->buildProperty($event, ProductInterface::class,
40+
function (ProductInterface $product, Document $document): void {
41+
$createdAt = (int)$product->getCreatedAt()->format('U');
4042

41-
if (!$product instanceof ProductInterface) {
42-
return;
43-
}
44-
45-
$document = $event->getDocument();
46-
$createdAt = (int) $product->getCreatedAt()->format('U');
47-
48-
$document->set($this->createdAtProperty, $createdAt);
43+
$document->set($this->createdAtProperty, $createdAt);
44+
}
45+
);
4946
}
5047
}

0 commit comments

Comments
 (0)