vendor/store.shopware.com/zenitplatformdatabadges/src/Subscriber/ThemeVariablesSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformDataBadges\Subscriber;
  3. use Shopware\Core\DevOps\Environment\EnvironmentHelper;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ThemeVariablesSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var string
  11.      */
  12.     private $configPath 'zenitPlatformDataBadges.config.';
  13.     /**
  14.      * @var SystemConfigService
  15.      */
  16.     protected $systemConfig;
  17.     // add the `SystemConfigService` to your constructor
  18.     public function __construct(SystemConfigService $systemConfig)
  19.     {
  20.         $this->systemConfig $systemConfig;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
  26.         ];
  27.     }
  28.     public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
  29.     {
  30.         $dsn trim((string) (EnvironmentHelper::getVariable('DATABASE_URL'getenv('DATABASE_URL'))));
  31.         if ($dsn === '' || $dsn === 'mysql://_placeholder.test') {
  32.             // deployment server without database
  33.             return;
  34.         }
  35.         $shopId $event->getSalesChannelId();
  36.         /**
  37.          * @var string $variantBadgeColor
  38.          * @var string $stockBadgeColor
  39.          * @var string $salesBadgeColor
  40.          * @var string $discountBadgeColor
  41.          * @var string $closeoutBadgeColor
  42.          * @var string $soldOutBadgeColor
  43.          * @var string $shippingfreeBadgeColor
  44.          * @var string $topsellerBadgeColor
  45.          * @var string $newBadgeColor
  46.          * @var string $releaseBadgeColor
  47.          * @var string $preorderBadgeColor
  48.          * @var string $manufacturerBadgeColor
  49.          * @var string $custom1BadgeColor
  50.          * @var string $custom2BadgeColor
  51.          * @var string $custom3BadgeColor
  52.          */
  53.         $variantBadgeColor $this->systemConfig->get($this->configPath 'variantBadgeColor'$shopId);
  54.         $stockBadgeColor $this->systemConfig->get($this->configPath 'stockBadgeColor'$shopId);
  55.         $salesBadgeColor $this->systemConfig->get($this->configPath 'salesBadgeColor'$shopId);
  56.         $discountBadgeColor $this->systemConfig->get($this->configPath 'discountBadgeColor'$shopId);
  57.         $closeoutBadgeColor $this->systemConfig->get($this->configPath 'closeoutBadgeColor'$shopId);
  58.         $soldOutBadgeColor $this->systemConfig->get($this->configPath 'soldOutBadgeColor'$shopId);
  59.         $shippingfreeBadgeColor $this->systemConfig->get($this->configPath 'shippingfreeBadgeColor'$shopId);
  60.         $topsellerBadgeColor $this->systemConfig->get($this->configPath 'topsellerBadgeColor'$shopId);
  61.         $newBadgeColor $this->systemConfig->get($this->configPath 'newBadgeColor'$shopId);
  62.         $releaseBadgeColor $this->systemConfig->get($this->configPath 'releaseBadgeColor'$shopId);
  63.         $preorderBadgeColor $this->systemConfig->get($this->configPath 'preorderBadgeColor'$shopId);
  64.         $manufacturerBadgeColor $this->systemConfig->get($this->configPath 'manufacturerBadgeColor'$shopId);
  65.         $custom1BadgeColor $this->systemConfig->get($this->configPath 'custom1BadgeColor'$shopId);
  66.         $custom2BadgeColor $this->systemConfig->get($this->configPath 'custom2BadgeColor'$shopId);
  67.         $custom3BadgeColor $this->systemConfig->get($this->configPath 'custom3BadgeColor'$shopId);
  68.         if (isset($variantBadgeColor) && !empty($variantBadgeColor)) {
  69.             $event->addVariable('zen-data-badges-variant-color'$variantBadgeColor);
  70.         } else {
  71.             $event->addVariable('zen-data-badges-variant-color''#bcc1c7');
  72.         };
  73.         if (isset($stockBadgeColor) && !empty($stockBadgeColor)) {
  74.             $event->addVariable('zen-data-badges-stock-color'$stockBadgeColor);
  75.         } else {
  76.             $event->addVariable('zen-data-badges-stock-color''#5f7285');
  77.         };
  78.         if (isset($salesBadgeColor) && !empty($salesBadgeColor)) {
  79.             $event->addVariable('zen-data-badges-sales-color'$salesBadgeColor);
  80.         } else {
  81.             $event->addVariable('zen-data-badges-sales-color''#e52427');
  82.         };
  83.         if (isset($discountBadgeColor) && !empty($discountBadgeColor)) {
  84.             $event->addVariable('zen-data-badges-discount-color'$discountBadgeColor);
  85.         } else {
  86.             $event->addVariable('zen-data-badges-discount-color''#e52427');
  87.         };
  88.         if (isset($closeoutBadgeColor) && !empty($closeoutBadgeColor)) {
  89.             $event->addVariable('zen-data-badges-close-out-color'$closeoutBadgeColor);
  90.         } else {
  91.             $event->addVariable('zen-data-badges-close-out-color''#e74c3c');
  92.         };
  93.         if (isset($soldOutBadgeColor) && !empty($soldOutBadgeColor)) {
  94.             $event->addVariable('zen-data-badges-sold-out-color'$soldOutBadgeColor);
  95.         } else {
  96.             $event->addVariable('zen-data-badges-sold-out-color''#e74c3c');
  97.         };
  98.         if (isset($shippingfreeBadgeColor) && !empty($shippingfreeBadgeColor)) {
  99.             $event->addVariable('zen-data-badges-shipping-free-color'$shippingfreeBadgeColor);
  100.         } else {
  101.             $event->addVariable('zen-data-badges-shipping-free-color''#2ecc71');
  102.         };
  103.         if (isset($topsellerBadgeColor) && !empty($topsellerBadgeColor)) {
  104.             $event->addVariable('zen-data-badges-topseller-color'$topsellerBadgeColor);
  105.         } else {
  106.             $event->addVariable('zen-data-badges-topseller-color''#ffbd5d');
  107.         };
  108.         if (isset($newBadgeColor) && !empty($newBadgeColor)) {
  109.             $event->addVariable('zen-data-badges-new-color'$newBadgeColor);
  110.         } else {
  111.             $event->addVariable('zen-data-badges-new-color''#3cc261');
  112.         };
  113.         if (isset($releaseBadgeColor) && !empty($releaseBadgeColor)) {
  114.             $event->addVariable('zen-data-badges-release-color'$releaseBadgeColor);
  115.         } else {
  116.             $event->addVariable('zen-data-badges-release-color''#ffbd5d');
  117.         };
  118.         if (isset($preorderBadgeColor) && !empty($preorderBadgeColor)) {
  119.             $event->addVariable('zen-data-badges-preorder-color'$preorderBadgeColor);
  120.         } else {
  121.             $event->addVariable('zen-data-badges-preorder-color''#ffbd5d');
  122.         };
  123.         if (isset($manufacturerBadgeColor) && !empty($manufacturerBadgeColor)) {
  124.             $event->addVariable('zen-data-badges-manufacturer-color'$manufacturerBadgeColor);
  125.         } else {
  126.             $event->addVariable('zen-data-badges-manufacturer-color''#5f7285');
  127.         };
  128.         if (isset($custom1BadgeColor) && !empty($custom1BadgeColor)) {
  129.             $event->addVariable('zen-data-badges-custom-first-color'$custom1BadgeColor);
  130.         } else {
  131.             $event->addVariable('zen-data-badges-custom-first-color''#f1c40f');
  132.         };
  133.         if (isset($custom2BadgeColor) && !empty($custom2BadgeColor)) {
  134.             $event->addVariable('zen-data-badges-custom-second-color'$custom2BadgeColor);
  135.         } else {
  136.             $event->addVariable('zen-data-badges-custom-second-color''#f1c40f');
  137.         };
  138.         if (isset($custom3BadgeColor) && !empty($custom3BadgeColor)) {
  139.             $event->addVariable('zen-data-badges-custom-third-color'$custom3BadgeColor);
  140.         } else {
  141.             $event->addVariable('zen-data-badges-custom-third-color''#f1c40f');
  142.         };
  143.         /**
  144.          * @var string $listingsFontSize
  145.          * @var string $listingsRadiusLeft
  146.          * @var string $listingsRadiusRight
  147.          * @var string $listingsPadding
  148.          * @var string $listingsMarginLeft
  149.          */
  150.         $listingsFontSize $this->systemConfig->get($this->configPath 'listingsFontSize'$shopId);
  151.         $listingsRadiusLeft $this->systemConfig->get($this->configPath 'listingsRadiusLeft'$shopId) ?? '0';
  152.         $listingsRadiusRight $this->systemConfig->get($this->configPath 'listingsRadiusRight'$shopId) ?? '0';
  153.         $listingsPadding $this->systemConfig->get($this->configPath 'listingsPadding'$shopId) ?? '0';
  154.         $listingsPaddingVertical = ($listingsPadding >= 5) ? (int)$listingsPadding '0';
  155.         $listingsMarginLeft = ($listingsRadiusLeft 0) ? '10' '0';
  156.         $event->addVariable('zen-data-badges-listings-font-size'$listingsFontSize 'px');
  157.         $event->addVariable('zen-data-badges-listings-height', (int)$listingsFontSize 'px');
  158.         $event->addVariable('zen-data-badges-listings-radius-left'$listingsRadiusLeft 'px');
  159.         $event->addVariable('zen-data-badges-listings-radius-right'$listingsRadiusRight 'px');
  160.         $event->addVariable('zen-data-badges-listings-padding-horizontal'$listingsPadding 'px');
  161.         $event->addVariable('zen-data-badges-listings-padding-vertical'$listingsPaddingVertical'px');
  162.         $event->addVariable('zen-data-badges-listings-margin-left'$listingsMarginLeft 'px');
  163.         /**
  164.          * @var string $detailsFontSize
  165.          * @var string $detailsRadiusLeft
  166.          * @var string $detailsRadiusRight
  167.          * @var string $detailsPadding
  168.          * @var string $detailsMarginLeft
  169.          */
  170.         $detailsFontSize $this->systemConfig->get($this->configPath 'detailsFontSize'$shopId);
  171.         $detailsRadiusLeft $this->systemConfig->get($this->configPath 'detailsRadiusLeft'$shopId) ?? '0';
  172.         $detailsRadiusRight $this->systemConfig->get($this->configPath 'detailsRadiusRight'$shopId) ?? '0';
  173.         $detailsPadding $this->systemConfig->get($this->configPath 'detailsPadding'$shopId) ?? '0';
  174.         $detailsPaddingVertical = ($detailsPadding >= 5) ? (int)$detailsPadding '0';
  175.         $detailsMarginLeft = ($detailsRadiusLeft 0) ? '10' '0';
  176.         $event->addVariable('zen-data-badges-details-font-size'$detailsFontSize 'px');
  177.         $event->addVariable('zen-data-badges-details-height', (int)$detailsFontSize 'px');
  178.         $event->addVariable('zen-data-badges-details-radius-left'$detailsRadiusLeft 'px');
  179.         $event->addVariable('zen-data-badges-details-radius-right'$detailsRadiusRight 'px');
  180.         $event->addVariable('zen-data-badges-details-padding-horizontal'$detailsPadding 'px');
  181.         $event->addVariable('zen-data-badges-details-padding-vertical'$detailsPaddingVertical 'px');
  182.         $event->addVariable('zen-data-badges-details-margin-left'$detailsMarginLeft 'px');
  183.     }
  184. }