vendor/shopware/administration/Controller/UserConfigController.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Administration\Controller;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\Exception\InvalidContextSourceException;
  7. use Shopware\Core\Framework\Api\Controller\Exception\ExpectedUserHttpException;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\MultiInsertQueryQueue;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  16. use Shopware\Core\Framework\Routing\Annotation\Since;
  17. use Shopware\Core\Framework\Uuid\Uuid;
  18. use Shopware\Core\System\User\Aggregate\UserConfig\UserConfigDefinition;
  19. use Shopware\Core\System\User\Aggregate\UserConfig\UserConfigEntity;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. class UserConfigController extends AbstractController
  26. {
  27.     private EntityRepositoryInterface $userConfigRepository;
  28.     private Connection $connection;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(EntityRepositoryInterface $userConfigRepositoryConnection $connection)
  33.     {
  34.         $this->userConfigRepository $userConfigRepository;
  35.         $this->connection $connection;
  36.     }
  37.     /**
  38.      * @Since("6.4.5.0")
  39.      * @Route("/api/_info/config-me", name="api.config_me.get", defaults={"auth_required"=true, "_routeScope"={"administration"}}, methods={"GET"})
  40.      */
  41.     public function getConfigMe(Context $contextRequest $request): Response
  42.     {
  43.         $userConfigs $this->getOwnUserConfig($context$request->query->all('keys'));
  44.         $data = [];
  45.         /** @var UserConfigEntity $userConfig */
  46.         foreach ($userConfigs as $userConfig) {
  47.             $data[$userConfig->getKey()] = $userConfig->getValue();
  48.         }
  49.         return new JsonResponse(['data' => $data]);
  50.     }
  51.     /**
  52.      * @Since("6.4.5.0")
  53.      * @Route("/api/_info/config-me", name="api.config_me.update", defaults={"auth_required"=true, "_routeScope"={"administration"}}, methods={"POST"})
  54.      */
  55.     public function updateConfigMe(Context $contextRequest $request): Response
  56.     {
  57.         $postUpdateConfigs $request->request->all();
  58.         if (empty($postUpdateConfigs)) {
  59.             return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  60.         }
  61.         $this->massUpsert($context$postUpdateConfigs);
  62.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  63.     }
  64.     private function getOwnUserConfig(Context $context, array $keys): array
  65.     {
  66.         $userId $this->getUserId($context);
  67.         $criteria = new Criteria();
  68.         $criteria->addFilter(new EqualsFilter('userId'$userId));
  69.         if (!empty($keys)) {
  70.             $criteria->addFilter(new EqualsAnyFilter('key'$keys));
  71.         }
  72.         return $this->userConfigRepository->search($criteria$context)->getElements();
  73.     }
  74.     private function getUserId(Context $context): string
  75.     {
  76.         if (!$context->getSource() instanceof AdminApiSource) {
  77.             throw new InvalidContextSourceException(AdminApiSource::class, \get_class($context->getSource()));
  78.         }
  79.         $userId $context->getSource()->getUserId();
  80.         if (!$userId) {
  81.             throw new ExpectedUserHttpException();
  82.         }
  83.         return $userId;
  84.     }
  85.     private function massUpsert(Context $context, array $postUpdateConfigs): void
  86.     {
  87.         $userId $this->getUserId($context);
  88.         $userConfigs $this->getOwnUserConfig($contextarray_keys($postUpdateConfigs));
  89.         $userConfigsGroupByKey = [];
  90.         /** @var UserConfigEntity $userConfig */
  91.         foreach ($userConfigs as $userConfig) {
  92.             $userConfigsGroupByKey[$userConfig->getKey()] = $userConfig->getId();
  93.         }
  94.         $queue = new MultiInsertQueryQueue($this->connection250falsetrue);
  95.         foreach ($postUpdateConfigs as $key => $value) {
  96.             $data = [
  97.                 'value' => JsonFieldSerializer::encodeJson($value),
  98.                 'user_id' => Uuid::fromHexToBytes($userId),
  99.                 'key' => $key,
  100.                 'id' => Uuid::randomBytes(),
  101.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  102.             ];
  103.             if (\array_key_exists($key$userConfigsGroupByKey)) {
  104.                 $data['id'] = Uuid::fromHexToBytes($userConfigsGroupByKey[$key]);
  105.             }
  106.             $queue->addInsert(UserConfigDefinition::ENTITY_NAME$data);
  107.         }
  108.         $queue->execute();
  109.     }
  110. }