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

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