vendor/shopware/core/Checkout/Document/Service/DocumentConfigLoader.php line 73

Open in your IDE?
  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\EntityRepositoryInterface;
  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. /**
  15.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  16.  */
  17. #[Package('customer-order')]
  18. final class DocumentConfigLoader implements EventSubscriberInterfaceResetInterface
  19. {
  20.     /**
  21.      * @var array<string, array<string, DocumentConfiguration>>
  22.      */
  23.     private array $configs = [];
  24.     private EntityRepositoryInterface $documentConfigRepository;
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(EntityRepositoryInterface $documentConfigRepository)
  29.     {
  30.         $this->documentConfigRepository $documentConfigRepository;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             'document_base_config.written' => 'reset',
  36.         ];
  37.     }
  38.     public function load(string $documentTypestring $salesChannelIdContext $context): DocumentConfiguration
  39.     {
  40.         if (!empty($this->configs[$documentType][$salesChannelId])) {
  41.             return $this->configs[$documentType][$salesChannelId];
  42.         }
  43.         $criteria = new Criteria();
  44.         $criteria->addFilter(new EqualsFilter('documentType.technicalName'$documentType));
  45.         $criteria->addAssociation('logo');
  46.         $criteria->getAssociation('salesChannels')->addFilter(new EqualsFilter('salesChannelId'$salesChannelId));
  47.         /** @var DocumentBaseConfigCollection $documentConfigs */
  48.         $documentConfigs $this->documentConfigRepository->search($criteria$context)->getEntities();
  49.         $globalConfig $documentConfigs->filterByProperty('global'true)->first();
  50.         $salesChannelConfig $documentConfigs->filter(function (DocumentBaseConfigEntity $config) {
  51.             return $config->getSalesChannels()->count() > 0;
  52.         })->first();
  53.         $config DocumentConfigurationFactory::createConfiguration([], $globalConfig$salesChannelConfig);
  54.         $this->configs[$documentType] = $this->configs[$documentType] ?? [];
  55.         return $this->configs[$documentType][$salesChannelId] = $config;
  56.     }
  57.     public function reset(): void
  58.     {
  59.         $this->configs = [];
  60.     }
  61. }