src/Core/Checkout/Document/Service/DocumentConfigLoader.php line 71

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Document\Service;
  3. use Shopware\Core\Checkout\Document\Aggregate\DocumentBaseConfig\DocumentBaseConfigCollection;
  4. use Shopware\Core\Checkout\Document\Aggregate\DocumentBaseConfig\DocumentBaseConfigEntity;
  5. use Shopware\Core\Checkout\Document\DocumentConfiguration;
  6. use Shopware\Core\Checkout\Document\DocumentConfigurationFactory;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Log\Package;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Contracts\Service\ResetInterface;
  14. #[Package('customer-order')]
  15. final class DocumentConfigLoader implements EventSubscriberInterfaceResetInterface
  16. {
  17.     /**
  18.      * @var array<string, array<string, DocumentConfiguration>>
  19.      */
  20.     private array $configs = [];
  21.     /**
  22.      * @internal
  23.      */
  24.     public function __construct(private readonly EntityRepository $documentConfigRepository)
  25.     {
  26.     }
  27.     /**
  28.      * @internal
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'document_base_config.written' => 'reset',
  34.         ];
  35.     }
  36.     public function load(string $documentTypestring $salesChannelIdContext $context): DocumentConfiguration
  37.     {
  38.         if (!empty($this->configs[$documentType][$salesChannelId])) {
  39.             return $this->configs[$documentType][$salesChannelId];
  40.         }
  41.         $criteria = new Criteria();
  42.         $criteria->addFilter(new EqualsFilter('documentType.technicalName'$documentType));
  43.         $criteria->addAssociation('logo');
  44.         $criteria->getAssociation('salesChannels')->addFilter(new EqualsFilter('salesChannelId'$salesChannelId));
  45.         /** @var DocumentBaseConfigCollection $documentConfigs */
  46.         $documentConfigs $this->documentConfigRepository->search($criteria$context)->getEntities();
  47.         $globalConfig $documentConfigs->filterByProperty('global'true)->first();
  48.         $salesChannelConfig $documentConfigs->filter(fn (DocumentBaseConfigEntity $config) => $config->getSalesChannels()->count() > 0)->first();
  49.         $config DocumentConfigurationFactory::createConfiguration([], $globalConfig$salesChannelConfig);
  50.         $this->configs[$documentType] ??= [];
  51.         return $this->configs[$documentType][$salesChannelId] = $config;
  52.     }
  53.     /**
  54.      * @internal
  55.      */
  56.     public function reset(): void
  57.     {
  58.         $this->configs = [];
  59.     }
  60. }