Skip to content

Commit

Permalink
Simplify redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbager committed Mar 17, 2018
1 parent 1a7a285 commit c3773ff
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/Form/Type/ChoiceMapper/ProductAttributesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function mapToChoices(ProductAttributeInterface $productAttribute): array
{
$attributeValues = $this->productAttributeValueRepository->findBy(['attribute' => $productAttribute]);
$choices = [];
array_walk($attributeValues, function (ProductAttributeValueInterface $productAttributeValue) use (&$choices) {
array_walk($attributeValues, function (ProductAttributeValueInterface $productAttributeValue) use (&$choices): void {
$value = $productAttributeValue->getValue();
$configuration = $productAttributeValue->getAttribute()->getConfiguration();
if (is_array($value)
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/ChoiceMapper/ProductOptionsMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function mapToChoices(ProductOptionInterface $productOption): array
$productOptionValues = $productOption->getValues()->toArray();
$choices = [];

array_walk($productOptionValues, function (ProductOptionValueInterface $productOptionValue) use (&$choices) {
array_walk($productOptionValues, function (ProductOptionValueInterface $productOptionValue) use (&$choices): void {
$value = $productOptionValue->getValue();
$choices[$value] = $this->stringFormatter->formatToLowercaseWithoutSpaces($value);
});
Expand Down
18 changes: 17 additions & 1 deletion src/PropertyBuilder/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@

abstract class AbstractBuilder implements PropertyBuilderInterface
{
/**
* {@inheritdoc}
*/
public function buildProperty(TransformEvent $event, string $supportedModelClass, callable $callback): void
{
$model = $event->getObject();

if (!$model instanceof $supportedModelClass) {
return;
}

$document = $event->getDocument();

$callback($model, $document);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
TransformEvent::POST_TRANSFORM => 'buildProperty',
TransformEvent::POST_TRANSFORM => 'consumeEvent',
];
}
}
21 changes: 8 additions & 13 deletions src/PropertyBuilder/AttributeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,21 @@ final class AttributeBuilder extends AbstractBuilder
public function __construct(
ConcatedNameResolverInterface $attributeNameResolver,
StringFormatterInterface $stringFormatter
) {
)
{
$this->attributeNameResolver = $attributeNameResolver;
$this->stringFormatter = $stringFormatter;
}

/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
/** @var ProductInterface $product */
$product = $event->getObject();

if (!$product instanceof ProductInterface) {
return;
}

$document = $event->getDocument();

$this->resolveProductAttributes($product, $document);
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
$this->resolveProductAttributes($product, $document);
});
}

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

if (is_array($value)) {
foreach ($value as $singleElement) {
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement);
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string)$singleElement);
}
} else {
$value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyBuilder/AttributeTaxonsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(
/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
$documentAttribute = $event->getObject();

Expand Down
25 changes: 11 additions & 14 deletions src/PropertyBuilder/ChannelPricingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;

use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Document;
use FOS\ElasticaBundle\Event\TransformEvent;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
Expand All @@ -35,22 +36,18 @@ public function __construct(ConcatedNameResolverInterface $channelPricingNameRes
/**
* {@inheritdoc}
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
$product = $event->getObject();
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->first();

if (!$product instanceof ProductInterface) {
return;
}
foreach ($productVariant->getChannelPricings() as $channelPricing) {
$propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelPricing->getChannelCode());

$document = $event->getDocument();
/** @var ProductVariantInterface $productVariant */
$productVariant = $product->getVariants()->first();

foreach ($productVariant->getChannelPricings() as $channelPricing) {
$propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelPricing->getChannelCode());

$document->set($propertyName, $channelPricing->getPrice());
}
$document->set($propertyName, $channelPricing->getPrice());
}
});
}
}
24 changes: 11 additions & 13 deletions src/PropertyBuilder/ChannelsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;

use Elastica\Document;
use FOS\ElasticaBundle\Event\TransformEvent;
use Sylius\Component\Core\Model\ProductInterface;

Expand All @@ -33,21 +34,18 @@ public function __construct(string $channelsProperty)
/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
$product = $event->getObject();
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
$channels = [];

if (!$product instanceof ProductInterface) {
return;
}
foreach ($product->getChannels() as $channel) {
$channels[] = $channel->getCode();
}

$channels = [];

/** @var ProductInterface $product */
foreach ($product->getChannels() as $channel) {
$channels[] = $channel->getCode();
}

$event->getDocument()->set($this->channelsProperty, $channels);
$document->set($this->channelsProperty, $channels);
}
);
}
}
27 changes: 8 additions & 19 deletions src/PropertyBuilder/OptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ public function __construct(ConcatedNameResolverInterface $optionNameResolver, S
/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
/** @var ProductInterface $product */
$product = $event->getObject();

if (!$product instanceof ProductInterface) {
return;
}

$document = $event->getDocument();

$this->resolveProductOptions($product, $document);
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
$this->resolveProductOptions($product, $document);
});
}

