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

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Subscriber;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  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.  * @internal
  15.  */
  16. #[Package('storefront')]
  17. class FirstRunWizardSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         private readonly ThemeService $themeService,
  24.         private readonly ThemeLifecycleService $themeLifecycleService,
  25.         private readonly EntityRepository $themeRepository,
  26.         private readonly EntityRepository $themeSalesChannelRepository,
  27.         private readonly EntityRepository $salesChannelRepository
  28.     ) {
  29.     }
  30.     /**
  31.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             FirstRunWizardFinishedEvent::class => 'frwFinished',
  37.         ];
  38.     }
  39.     public function frwFinished(FirstRunWizardFinishedEvent $event): void
  40.     {
  41.         // only run on open -> completed|failed transition
  42.         if (!$event->getPreviousState()->isOpen() || $event->getState()->isOpen()) {
  43.             return;
  44.         }
  45.         $context $event->getContext();
  46.         $this->themeLifecycleService->refreshThemes($context);
  47.         $themeCriteria = new Criteria();
  48.         $themeCriteria->addAssociation('salesChannels');
  49.         $themeCriteria->addFilter(new EqualsFilter('technicalName''Storefront'));
  50.         /** @var ThemeEntity|null $theme */
  51.         $theme $this->themeRepository->search($themeCriteria$context)->first();
  52.         if (!$theme) {
  53.             throw new \RuntimeException('Default theme not found');
  54.         }
  55.         $themeSalesChannels $theme->getSalesChannels();
  56.         // only run if the themes are not already initialised
  57.         if ($themeSalesChannels && $themeSalesChannels->count() > 0) {
  58.             return;
  59.         }
  60.         $salesChannelCriteria = new Criteria();
  61.         $salesChannelCriteria->addFilter(new EqualsFilter('typeId'Defaults::SALES_CHANNEL_TYPE_STOREFRONT));
  62.         $salesChannelIds $this->salesChannelRepository->search($salesChannelCriteria$context)->getIds();
  63.         foreach ($salesChannelIds as $id) {
  64.             $this->themeService->compileTheme($id$theme->getId(), $context);
  65.             $this->themeSalesChannelRepository->upsert([[
  66.                 'themeId' => $theme->getId(),
  67.                 'salesChannelId' => $id,
  68.             ]], $context);
  69.         }
  70.     }
  71. }