src/Core/System/SalesChannel/Context/BaseContextFactory.php line 90

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