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

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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Contracts\Service\ResetInterface;
  13. /**
  14.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  15.  */
  16. final class DocumentConfigLoader implements EventSubscriberInterfaceResetInterface
  17. {
  18.     /**
  19.      * @var array<string, array<string, DocumentConfiguration>>
  20.      */
  21.     private array $configs = [];
  22.     private EntityRepositoryInterface $documentConfigRepository;
  23.     /**
  24.      * @internal
  25.      */
  26.     public function __construct(EntityRepositoryInterface $documentConfigRepository)
  27.     {
  28.         $this->documentConfigRepository $documentConfigRepository;
  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(function (DocumentBaseConfigEntity $config) {
  49.             return $config->getSalesChannels()->count() > 0;
  50.         })->first();
  51.         $config DocumentConfigurationFactory::createConfiguration([], $globalConfig$salesChannelConfig);
  52.         $this->configs[$documentType] = $this->configs[$documentType] ?? [];
  53.         return $this->configs[$documentType][$salesChannelId] = $config;
  54.     }
  55.     public function reset(): void
  56.     {
  57.         $this->configs = [];
  58.     }
  59. }