vendor/shopware/storefront/Theme/Subscriber/FirstRunWizardSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Subscriber;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Store\Event\FirstRunWizardFinishedEvent;
  9. use Shopware\Storefront\Theme\ThemeEntity;
  10. use Shopware\Storefront\Theme\ThemeLifecycleService;
  11. use Shopware\Storefront\Theme\ThemeService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  15.  */
  16. #[Package('storefront')]
  17. class FirstRunWizardSubscriber implements EventSubscriberInterface
  18. {
  19.     private ThemeService $themeService;
  20.     private ThemeLifecycleService $themeLifecycleService;
  21.     private EntityRepositoryInterface $themeRepository;
  22.     private EntityRepositoryInterface $themeSalesChannelRepository;
  23.     private EntityRepositoryInterface $salesChannelRepository;
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         ThemeService $themeService,
  29.         ThemeLifecycleService $themeLifecycleService,
  30.         EntityRepositoryInterface $themeRepository,
  31.         EntityRepositoryInterface $themeSalesChannelRepository,
  32.         EntityRepositoryInterface $salesChannelRepository
  33.     ) {
  34.         $this->themeService $themeService;
  35.         $this->themeLifecycleService $themeLifecycleService;
  36.         $this->themeRepository $themeRepository;
  37.         $this->themeSalesChannelRepository $themeSalesChannelRepository;
  38.         $this->salesChannelRepository $salesChannelRepository;
  39.     }
  40.     /**
  41.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  42.      */
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             FirstRunWizardFinishedEvent::class => 'frwFinished',
  47.         ];
  48.     }
  49.     public function frwFinished(FirstRunWizardFinishedEvent $event): void
  50.     {
  51.         // only run on open -> completed|failed transition
  52.         if (!$event->getPreviousState()->isOpen() || $event->getState()->isOpen()) {
  53.             return;
  54.         }
  55.         $context $event->getContext();
  56.         $this->themeLifecycleService->refreshThemes($context);
  57.         $criteria = new Criteria();
  58.         $criteria->addAssociation('salesChannels');
  59.         $criteria->addFilter(new EqualsFilter('technicalName''Storefront'));
  60.         /** @var ThemeEntity|null $theme */
  61.         $theme $this->themeRepository->search($criteria$context)->first();
  62.         if (!$theme) {
  63.             throw new \RuntimeException('Default theme not found');
  64.         }
  65.         $themeSalesChannels $theme->getSalesChannels();
  66.         // only run if the themes are not already initialised
  67.         if ($themeSalesChannels && $themeSalesChannels->count() > 0) {
  68.             return;
  69.         }
  70.         $criteria = new Criteria();
  71.         $criteria->addFilter(new EqualsFilter('typeId'Defaults::SALES_CHANNEL_TYPE_STOREFRONT));
  72.         $salesChannelIds $this->salesChannelRepository->search($criteria$context)->getIds();
  73.         foreach ($salesChannelIds as $id) {
  74.             $this->themeService->compileTheme($id$theme->getId(), $context);
  75.             $this->themeSalesChannelRepository->upsert([[
  76.                 'themeId' => $theme->getId(),
  77.                 'salesChannelId' => $id,
  78.             ]], $context);
  79.         }
  80.     }
  81. }