vendor/shopware/core/Framework/Api/Controller/SyncController.php line 51

Open in your IDE?
  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\Feature;
  11. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  12. use Shopware\Core\Framework\Routing\Annotation\Since;
  13. use Shopware\Core\PlatformRequest;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Serializer\Serializer;
  20. /**
  21.  * @Route(defaults={"_routeScope"={"api"}})
  22.  */
  23. class SyncController extends AbstractController
  24. {
  25.     public const ACTION_UPSERT 'upsert';
  26.     public const ACTION_DELETE 'delete';
  27.     private Serializer $serializer;
  28.     private SyncServiceInterface $syncService;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(SyncServiceInterface $syncServiceSerializer $serializer)
  33.     {
  34.         $this->serializer $serializer;
  35.         $this->syncService $syncService;
  36.     }
  37.     /**
  38.      * @Since("6.0.0.0")
  39.      * @Route("/api/_action/sync", name="api.action.sync", methods={"POST"})
  40.      *
  41.      * @throws ConnectionException
  42.      * @throws InvalidSyncOperationException
  43.      */
  44.     public function sync(Request $requestContext $context): JsonResponse
  45.     {
  46.         $indexingSkips array_filter(explode(','$request->headers->get(PlatformRequest::HEADER_INDEXING_SKIP'')));
  47.         if (Feature::isActive('FEATURE_NEXT_15815')) {
  48.             $behavior = new SyncBehavior(
  49.                 $request->headers->get(PlatformRequest::HEADER_INDEXING_BEHAVIOR),
  50.                 $indexingSkips
  51.             );
  52.         } else {
  53.             $behavior = new SyncBehavior(
  54.                 filter_var($request->headers->get(PlatformRequest::HEADER_FAIL_ON_ERROR'true'), \FILTER_VALIDATE_BOOLEAN),
  55.                 filter_var($request->headers->get(PlatformRequest::HEADER_SINGLE_OPERATION'false'), \FILTER_VALIDATE_BOOLEAN),
  56.                 $request->headers->get(PlatformRequest::HEADER_INDEXING_BEHAVIORnull),
  57.                 $indexingSkips
  58.             );
  59.         }
  60.         $payload $this->serializer->decode($request->getContent(), 'json');
  61.         $operations = [];
  62.         foreach ($payload as $key => $operation) {
  63.             if (isset($operation['key'])) {
  64.                 $key $operation['key'];
  65.             }
  66.             $operations[] = new SyncOperation((string) $key$operation['entity'], $operation['action'], $operation['payload']);
  67.         }
  68.         $result $context->scope(Context::CRUD_API_SCOPE, function (Context $context) use ($operations$behavior): SyncResult {
  69.             return $this->syncService->sync($operations$context$behavior);
  70.         });
  71.         if (Feature::isActive('FEATURE_NEXT_15815')) {
  72.             return $this->createResponse($resultResponse::HTTP_OK);
  73.         }
  74.         if ($behavior->failOnError() && !$result->isSuccess()) {
  75.             return $this->createResponse($resultResponse::HTTP_BAD_REQUEST);
  76.         }
  77.         return $this->createResponse($resultResponse::HTTP_OK);
  78.     }
  79.     private function createResponse(SyncResult $resultint $statusCode 200): JsonResponse
  80.     {
  81.         $response = new JsonResponse(null$statusCode);
  82.         $response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS \JSON_INVALID_UTF8_SUBSTITUTE);
  83.         $response->setData($result);
  84.         return $response;
  85.     }
  86. }