From 9cea1c439ec733e49af953ab4f1436d012ea217b Mon Sep 17 00:00:00 2001 From: Tomasz Kardas Date: Mon, 2 Aug 2021 14:27:57 +0200 Subject: [PATCH] refactor product variant configuration --- .../Product/ProductVariantChildMapper.php | 86 ------------------- .../Mapper/Product/ProductVariantMapper.php | 27 +++--- src/Resources/config/services.yml | 1 - 3 files changed, 15 insertions(+), 99 deletions(-) delete mode 100644 src/Infrastructure/Mapper/Product/ProductVariantChildMapper.php diff --git a/src/Infrastructure/Mapper/Product/ProductVariantChildMapper.php b/src/Infrastructure/Mapper/Product/ProductVariantChildMapper.php deleted file mode 100644 index 97ac82769..000000000 --- a/src/Infrastructure/Mapper/Product/ProductVariantChildMapper.php +++ /dev/null @@ -1,86 +0,0 @@ -childQuery = $childQuery; - $this->shopware6ProductRepository = $shopware6ProductRepository; - $this->productRepository = $productRepository; - } - - public function map( - Shopware6Channel $channel, - Export $export, - Shopware6Product $shopware6Product, - AbstractProduct $product, - ?Language $language = null - ): Shopware6Product { - $parentsIds = $this->childQuery->findProductIdByProductChildrenId($product->getId()); - if (!empty($parentsIds)) { - $parentProductId = reset($parentsIds); - $this->parentMap($shopware6Product, $parentProductId, $product, $channel); - } - - return $shopware6Product; - } - - private function parentMap( - Shopware6Product $shopware6Product, - ProductId $parentProductId, - AbstractProduct $product, - Shopware6Channel $channel - ): Shopware6Product { - - $parentProduct = $this->productRepository->load($parentProductId); - if ($parentProduct instanceof VariableProduct) { - $parent = $this->shopware6ProductRepository->load($channel->getId(), $parentProductId); - $shopware6Product->setParentId($parent); - foreach ($parentProduct->getBindings() as $bindingId) { - $shopwareOption = $this->optionMapper($bindingId, $product, $channel); - if ($shopwareOption) { - $shopware6Product->addOptions($shopwareOption); - } - } - } - - return $shopware6Product; - } -} diff --git a/src/Infrastructure/Mapper/Product/ProductVariantMapper.php b/src/Infrastructure/Mapper/Product/ProductVariantMapper.php index 3099be722..373d75fba 100644 --- a/src/Infrastructure/Mapper/Product/ProductVariantMapper.php +++ b/src/Infrastructure/Mapper/Product/ProductVariantMapper.php @@ -8,7 +8,6 @@ namespace Ergonode\ExporterShopware6\Infrastructure\Mapper\Product; -use Ergonode\Attribute\Domain\Query\OptionQueryInterface; use Ergonode\Attribute\Domain\Repository\AttributeRepositoryInterface; use Ergonode\Attribute\Domain\Repository\OptionRepositoryInterface; use Ergonode\Core\Domain\ValueObject\Language; @@ -21,26 +20,27 @@ use Ergonode\ExporterShopware6\Infrastructure\Model\Product\Shopware6ProductConfiguratorSettings; use Ergonode\Product\Domain\Entity\AbstractProduct; use Ergonode\Product\Domain\Entity\VariableProduct; +use Ergonode\Product\Domain\Repository\ProductRepositoryInterface; use Ergonode\SharedKernel\Domain\Aggregate\AttributeId; -use Ergonode\SharedKernel\Domain\AggregateId; +use Webmozart\Assert\Assert; class ProductVariantMapper extends AbstractVariantOptionMapper { private PropertyGroupRepositoryInterface $propertyGroupRepository; - private OptionQueryInterface $optionQuery; + protected ProductRepositoryInterface $productRepository; public function __construct( PropertyGroupRepositoryInterface $propertyGroupRepository, - OptionQueryInterface $optionQuery, AttributeRepositoryInterface $attributeRepository, OptionRepositoryInterface $optionRepository, AttributeTranslationInheritanceCalculator $calculator, - PropertyGroupOptionsRepositoryInterface $propertyGroupOptionsRepository + PropertyGroupOptionsRepositoryInterface $propertyGroupOptionsRepository, + ProductRepositoryInterface $productRepository ) { parent::__construct($attributeRepository, $optionRepository, $calculator, $propertyGroupOptionsRepository); $this->propertyGroupRepository = $propertyGroupRepository; - $this->optionQuery = $optionQuery; + $this->productRepository = $productRepository; } public function map( @@ -64,22 +64,25 @@ private function variantMapper( ): Shopware6Product { foreach ($product->getBindings() as $bindingId) { if ($this->propertyGroupRepository->exists($channel->getId(), $bindingId)) { - $this->mapOptions($channel, $shopware6Product, $bindingId); + $this->mapOptions($channel, $shopware6Product, $bindingId, $product->getChildren()); } } return $shopware6Product; } + private function mapOptions( Shopware6Channel $channel, Shopware6Product $shopware6Product, - AttributeId $bindingId + AttributeId $bindingId, + array $childrenId ): Shopware6Product { - $options = $this->optionQuery->getOptions($bindingId); - foreach ($options as $option) { - $optionId = new AggregateId($option); - $shopwareId = $this->optionMap($channel, $bindingId, $optionId); + foreach ($childrenId as $childId) { + $child = $this->productRepository->load($childId); + Assert::isInstanceOf($child, AbstractProduct::class); + + $shopwareId = $this->optionMapper($bindingId, $child, $channel); if ($shopwareId) { $shopware6Product->addConfiguratorSettings( new Shopware6ProductConfiguratorSettings(null, $shopwareId) diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index ef2976ca4..59eef7ca2 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -111,7 +111,6 @@ services: - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\CustomField\ProductCustomFieldSetMultimediaMapper' - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\CustomField\ProductCustomFieldSetUnitMapper' - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\ProductVariantMapper' - - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\ProductVariantChildMapper' - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\ProductCoverMapper' - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\ProductSEOMetaTitleMapper' - '@Ergonode\ExporterShopware6\Infrastructure\Mapper\Product\ProductSEOMetaDescriptionMapper'