Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/Controller/ErrorResolutionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SwagMigrationAssistant\Controller;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
use SwagMigrationAssistant\Exception\MigrationException;
use SwagMigrationAssistant\Migration\ErrorResolution\MigrationFieldExampleGenerator;
use SwagMigrationAssistant\Migration\Validation\MigrationFieldValidationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

#[Route(defaults: ['_routeScope' => ['api']])]
#[Package('fundamentals@after-sales')]
class ErrorResolutionController extends AbstractController
{
/**
* @internal
*/
public function __construct(
private readonly DefinitionInstanceRegistry $definitionRegistry,
private readonly MigrationFieldValidationService $fieldValidationService,
) {
}

#[Route(
path: '/api/_action/migration/error-resolution/validate',
name: 'api.admin.migration.error-resolution.validate',
defaults: ['_acl' => ['swag_migration.viewer']],
methods: [Request::METHOD_POST]
)]
public function validateResolution(Request $request, Context $context): JsonResponse
{
$entityName = (string) $request->request->get('entityName');
$fieldName = (string) $request->request->get('fieldName');

if ($entityName === '') {
throw MigrationException::missingRequestParameter('entityName');
}

if ($fieldName === '') {
throw MigrationException::missingRequestParameter('fieldName');
}

$fieldValue = $this->decodeFieldValue($request->request->all()['fieldValue'] ?? null);

if ($fieldValue === null) {
throw MigrationException::missingRequestParameter('fieldValue');
}

try {
$this->fieldValidationService->validateFieldValue(
$entityName,
$fieldName,
$fieldValue,
$context,
);
} catch (WriteConstraintViolationException $e) {
return new JsonResponse([
'valid' => false,
'violations' => $e->toArray(),
]);
} catch (\Exception $e) {
return new JsonResponse([
'valid' => false,
'violations' => [['message' => $e->getMessage()]],
]);
}

return new JsonResponse([
'valid' => true,
'violations' => [],
]);
}

#[Route(
path: '/api/_action/migration/error-resolution/example-field-structure',
name: 'api.admin.migration.error-resolution.example-field-structure',
defaults: ['_acl' => ['swag_migration.viewer']],
methods: [Request::METHOD_POST]
)]
public function getExampleFieldStructure(Request $request): JsonResponse
{
$entityName = (string) $request->request->get('entityName');
$fieldName = (string) $request->request->get('fieldName');

if ($entityName === '') {
throw MigrationException::missingRequestParameter('entityName');
}

if ($fieldName === '') {
throw MigrationException::missingRequestParameter('fieldName');
}

$entityDefinition = $this->definitionRegistry->getByEntityName($entityName);
$fields = $entityDefinition->getFields();

if (!$fields->has($fieldName)) {
throw MigrationException::entityFieldNotFound($entityName, $fieldName);
}

$field = $fields->get($fieldName);

$response = [
'fieldType' => MigrationFieldExampleGenerator::getFieldType($field),
'example' => MigrationFieldExampleGenerator::generateExample($field),
];

return new JsonResponse($response);
}

/**
* @return array<array-key, mixed>|bool|float|int|string|null
*/
private function decodeFieldValue(mixed $value): array|bool|float|int|string|null
{
if ($value === null || $value === '' || $value === []) {
return null;
}

if (!\is_string($value)) {
return $value;
}

$decoded = \json_decode($value, true);

return \json_last_error() === \JSON_ERROR_NONE ? $decoded : $value;
}
}
14 changes: 14 additions & 0 deletions src/DependencyInjection/migration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@
</call>
</service>

<service id="SwagMigrationAssistant\Controller\ErrorResolutionController" public="true">
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Validation\MigrationFieldValidationService"/>

<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>

<service id="SwagMigrationAssistant\Migration\DataSelection\DataSelectionRegistry">
<argument type="tagged_iterator" tag="shopware.migration.data_selection"/>
</service>
Expand Down Expand Up @@ -426,6 +435,11 @@
<argument type="service" id="Symfony\Component\EventDispatcher\EventDispatcherInterface"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Logging\LoggingService"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\MappingService"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Validation\MigrationFieldValidationService"/>
</service>

<service id="SwagMigrationAssistant\Migration\Validation\MigrationFieldValidationService">
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry"/>
</service>
</services>
</container>
24 changes: 24 additions & 0 deletions src/Exception/MigrationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class MigrationException extends HttpException

public const DUPLICATE_SOURCE_CONNECTION = 'SWAG_MIGRATION__DUPLICATE_SOURCE_CONNECTION';

final public const MISSING_REQUEST_PARAMETER = 'SWAG_MIGRATION__MISSING_REQUEST_PARAMETER';

final public const ENTITY_FIELD_NOT_FOUND = 'SWAG_MIGRATION__ENTITY_FIELD_NOT_FOUND';

public static function associationEntityRequiredMissing(string $entity, string $missingEntity): self
{
return new self(
Expand Down Expand Up @@ -500,4 +504,24 @@ public static function duplicateSourceConnection(): self
'A connection to this source system already exists.',
);
}

public static function missingRequestParameter(string $parameterName): self
{
return new self(
Response::HTTP_BAD_REQUEST,
self::MISSING_REQUEST_PARAMETER,
'Required request parameter "{{ parameterName }}" is missing.',
['parameterName' => $parameterName]
);
}

public static function entityFieldNotFound(string $entityName, string $fieldName): self
{
return new self(
Response::HTTP_NOT_FOUND,
self::ENTITY_FIELD_NOT_FOUND,
'Field "{{ fieldName }}" not found in entity "{{ entityName }}".',
['fieldName' => $fieldName, 'entityName' => $entityName]
);
}
}
Loading
Loading