src/Storefront/Theme/Subscriber/FirstRunWizardSubscriber.php line 39

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