vendor/shopware/core/System/SalesChannel/Context/BaseContextFactory.php line 84

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\Context;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  6. use Shopware\Core\Checkout\Payment\Exception\UnknownPaymentMethodException;
  7. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  8. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  9. use Shopware\Core\Defaults;
  10. use Shopware\Core\Framework\Api\Context\AdminSalesChannelApiSource;
  11. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  12. use Shopware\Core\Framework\Context;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Feature;
  18. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  19. use Shopware\Core\Framework\Routing\Exception\LanguageNotFoundException;
  20. use Shopware\Core\Framework\Uuid\Uuid;
  21. use Shopware\Core\System\Currency\Aggregate\CurrencyCountryRounding\CurrencyCountryRoundingEntity;
  22. use Shopware\Core\System\Currency\CurrencyEntity;
  23. use Shopware\Core\System\SalesChannel\BaseContext;
  24. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  25. use Shopware\Core\System\Tax\TaxCollection;
  26. use function array_unique;
  27. /**
  28.  * @internal
  29.  */
  30. class BaseContextFactory extends AbstractBaseContextFactory
  31. {
  32.     private EntityRepositoryInterface $salesChannelRepository;
  33.     private EntityRepositoryInterface $currencyRepository;
  34.     private EntityRepositoryInterface $customerGroupRepository;
  35.     private EntityRepositoryInterface $countryRepository;
  36.     private EntityRepositoryInterface $taxRepository;
  37.     private EntityRepositoryInterface $paymentMethodRepository;
  38.     private EntityRepositoryInterface $shippingMethodRepository;
  39.     private Connection $connection;
  40.     private EntityRepositoryInterface $countryStateRepository;
  41.     private EntityRepositoryInterface $currencyCountryRepository;
  42.     public function __construct(
  43.         EntityRepositoryInterface $salesChannelRepository,
  44.         EntityRepositoryInterface $currencyRepository,
  45.         EntityRepositoryInterface $customerGroupRepository,
  46.         EntityRepositoryInterface $countryRepository,
  47.         EntityRepositoryInterface $taxRepository,
  48.         EntityRepositoryInterface $paymentMethodRepository,
  49.         EntityRepositoryInterface $shippingMethodRepository,
  50.         Connection $connection,
  51.         EntityRepositoryInterface $countryStateRepository,
  52.         EntityRepositoryInterface $currencyCountryRepository
  53.     ) {
  54.         $this->salesChannelRepository $salesChannelRepository;
  55.         $this->currencyRepository $currencyRepository;
  56.         $this->countryRepository $countryRepository;
  57.         $this->taxRepository $taxRepository;
  58.         $this->paymentMethodRepository $paymentMethodRepository;
  59.         $this->shippingMethodRepository $shippingMethodRepository;
  60.         $this->connection $connection;
  61.         $this->countryStateRepository $countryStateRepository;
  62.         $this->currencyCountryRepository $currencyCountryRepository;
  63.         $this->customerGroupRepository $customerGroupRepository;
  64.     }
  65.     public function getDecorated(): AbstractBaseContextFactory
  66.     {
  67.         throw new DecorationPatternException(self::class);
  68.     }
  69.     public function create(string $salesChannelId, array $options = []): BaseContext
  70.     {
  71.         $context $this->getContext($salesChannelId$options);
  72.         $criteria = new Criteria([$salesChannelId]);
  73.         $criteria->setTitle('base-context-factory::sales-channel');
  74.         $criteria->addAssociation('currency');
  75.         $criteria->addAssociation('domains');
  76.         /** @var SalesChannelEntity|null $salesChannel */
  77.         $salesChannel $this->salesChannelRepository->search($criteria$context)
  78.             ->get($salesChannelId);
  79.         if (!$salesChannel) {
  80.             throw new \RuntimeException(sprintf('Sales channel with id %s not found or not valid!'$salesChannelId));
  81.         }
  82.         if (!Feature::isActive('FEATURE_NEXT_17276')) {
  83.             /*
  84.              * @deprecated tag:v6.5.0 - Overriding the languageId of the SalesChannel is deprecated and will be removed in v6.5.0
  85.              * use `$salesChannelContext->getLanguageId()` instead
  86.              */
  87.             if (\array_key_exists(SalesChannelContextService::LANGUAGE_ID$options)) {
  88.                 $salesChannel->setLanguageId($options[SalesChannelContextService::LANGUAGE_ID]);
  89.             }
  90.         }
  91.         //load active currency, fallback to shop currency
  92.         $currency $salesChannel->getCurrency();
  93.         if (\array_key_exists(SalesChannelContextService::CURRENCY_ID$options)) {
  94.             $currencyId $options[SalesChannelContextService::CURRENCY_ID];
  95.             $criteria = new Criteria([$currencyId]);
  96.             $criteria->setTitle('base-context-factory::currency');
  97.             $currency $this->currencyRepository->search($criteria$context)->get($currencyId);
  98.         }
  99.         //load not logged in customer with default shop configuration or with provided checkout scopes
  100.         $shippingLocation $this->loadShippingLocation($options$context$salesChannel);
  101.         $groupId $salesChannel->getCustomerGroupId();
  102.         /** @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed */
  103.         $groupIds array_unique([$salesChannel->getCustomerGroupId(), Defaults::FALLBACK_CUSTOMER_GROUP]);
  104.         $criteria = new Criteria($groupIds);
  105.         $criteria->setTitle('base-context-factory::customer-group');
  106.         $customerGroups $this->customerGroupRepository->search($criteria$context);
  107.         /** @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed */
  108.         $fallbackGroup $customerGroups->has(Defaults::FALLBACK_CUSTOMER_GROUP) ? $customerGroups->get(Defaults::FALLBACK_CUSTOMER_GROUP) : $customerGroups->get($salesChannel->getCustomerGroupId());
  109.         $customerGroup $customerGroups->get($groupId);
  110.         //loads tax rules based on active customer and delivery address
  111.         $taxRules $this->getTaxRules($context);
  112.         //detect active payment method, first check if checkout defined other payment method, otherwise validate if customer logged in, at least use shop default
  113.         $payment $this->getPaymentMethod($options$context$salesChannel);
  114.         //detect active delivery method, at first checkout scope, at least shop default method
  115.         $shippingMethod $this->getShippingMethod($options$context$salesChannel);
  116.         [$itemRounding$totalRounding] = $this->getCashRounding($currency$shippingLocation$context);
  117.         $context = new Context(
  118.             $context->getSource(),
  119.             [],
  120.             $currency->getId(),
  121.             $context->getLanguageIdChain(),
  122.             $context->getVersionId(),
  123.             $currency->getFactor(),
  124.             true,
  125.             CartPrice::TAX_STATE_GROSS,
  126.             $itemRounding
  127.         );
  128.         return new BaseContext(
  129.             $context,
  130.             $salesChannel,
  131.             $currency,
  132.             $customerGroup,
  133.             $fallbackGroup,
  134.             $taxRules,
  135.             $payment,
  136.             $shippingMethod,
  137.             $shippingLocation,
  138.             $itemRounding,
  139.             $totalRounding
  140.         );
  141.     }
  142.     private function getTaxRules(Context $context): TaxCollection
  143.     {
  144.         $criteria = new Criteria();
  145.         $criteria->setTitle('base-context-factory::taxes');
  146.         $criteria->addAssociation('rules.type');
  147.         $taxes $this->taxRepository->search($criteria$context)->getEntities();
  148.         return new TaxCollection($taxes);
  149.     }
  150.     private function getPaymentMethod(array $optionsContext $contextSalesChannelEntity $salesChannel): PaymentMethodEntity
  151.     {
  152.         $id $options[SalesChannelContextService::PAYMENT_METHOD_ID] ?? $salesChannel->getPaymentMethodId();
  153.         $criteria = (new Criteria([$id]))->addAssociation('media');
  154.         $criteria->setTitle('base-context-factory::payment-method');
  155.         $paymentMethod $this->paymentMethodRepository
  156.             ->search($criteria$context)
  157.             ->get($id);
  158.         if (!$paymentMethod) {
  159.             throw new UnknownPaymentMethodException($id);
  160.         }
  161.         return $paymentMethod;
  162.     }
  163.     private function getShippingMethod(array $optionsContext $contextSalesChannelEntity $salesChannel): ShippingMethodEntity
  164.     {
  165.         $id $options[SalesChannelContextService::SHIPPING_METHOD_ID] ?? $salesChannel->getShippingMethodId();
  166.         $ids array_unique(array_filter([$id$salesChannel->getShippingMethodId()]));
  167.         $criteria = new Criteria($ids);
  168.         $criteria->addAssociation('media');
  169.         $criteria->setTitle('base-context-factory::shipping-method');
  170.         $shippingMethods $this->shippingMethodRepository->search($criteria$context);
  171.         return $shippingMethods->get($id) ?? $shippingMethods->get($salesChannel->getShippingMethodId());
  172.     }
  173.     private function getContext(string $salesChannelId, array $session): Context
  174.     {
  175.         $sql '
  176.         # context-factory::base-context
  177.         SELECT
  178.           sales_channel.id as sales_channel_id,
  179.           sales_channel.language_id as sales_channel_default_language_id,
  180.           sales_channel.currency_id as sales_channel_currency_id,
  181.           currency.factor as sales_channel_currency_factor,
  182.           GROUP_CONCAT(LOWER(HEX(sales_channel_language.language_id))) as sales_channel_language_ids
  183.         FROM sales_channel
  184.             INNER JOIN currency
  185.                 ON sales_channel.currency_id = currency.id
  186.             LEFT JOIN sales_channel_language
  187.                 ON sales_channel_language.sales_channel_id = sales_channel.id
  188.         WHERE sales_channel.id = :id
  189.         GROUP BY sales_channel.id, sales_channel.language_id, sales_channel.currency_id, currency.factor';
  190.         $data $this->connection->fetchAssoc($sql, [
  191.             'id' => Uuid::fromHexToBytes($salesChannelId),
  192.         ]);
  193.         if ($data === false) {
  194.             throw new \RuntimeException(sprintf('No context data found for SalesChannel "%s"'$salesChannelId));
  195.         }
  196.         if (isset($session[SalesChannelContextService::ORIGINAL_CONTEXT])) {
  197.             $origin = new AdminSalesChannelApiSource($salesChannelId$session[SalesChannelContextService::ORIGINAL_CONTEXT]);
  198.         } else {
  199.             $origin = new SalesChannelApiSource($salesChannelId);
  200.         }
  201.         //explode all available languages for the provided sales channel
  202.         $languageIds $data['sales_channel_language_ids'] ? explode(','$data['sales_channel_language_ids']) : [];
  203.         $languageIds array_keys(array_flip($languageIds));
  204.         //check which language should be used in the current request (request header set, or context already contains a language - stored in `sales_channel_api_context`)
  205.         $defaultLanguageId Uuid::fromBytesToHex($data['sales_channel_default_language_id']);
  206.         $languageChain $this->buildLanguageChain($session$defaultLanguageId$languageIds);
  207.         $versionId Defaults::LIVE_VERSION;
  208.         if (isset($session[SalesChannelContextService::VERSION_ID])) {
  209.             $versionId $session[SalesChannelContextService::VERSION_ID];
  210.         }
  211.         return new Context(
  212.             $origin,
  213.             [],
  214.             Uuid::fromBytesToHex($data['sales_channel_currency_id']),
  215.             $languageChain,
  216.             $versionId,
  217.             (float) $data['sales_channel_currency_factor'],
  218.             true
  219.         );
  220.     }
  221.     private function getParentLanguageId(string $languageId): ?string
  222.     {
  223.         if (!Uuid::isValid($languageId)) {
  224.             throw new LanguageNotFoundException($languageId);
  225.         }
  226.         $data $this->connection->createQueryBuilder()
  227.             ->select(['LOWER(HEX(language.parent_id))'])
  228.             ->from('language')
  229.             ->where('language.id = :id')
  230.             ->setParameter('id'Uuid::fromHexToBytes($languageId))
  231.             ->execute()
  232.             ->fetchColumn();
  233.         if ($data === false) {
  234.             throw new LanguageNotFoundException($languageId);
  235.         }
  236.         return $data;
  237.     }
  238.     private function loadShippingLocation(array $optionsContext $contextSalesChannelEntity $salesChannel): ShippingLocation
  239.     {
  240.         //allows previewing cart calculation for a specify state for not logged in customers
  241.         if (isset($options[SalesChannelContextService::COUNTRY_STATE_ID])) {
  242.             $criteria = new Criteria([$options[SalesChannelContextService::COUNTRY_STATE_ID]]);
  243.             $criteria->addAssociation('country');
  244.             $criteria->setTitle('base-context-factory::country');
  245.             $state $this->countryStateRepository->search($criteria$context)
  246.                 ->get($options[SalesChannelContextService::COUNTRY_STATE_ID]);
  247.             return new ShippingLocation($state->getCountry(), $statenull);
  248.         }
  249.         $countryId $options[SalesChannelContextService::COUNTRY_ID] ?? $salesChannel->getCountryId();
  250.         $criteria = new Criteria([$countryId]);
  251.         $criteria->setTitle('base-context-factory::country');
  252.         $country $this->countryRepository->search($criteria$context)->get($countryId);
  253.         return ShippingLocation::createFromCountry($country);
  254.     }
  255.     /**
  256.      * @param array<string> $availableLanguageIds
  257.      *
  258.      * @return non-empty-array<string>
  259.      */
  260.     private function buildLanguageChain(array $sessionOptionsstring $defaultLanguageId, array $availableLanguageIds): array
  261.     {
  262.         $current $sessionOptions[SalesChannelContextService::LANGUAGE_ID] ?? $defaultLanguageId;
  263.         //check provided language is part of the available languages
  264.         if (!\in_array($current$availableLanguageIdstrue)) {
  265.             throw new \RuntimeException(
  266.                 sprintf('Provided language %s is not in list of available languages: %s'$currentimplode(', '$availableLanguageIds))
  267.             );
  268.         }
  269.         if ($current === Defaults::LANGUAGE_SYSTEM) {
  270.             return [Defaults::LANGUAGE_SYSTEM];
  271.         }
  272.         //provided language can be a child language
  273.         return array_filter([$current$this->getParentLanguageId($current), Defaults::LANGUAGE_SYSTEM]);
  274.     }
  275.     /**
  276.      * @return CashRoundingConfig[]
  277.      */
  278.     private function getCashRounding(CurrencyEntity $currencyShippingLocation $shippingLocationContext $context): array
  279.     {
  280.         $criteria = new Criteria();
  281.         $criteria->setTitle('base-context-factory::cash-rounding');
  282.         $criteria->setLimit(1);
  283.         $criteria->addFilter(new EqualsFilter('currencyId'$currency->getId()));
  284.         $criteria->addFilter(new EqualsFilter('countryId'$shippingLocation->getCountry()->getId()));
  285.         /** @var CurrencyCountryRoundingEntity|null $countryConfig */
  286.         $countryConfig $this->currencyCountryRepository->search($criteria$context)->first();
  287.         if ($countryConfig) {
  288.             return [$countryConfig->getItemRounding(), $countryConfig->getTotalRounding()];
  289.         }
  290.         return [$currency->getItemRounding(), $currency->getTotalRounding()];
  291.     }
  292. }