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

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