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

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