platform/src/Storefront/Theme/Subscriber/FirstRunWizardSubscriber.php line 48

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.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             FirstRunWizardFinishedEvent::class => 'frwFinished',
  36.         ];
  37.     }
  38.     public function frwFinished(FirstRunWizardFinishedEvent $event): void
  39.     {
  40.         // only run on open -> completed|failed transition
  41.         if (!$event->getPreviousState()->isOpen() || $event->getState()->isOpen()) {
  42.             return;
  43.         }
  44.         $context $event->getContext();
  45.         $this->themeLifecycleService->refreshThemes($context);
  46.         $criteria = new Criteria();
  47.         $criteria->addAssociation('salesChannels');
  48.         $criteria->addFilter(new EqualsFilter('technicalName''Storefront'));
  49.         /** @var ThemeEntity|null $theme */
  50.         $theme $this->themeRepository->search($criteria$context)->first();
  51.         if (!$theme) {
  52.             throw new \RuntimeException('Default theme not found');
  53.         }
  54.         $themeSalesChannels $theme->getSalesChannels();
  55.         // only run if the themes are not already initialised
  56.         if ($themeSalesChannels && $themeSalesChannels->count() > 0) {
  57.             return;
  58.         }
  59.         $criteria = new Criteria();
  60.         $criteria->addFilter(new EqualsFilter('typeId'Defaults::SALES_CHANNEL_TYPE_STOREFRONT));
  61.         $salesChannelIds $this->salesChannelRepository->search($criteria$context)->getIds();
  62.         foreach ($salesChannelIds as $id) {
  63.             $this->themeService->compileTheme($id$theme->getId(), $context);
  64.             $this->themeSalesChannelRepository->upsert([[
  65.                 'themeId' => $theme->getId(),
  66.                 'salesChannelId' => $id,
  67.             ]], $context);
  68.         }
  69.     }
  70. }