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

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