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

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