Skip to content

Commit 5d4ec50

Browse files
refactor: remove validation from converter (#79)
1 parent de33168 commit 5d4ec50

File tree

118 files changed

+1286
-1625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+1286
-1625
lines changed

src/DependencyInjection/shopware.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@
212212

213213
<service id="SwagMigrationAssistant\Profile\Shopware\Converter\LanguageConverter"
214214
parent="SwagMigrationAssistant\Profile\Shopware\Converter\ShopwareConverter" abstract="true">
215-
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\Lookup\LanguageLookup"/>
216215
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\Lookup\LocaleLookup"/>
216+
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\Lookup\LanguageLookup"/>
217217
</service>
218218

219219
<service id="SwagMigrationAssistant\Profile\Shopware\Converter\SalesChannelConverter"

src/Migration/Converter/ConverterInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ public function getMediaUuids(array $converted): ?array;
3838
/**
3939
* Converts the given data into the internal structure
4040
*
41+
* A converter also could return null in various cases
42+
*
4143
* @param array<string, mixed> $data
4244
*/
43-
public function convert(array $data, Context $context, MigrationContextInterface $migrationContext): ConvertStruct;
45+
public function convert(array $data, Context $context, MigrationContextInterface $migrationContext): ?ConvertStruct;
4446

4547
public function writeMapping(Context $context): void;
4648
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* (c) shopware AG <info@shopware.com>
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
namespace SwagMigrationAssistant\Migration\Logging\Log;
9+
10+
use Shopware\Core\Framework\Log\Package;
11+
use SwagMigrationAssistant\Migration\Logging\Log\Builder\AbstractMigrationLogEntry;
12+
13+
#[Package('after-sales')]
14+
readonly class MainVariantRelationNotConverted extends AbstractMigrationLogEntry
15+
{
16+
public function getLevel(): string
17+
{
18+
return self::LOG_LEVEL_ERROR;
19+
}
20+
21+
public function getCode(): string
22+
{
23+
return 'SWAG_MIGRATION__MAIN_VARIANT_RELATION_NOT_CONVERTED';
24+
}
25+
26+
public function isUserFixable(): bool
27+
{
28+
return false;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* (c) shopware AG <info@shopware.com>
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
namespace SwagMigrationAssistant\Migration\Logging\Log;
9+
10+
use Shopware\Core\Framework\Log\Package;
11+
use SwagMigrationAssistant\Migration\Logging\Log\Builder\AbstractMigrationLogEntry;
12+
13+
#[Package('after-sales')]
14+
readonly class NotConvertedLog extends AbstractMigrationLogEntry
15+
{
16+
public function getLevel(): string
17+
{
18+
return self::LOG_LEVEL_INFO;
19+
}
20+
21+
public function getCode(): string
22+
{
23+
return 'SWAG_MIGRATION__ENTITY_NOT_CONVERTED';
24+
}
25+
26+
public function isUserFixable(): bool
27+
{
28+
return false;
29+
}
30+
}

src/Migration/Service/MigrationDataConverter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
use Shopware\Core\Framework\Util\Hasher;
1616
use SwagMigrationAssistant\Migration\Converter\ConverterInterface;
1717
use SwagMigrationAssistant\Migration\Converter\ConverterRegistryInterface;
18+
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1819
use SwagMigrationAssistant\Migration\DataSelection\DataSet\DataSet;
1920
use SwagMigrationAssistant\Migration\Logging\Log\Builder\MigrationLogBuilder;
2021
use SwagMigrationAssistant\Migration\Logging\Log\ExceptionRunLog;
22+
use SwagMigrationAssistant\Migration\Logging\Log\NotConvertedLog;
2123
use SwagMigrationAssistant\Migration\Logging\LoggingServiceInterface;
2224
use SwagMigrationAssistant\Migration\Mapping\MappingDeltaResult;
2325
use SwagMigrationAssistant\Migration\Mapping\MappingServiceInterface;
@@ -96,6 +98,17 @@ private function convertData(
9698
foreach ($data as $item) {
9799
try {
98100
$convertStruct = $converter->convert($item, $context, $migrationContext);
101+
if (!$convertStruct instanceof ConvertStruct) {
102+
$this->loggingService->addLogEntry(
103+
MigrationLogBuilder::fromMigrationContext($migrationContext)
104+
->withSourceData($item)
105+
->withEntityName($dataSet::getEntity())
106+
->build(NotConvertedLog::class)
107+
);
108+
109+
continue;
110+
}
111+
99112
$convertFailureFlag = empty($convertStruct->getConverted());
100113

101114
$this->validationService->validate(

src/Profile/Shopware/Converter/CategoryConverter.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use SwagMigrationAssistant\Exception\MigrationException;
1515
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1616
use SwagMigrationAssistant\Migration\DataSelection\DefaultEntities;
17-
use SwagMigrationAssistant\Migration\Logging\Log\Builder\MigrationLogBuilder;
18-
use SwagMigrationAssistant\Migration\Logging\Log\EmptyNecessaryFieldRunLog;
1917
use SwagMigrationAssistant\Migration\Logging\LoggingServiceInterface;
2018
use SwagMigrationAssistant\Migration\Mapping\Lookup\DefaultCmsPageLookup;
2119
use SwagMigrationAssistant\Migration\Mapping\Lookup\LanguageLookup;
@@ -37,7 +35,7 @@ abstract class CategoryConverter extends ShopwareConverter
3735

3836
protected string $oldCategoryId;
3937

40-
protected string $locale;
38+
protected ?string $locale = null;
4139

4240
protected string $runId;
4341

@@ -84,19 +82,13 @@ public function convert(
8482
$this->connectionId = $connection->getId();
8583
$this->connectionName = $connection->getName();
8684

87-
if (!isset($data['_locale'])) {
88-
$this->loggingService->addLogEntry(
89-
MigrationLogBuilder::fromMigrationContext($migrationContext)
90-
->withEntityName(CategoryDefinition::ENTITY_NAME)
91-
->withFieldSourcePath('_locale')
92-
->withSourceData($data)
93-
->build(EmptyNecessaryFieldRunLog::class)
94-
);
85+
$converted = [];
9586

96-
return new ConvertStruct(null, $data);
87+
if (isset($data['_locale'])) {
88+
$this->locale = $data['_locale'];
89+
} else {
90+
$this->locale = null;
9791
}
98-
$this->locale = $data['_locale'];
99-
$converted = [];
10092

10193
$cmsPageUuid = $this->defaultCmsPageLookup->get($context);
10294
if ($cmsPageUuid !== null) {
@@ -244,8 +236,7 @@ protected function setGivenCategoryTranslation(array &$data, array &$converted):
244236
}
245237

246238
$locale = $language->getLocale();
247-
248-
if ($locale === null || $locale->getCode() === $data['_locale']) {
239+
if (!isset($data['_locale']) || $locale?->getCode() === $data['_locale']) {
249240
return;
250241
}
251242

@@ -364,10 +355,12 @@ protected function addMediaTranslation(array &$media, array $data): void
364355
$localeTranslation['id'] = $mapping['entityId'];
365356
$this->mappingIds[] = $mapping['id'];
366357

367-
$languageUuid = $this->languageLookup->get($this->locale, $this->context);
368-
if ($languageUuid !== null) {
369-
$localeTranslation['languageId'] = $languageUuid;
370-
$media['translations'][$languageUuid] = $localeTranslation;
358+
if ($this->locale !== null) {
359+
$languageUuid = $this->languageLookup->get($this->locale, $this->context);
360+
if ($languageUuid !== null) {
361+
$localeTranslation['languageId'] = $languageUuid;
362+
$media['translations'][$languageUuid] = $localeTranslation;
363+
}
371364
}
372365
}
373366
}

src/Profile/Shopware/Converter/CrossSellingConverter.php

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77

88
namespace SwagMigrationAssistant\Profile\Shopware\Converter;
99

10-
use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingDefinition;
1110
use Shopware\Core\Framework\Context;
1211
use Shopware\Core\Framework\Log\Package;
1312
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1413
use SwagMigrationAssistant\Migration\DataSelection\DefaultEntities;
15-
use SwagMigrationAssistant\Migration\Logging\Log\AssociationRequiredMissingLog;
16-
use SwagMigrationAssistant\Migration\Logging\Log\Builder\MigrationLogBuilder;
1714
use SwagMigrationAssistant\Migration\MigrationContextInterface;
1815

1916
#[Package('fundamentals@after-sales')]
@@ -59,36 +56,14 @@ public function convert(array $data, Context $context, MigrationContextInterface
5956
$converted['id'] = $crossSellingMapping['entityId'];
6057

6158
$sourceProductMapping = $this->getProductMapping($data['articleID']);
62-
if ($sourceProductMapping === null) {
63-
$this->loggingService->addLogEntry(
64-
MigrationLogBuilder::fromMigrationContext($migrationContext)
65-
->withEntityName(ProductCrossSellingDefinition::ENTITY_NAME)
66-
->withFieldName('id')
67-
->withFieldSourcePath('articleID')
68-
->withSourceData($data)
69-
->withConvertedData($converted)
70-
->build(AssociationRequiredMissingLog::class)
71-
);
72-
73-
return new ConvertStruct(null, $data);
59+
if ($sourceProductMapping !== null) {
60+
$this->mappingIds[] = $sourceProductMapping['id'];
7461
}
75-
$this->mappingIds[] = $sourceProductMapping['id'];
7662

7763
$relatedProductMapping = $this->getProductMapping($data['relatedarticle']);
78-
if ($relatedProductMapping === null) {
79-
$this->loggingService->addLogEntry(
80-
MigrationLogBuilder::fromMigrationContext($migrationContext)
81-
->withEntityName(ProductCrossSellingDefinition::ENTITY_NAME)
82-
->withFieldName('id')
83-
->withFieldSourcePath('relatedarticle')
84-
->withSourceData($data)
85-
->withConvertedData($converted)
86-
->build(AssociationRequiredMissingLog::class)
87-
);
88-
89-
return new ConvertStruct(null, $data);
64+
if ($relatedProductMapping !== null) {
65+
$this->mappingIds[] = $relatedProductMapping['id'];
9066
}
91-
$this->mappingIds[] = $relatedProductMapping['id'];
9267

9368
if ($data['type'] === DefaultEntities::CROSS_SELLING_SIMILAR) {
9469
$converted['name'] = 'Similar Items';
@@ -105,15 +80,17 @@ public function convert(array $data, Context $context, MigrationContextInterface
10580

10681
$converted['type'] = 'productList';
10782
$converted['active'] = true;
108-
$converted['productId'] = $sourceProductMapping['entityId'];
10983
$converted['assignedProducts'] = [
11084
[
111-
'id' => $relationMapping['entityId'],
112-
'position' => $data['position'],
113-
'productId' => $relatedProductMapping['entityId'],
85+
'id' => $relationMapping['entityId'] ?? null,
86+
'position' => $data['position'] ?? null,
87+
'productId' => $relatedProductMapping['entityId'] ?? null,
11488
],
11589
];
11690

91+
if (isset($sourceProductMapping['entityId'])) {
92+
$converted['productId'] = $sourceProductMapping['entityId'];
93+
}
11794
unset(
11895
$data['type'],
11996
$data['id'],

src/Profile/Shopware/Converter/CurrencyConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function getSourceIdentifier(array $data): string
4040
return $data['currency'];
4141
}
4242

43-
public function convert(array $data, Context $context, MigrationContextInterface $migrationContext): ConvertStruct
43+
public function convert(array $data, Context $context, MigrationContextInterface $migrationContext): ?ConvertStruct
4444
{
4545
$this->generateChecksum($data);
4646
$this->context = $context;
@@ -63,7 +63,7 @@ public function convert(array $data, Context $context, MigrationContextInterface
6363
);
6464
}
6565

66-
return new ConvertStruct(null, $data);
66+
return null;
6767
}
6868

6969
$converted = [];

0 commit comments

Comments
 (0)