-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProductDefaultValuesTransformer.php
More file actions
88 lines (71 loc) · 2.95 KB
/
ProductDefaultValuesTransformer.php
File metadata and controls
88 lines (71 loc) · 2.95 KB
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
88
<?php
declare(strict_types=1);
namespace Ergonode\IntegrationShopware\Transformer;
use Ergonode\IntegrationShopware\DTO\ProductTransformationDTO;
use Ergonode\IntegrationShopware\Model\ProductAttribute;
use Ergonode\IntegrationShopware\Service\ConfigService;
use Ergonode\IntegrationShopware\Util\IsoCodeConverter;
use Shopware\Core\Framework\Context;
class ProductDefaultValuesTransformer implements ProductDataTransformerInterface
{
private const DEFAULT_STOCK_VALUE = 0;
private ConfigService $configService;
public function __construct(ConfigService $configService)
{
$this->configService = $configService;
}
public function transform(ProductTransformationDTO $productData, Context $context): ProductTransformationDTO
{
$swData = $productData->getShopwareData();
$ergonodeData = $productData->getErgonodeData();
$sku = $ergonodeData->getSku();
if ($this->configService->forceUppercaseSkuSync()) {
$sku = strtoupper($sku);
}
$swData->setProductNumber($sku);
$swData->setName($this->getName($productData));
$swData->setStock($this->getStock($productData));
foreach ($ergonodeData->getName()?->getTranslations() ?? [] as $translation) {
if (!is_null($translation->getValue())) {
$swData->setTranslatedName(
IsoCodeConverter::ergonodeToShopwareIso($translation->getLanguage()),
$translation->getValue()
);
}
}
$productData->setShopwareData($swData);
return $productData;
}
private function getName(ProductTransformationDTO $productData): string
{
$ergonodeData = $productData->getErgonodeData();
$name = $ergonodeData->getName();
if ($name instanceof ProductAttribute) {
$defaultLanguage = $productData->getDefaultLanguage();
$translation = $name->getTranslation($defaultLanguage);
if ($translation && !is_null($translation->getValue())) {
return $translation->getValue();
}
}
if ($productData->getSwProduct()?->getName()) {
return $productData->getSwProduct()?->getName();
}
return $productData->getErgonodeData()->getSku();
}
private function getStock(ProductTransformationDTO $productData): int
{
$ergonodeData = $productData->getErgonodeData();
$stock = $ergonodeData->getStock();
if ($stock instanceof ProductAttribute) {
$defaultLanguage = $productData->getDefaultLanguage();
$translation = $stock->getTranslation($defaultLanguage);
if ($translation && !is_null($translation->getValue())) {
return $translation->getValue();
}
}
if ($productData->getSwProduct()?->getStock()) {
return $productData->getSwProduct()?->getStock();
}
return self::DEFAULT_STOCK_VALUE;
}
}