This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ProductVariantChildMapper.php
87 lines (74 loc) · 3.47 KB
/
ProductVariantChildMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Copyright © Ergonode Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/
declare(strict_types=1);
namespace Ergonode\ExporterShopware6\Infrastructure\Mapper\Product;
use Ergonode\Attribute\Domain\Repository\AttributeRepositoryInterface;
use Ergonode\Attribute\Domain\Repository\OptionRepositoryInterface;
use Ergonode\Channel\Domain\Entity\Export;
use Ergonode\Core\Domain\ValueObject\Language;
use Ergonode\ExporterShopware6\Domain\Entity\Shopware6Channel;
use Ergonode\ExporterShopware6\Domain\Repository\ProductRepositoryInterface as Shopware6ProductRepositoryInterface;
use Ergonode\ExporterShopware6\Domain\Repository\PropertyGroupOptionsRepositoryInterface;
use Ergonode\ExporterShopware6\Infrastructure\Model\Shopware6Product;
use Ergonode\Product\Domain\Entity\AbstractProduct;
use Ergonode\Product\Domain\Entity\VariableProduct;
use Ergonode\Product\Domain\Query\ProductChildrenQueryInterface;
use Ergonode\Product\Domain\Repository\ProductRepositoryInterface;
use Ergonode\Product\Infrastructure\Calculator\TranslationInheritanceCalculator;
use Ergonode\SharedKernel\Domain\Aggregate\ProductId;
class ProductVariantChildMapper extends AbstractVariantOptionMapper
{
private ProductChildrenQueryInterface $childQuery;
private Shopware6ProductRepositoryInterface $shopware6ProductRepository;
private ProductRepositoryInterface $productRepository;
public function __construct(
ProductChildrenQueryInterface $childQuery,
Shopware6ProductRepositoryInterface $shopware6ProductRepository,
ProductRepositoryInterface $productRepository,
AttributeRepositoryInterface $attributeRepository,
OptionRepositoryInterface $optionRepository,
TranslationInheritanceCalculator $calculator,
PropertyGroupOptionsRepositoryInterface $propertyGroupOptionsRepository
) {
parent::__construct($attributeRepository, $optionRepository, $calculator, $propertyGroupOptionsRepository);
$this->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;
}
}