custom/plugins/MolliePayments/src/Subscriber/StorefrontBuildSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kiener\MolliePayments\Subscriber;
  4. use Kiener\MolliePayments\Compatibility\VersionCompare;
  5. use Kiener\MolliePayments\Service\SettingsService;
  6. use Shopware\Storefront\Event\StorefrontRenderEvent;
  7. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\StorefrontPluginConfiguration;
  8. use Shopware\Storefront\Theme\StorefrontPluginRegistry;
  9. use Shopware\Storefront\Theme\StorefrontPluginRegistryInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class StorefrontBuildSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SettingsService
  15.      */
  16.     private $settingsService;
  17.     /** @phpstan-ignore class.notFound */
  18.     /**
  19.      * @var StorefrontPluginRegistry|StorefrontPluginRegistryInterface
  20.      */
  21.     private $pluginRegistry;
  22.     /**
  23.      * @var VersionCompare
  24.      */
  25.     private $versionCompare;
  26.     /** @phpstan-ignore class.notFound */
  27.     /** @param StorefrontPluginRegistry|StorefrontPluginRegistryInterface $pluginRegistry */
  28.     public function __construct(SettingsService $settingsService$pluginRegistryVersionCompare $versionCompare)
  29.     {
  30.         $this->settingsService $settingsService;
  31.         $this->pluginRegistry $pluginRegistry;
  32.         $this->versionCompare $versionCompare;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             StorefrontRenderEvent::class => 'onStorefrontRender',
  38.         ];
  39.     }
  40.     /**
  41.      * @throws \Exception
  42.      */
  43.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  44.     {
  45.         $settings $this->settingsService->getSettings($event->getSalesChannelContext()->getSalesChannel()->getId());
  46.         $useJsValue = (int) $settings->isUseShopwareJavascript();
  47.         $event->setParameter('mollie_javascript_use_shopware'$useJsValue);
  48.         $mollieJavascriptAlreadyExists false;
  49.         if ($this->versionCompare->gte('6.6')) {
  50.             /** @phpstan-ignore class.notFound */
  51.             $plugin $this->pluginRegistry->getConfigurations()->getByTechnicalName('MolliePayments');
  52.             if ($plugin instanceof StorefrontPluginConfiguration) {
  53.                 $scriptFiles $plugin->getScriptFiles();
  54.                 if ($useJsValue === 0) {
  55.                     $scriptFiles->remove(0);
  56.                 }
  57.                 $mollieJavascriptAlreadyExists $scriptFiles->count() > 0;
  58.             }
  59.         }
  60.         $event->setParameter('mollie_javascript_already_exists'$mollieJavascriptAlreadyExists);
  61.     }
  62. }