src/Core/Framework/Api/Controller/SyncController.php line 40

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Controller;
  3. use Doctrine\DBAL\ConnectionException;
  4. use Shopware\Core\Framework\Api\Exception\InvalidSyncOperationException;
  5. use Shopware\Core\Framework\Api\Sync\SyncBehavior;
  6. use Shopware\Core\Framework\Api\Sync\SyncOperation;
  7. use Shopware\Core\Framework\Api\Sync\SyncResult;
  8. use Shopware\Core\Framework\Api\Sync\SyncServiceInterface;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Shopware\Core\PlatformRequest;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Serializer\Encoder\DecoderInterface;
  18. #[Route(defaults: ['_routeScope' => ['api']])]
  19. #[Package('core')]
  20. class SyncController extends AbstractController
  21. {
  22.     final public const ACTION_UPSERT 'upsert';
  23.     final public const ACTION_DELETE 'delete';
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(private readonly SyncServiceInterface $syncService, private readonly DecoderInterface $serializer)
  28.     {
  29.     }
  30.     /**
  31.      * @throws ConnectionException
  32.      * @throws InvalidSyncOperationException
  33.      */
  34.     #[Route(path'/api/_action/sync'name'api.action.sync'methods: ['POST'])]
  35.     public function sync(Request $requestContext $context): JsonResponse
  36.     {
  37.         $indexingSkips array_filter(explode(',', (string) $request->headers->get(PlatformRequest::HEADER_INDEXING_SKIP'')));
  38.         $behavior = new SyncBehavior(
  39.             $request->headers->get(PlatformRequest::HEADER_INDEXING_BEHAVIOR),
  40.             $indexingSkips
  41.         );
  42.         $payload $this->serializer->decode($request->getContent(), 'json');
  43.         $operations = [];
  44.         foreach ($payload as $key => $operation) {
  45.             if (isset($operation['key'])) {
  46.                 $key $operation['key'];
  47.             }
  48.             $operations[] = new SyncOperation((string) $key$operation['entity'], $operation['action'], $operation['payload']);
  49.         }
  50.         $result $context->scope(Context::CRUD_API_SCOPE, fn (Context $context): SyncResult => $this->syncService->sync($operations$context$behavior));
  51.         return $this->createResponse($resultResponse::HTTP_OK);
  52.     }
  53.     private function createResponse(SyncResult $resultint $statusCode 200): JsonResponse
  54.     {
  55.         $response = new JsonResponse(null$statusCode);
  56.         $response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS \JSON_INVALID_UTF8_SUBSTITUTE);
  57.         $response->setData($result);
  58.         return $response;
  59.     }
  60. }