/**
Expand All @@ -67,16 +61,11 @@ private function resolveProductOptions(ProductInterface $product, Document $docu
foreach ($productVariant->getOptionValues() as $productOptionValue) {
$optionCode = $productOptionValue->getOption()->getCode();
$index = $this->optionNameResolver->resolvePropertyName($optionCode);

if (!$document->has($index)) {
$document->set($index, []);
}

$reference = $document->get($index);
$options = $document->has($index) ? $document->get($index) : [];
$value = $this->stringFormatter->formatToLowercaseWithoutSpaces($productOptionValue->getValue());
$reference[] = $value;
$options[] = $value;

$document->set($index, array_unique($reference));
$document->set($index, array_unique($options));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyBuilder/OptionTaxonsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct(
/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
$documentProductOption = $event->getObject();

Expand Down
19 changes: 8 additions & 11 deletions src/PropertyBuilder/ProductCreatedAtPropertyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;

use Elastica\Document;
use FOS\ElasticaBundle\Event\TransformEvent;
use Sylius\Component\Core\Model\ProductInterface;

Expand All @@ -33,18 +34,14 @@ public function __construct(string $createdAtProperty)
/**
* {@inheritdoc}
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
/** @var ProductInterface $product */
$product = $event->getObject();
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
$createdAt = (int)$product->getCreatedAt()->format('U');

if (!$product instanceof ProductInterface) {
return;
}

$document = $event->getDocument();
$createdAt = (int) $product->getCreatedAt()->format('U');

$document->set($this->createdAtProperty, $createdAt);
$document->set($this->createdAtProperty, $createdAt);
}
);
}
}
28 changes: 12 additions & 16 deletions src/PropertyBuilder/ProductNameBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;

use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Document;
use FOS\ElasticaBundle\Event\TransformEvent;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTranslationInterface;
Expand All @@ -35,22 +36,17 @@ public function __construct(ConcatedNameResolverInterface $productNameNameResolv
/**
* {@inheritdoc}
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
/** @var ProductInterface $product */
$product = $event->getObject();

if (!$product instanceof ProductInterface) {
return;
}

$document = $event->getDocument();

/** @var ProductTranslationInterface $productTranslation */
foreach ($product->getTranslations() as $productTranslation) {
$propertyName = $this->productNameNameResolver->resolvePropertyName($productTranslation->getLocale());

$document->set($propertyName, $productTranslation->getName());
}
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
/** @var ProductTranslationInterface $productTranslation */
foreach ($product->getTranslations() as $productTranslation) {
$propertyName = $this->productNameNameResolver->resolvePropertyName($productTranslation->getLocale());

$document->set($propertyName, $productTranslation->getName());
}
}
);
}
}
32 changes: 17 additions & 15 deletions src/PropertyBuilder/ProductTaxonsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@

namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;

use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
use Elastica\Document;
use FOS\ElasticaBundle\Event\TransformEvent;
use Sylius\Component\Core\Model\ProductInterface;

final class ProductTaxonsBuilder extends AbstractBuilder
{
/**
* @var ProductTaxonsMapperInterface
*/
private $productTaxonsMapper;

/**
* @var string
*/
private $taxonsProperty;

/**
* @param ProductTaxonsMapperInterface $productTaxonsMapper
* @param string $taxonsProperty
*/
public function __construct(string $taxonsProperty)
public function __construct(ProductTaxonsMapperInterface $productTaxonsMapper, string $taxonsProperty)
{
$this->productTaxonsMapper = $productTaxonsMapper;
$this->taxonsProperty = $taxonsProperty;
}

/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void
public function consumeEvent(TransformEvent $event): void
{
$product = $event->getObject();

if (!$product instanceof ProductInterface) {
return;
}

$taxons = [];

/** @var ProductInterface $product */
foreach ($product->getTaxons() as $taxon) {
$taxons[] = $taxon->getCode();
}
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
$taxons = $this->productTaxonsMapper->mapToUniqueCodes($product);

$event->getDocument()->set($this->taxonsProperty, $taxons);
$document->set($this->taxonsProperty, $taxons);
}
);
}
}
9 changes: 8 additions & 1 deletion src/PropertyBuilder/PropertyBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ interface PropertyBuilderInterface extends EventSubscriberInterface
/**
* @param TransformEvent $event
*/
public function buildProperty(TransformEvent $event): void;
public function consumeEvent(TransformEvent $event): void;

/**
* @param TransformEvent $event
* @param string $class
* @param callable $callback
*/
public function buildProperty(TransformEvent $event, string $class, callable $callback): void;
}
Loading

0 comments on commit c3773ff

Please sign in to comment.