SyncOperation example

$this->createMock(EventDispatcherInterface::class),
            new StaticDefinitionInstanceRegistry(
                [ProductDefinition::class],
                $this->createMock(ValidatorInterface::class),
                $this->createMock(EntityWriteGatewayInterface::class),
            ),
            $this->createMock(EntitySearcherInterface::class),
            $this->createMock(RequestCriteriaBuilder::class),
            $this->createMock(SyncFkResolver::class)
        );

        $upsert = new SyncOperation('foo', 'product', SyncOperation::ACTION_UPSERT, [
            ['id' => '1', 'name' => 'foo'],
            ['id' => '2', 'name' => 'bar'],
        ]);

        $delete = new SyncOperation('delete-foo', 'product', SyncOperation::ACTION_DELETE, [
            ['id' => '1'],
            ['id' => '2'],
        ]);

        $behavior = new SyncBehavior('disable-indexing', ['product.indexer']);
        $result = $service->sync([$upsert$delete], Context::createDefaultContext()$behavior);

        
return $writes;
    }

    /** * @param array{insert:array<string, array<int, mixed>>, update:array<string, array<int, mixed>>, delete:array<string, array<int, mixed>>} $writes */
    private function executeWrites(array $writes, WriteContext $liveContext): WriteResult
    {
        $operations = [];
        foreach ($writes['insert'] as $entity => $payload) {
            $operations[] = new SyncOperation('insert-' . $entity$entity, 'upsert', $payload);
        }
        foreach ($writes['update'] as $entity => $payload) {
            $operations[] = new SyncOperation('update-' . $entity$entity, 'upsert', $payload);
        }
        foreach ($writes['delete'] as $entity => $payload) {
            $operations[] = new SyncOperation('delete-' . $entity$entity, 'delete', $payload);
        }

        return $this->entityWriter->sync($operations$liveContext);
    }

    
$configuratorSettingPayload = $this->getProductConfiguratorSettingPayload($payload$parentId);
        $this->connection->executeStatement(
            'DELETE FROM `product_configurator_setting` WHERE `product_id` = :parentId AND `id` NOT IN (:ids);',
            [
                'parentId' => Uuid::fromHexToBytes($parentId),
                'ids' => Uuid::fromHexToBytesList(array_column($configuratorSettingPayload, 'id')),
            ],
            ['ids' => ArrayParameterType::STRING]
        );

        $this->syncService->sync([
            new SyncOperation(
                'write',
                ProductDefinition::ENTITY_NAME,
                SyncOperation::ACTION_UPSERT,
                $payload
            ),
            new SyncOperation(
                'write',
                ProductConfiguratorSettingDefinition::ENTITY_NAME,
                SyncOperation::ACTION_UPSERT,
                $configuratorSettingPayload
            ),
        ],
return '"' . $ids->create($key) . '"';
        }$content);
    }

    /** * @param array<array<int, mixed>> $content */
    private function sync(array $content): void
    {
        $operations = [];
        foreach ($content as $entity => $data) {
            $operations[] = new SyncOperation($entity$entity, 'upsert', $data);
        }

        $this->writer->sync($operations, WriteContext::createFromContext(Context::createDefaultContext()));
    }

    private function getLocaleIdOfSystemLanguage(): string
    {
        return $this->connection
            ->fetchOne(
                'SELECT LOWER(HEX(locale_id)) FROM language WHERE id = UNHEX(:systemLanguageId)',
                ['systemLanguageId' => Defaults::LANGUAGE_SYSTEM]
            );

    public function sync(array $payload): SyncResult
    {
        $operations = [];
        foreach ($payload as $key => $operation) {
            if (isset($operation['key'])) {
                $key = $operation['key'];
            }
            $operations[] = new SyncOperation((string) $key(string) $operation['entity'](string) $operation['action']$operation['payload']);
        }

        return $this->syncService->sync($operations$this->context, new SyncBehavior());
    }
}
public function reset(): void
    {
        $this->categoryIdCache = [];
    }

    /** * @param list<array<string, mixed>> $payload */
    private function createNewCategories(array $payload): void
    {
        $this->syncService->sync([
            new SyncOperation(
                'write',
                CategoryDefinition::ENTITY_NAME,
                SyncOperation::ACTION_UPSERT,
                $payload
            ),
        ], Context::createDefaultContext()new SyncBehavior());
    }
}
use Shopware\Core\Framework\Api\Sync\SyncOperation;

/** * @internal * * @covers \Shopware\Core\Framework\Api\Sync\SyncOperation */
class SyncOperationTest extends TestCase
{
    public function testWithValidOperation(): void
    {
        $operation = new SyncOperation(
            'valid-operation',
            'entity-name',
            'upsert',
            [
                ['id' => 'id1', 'name' => 'first manufacturer'],
                ['id' => 'id2', 'name' => 'second manufacturer'],
            ]
        );

        static::assertEmpty($operation->validate());
    }

    
$indexingSkips
        );

        $payload = $this->serializer->decode($request->getContent(), 'json');

        $operations = [];
        foreach ($payload as $key => $operation) {
            if (isset($operation['key'])) {
                $key = $operation['key'];
            }
            $key = (string) $key;
            $operations[] = new SyncOperation(
                $key,
                $operation['entity'],
                $operation['action'],
                $operation['payload'] ?? [],
                $operation['criteria'] ?? []
            );

            if (empty($operation['entity'])) {
                throw ApiException::invalidSyncOperationException(sprintf('Missing "entity" argument for operation with key "%s". It needs to be a non-empty string.', (string) $key));
            }
        }

        
$product = (new ProductBuilder($ids, 'p1'))
            ->price(100)
            ->media('media-1')
            ->media('media-2')
            ->media('media-3')
            ->build();

        $this->getContainer()->get('product.repository')
            ->create([$product], Context::createDefaultContext());

        $operations = [
            new SyncOperation('delete-media', 'product_media', 'delete', [['id' => $ids->get('media-2')]]),
            new SyncOperation('update-product', 'product', 'upsert', [['id' => $ids->get('p1'), 'media' => [['id' => $ids->get('media-3'), 'position' => 10]]]]),
        ];

        $this->service->sync($operations, Context::createDefaultContext()new SyncBehavior());
    }

    public function testSingleOperationWithDeletesAndWrites(): void
    {
        $ids = new TestDataCollection();

        $currency = [
            
Home | Imprint | This part of the site doesn't use cookies.