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

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