custom/plugins/NenoMarketingEssentials/src/Storefront/Service/ConversionBarLoader.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neno\MarketingEssentials\Storefront\Service;
  3. use Neno\MarketingEssentials\Core\Content\ConversionBar\ConversionBarEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. class ConversionBarLoader {
  11.     private EntityRepositoryInterface $conversionBarRepository;
  12.     public function __construct (EntityRepositoryInterface $conversionBarRepository)
  13.     {
  14.         $this->conversionBarRepository $conversionBarRepository;
  15.     }
  16.     private function fetchDefaultConfig(Context $context): ConversionBarEntity {
  17.         // default config matches all sales channels
  18.         $criteria = (new Criteria())
  19.             ->addFilter(new EqualsFilter('salesChannelId',null));
  20.         $defaultBarResult $this->conversionBarRepository
  21.             ->search($criteria$context)
  22.             ->getEntities();
  23.         if ($defaultBarResult->count()) {
  24.             return $defaultBarResult->first();
  25.         } else {
  26.             return new ConversionBarEntity();
  27.         }
  28.     }
  29.     public function loadForSalesChannel(Context $contextSalesChannelContext $salesChannelContext): EntityCollection {
  30.         $salesChannelId $salesChannelContext->getSalesChannelId();
  31.         $defaultBarResult $this->fetchDefaultConfig($context);
  32.         // per sales channel config
  33.         $criteria = (new Criteria())
  34.             ->addFilter(
  35.                 new EqualsFilter(
  36.                     'salesChannelId',
  37.                     $salesChannelId
  38.                 ),
  39.             );
  40.         /** @var ConversionBarEntity|null $barResult */
  41.         $barResult $this->conversionBarRepository
  42.             ->search($criteria$context)
  43.             ->getEntities()
  44.             ->first();
  45.         if ($barResult) {
  46.             // merge with defaults
  47.             if ($barResult->getBackgroundColor() === null) {
  48.                 $barResult->setBackgroundColor($defaultBarResult->getBackgroundColor());
  49.             }
  50.             if ($barResult->getSliderMaxWidth() === null) {
  51.                 $barResult->setSliderMaxWidth($defaultBarResult->getSliderMaxWidth());
  52.             }
  53.             if ($barResult->getTextColor() === null) {
  54.                 $barResult->setTextColor($defaultBarResult->getTextColor());
  55.             }
  56.             if ($barResult->getLinkColor() === null) {
  57.                 $barResult->setLinkColor($defaultBarResult->getLinkColor());
  58.             }
  59.             return new EntityCollection([$barResult]);
  60.         } else {
  61.             return new EntityCollection([$defaultBarResult]);
  62.         }
  63.     }
  64. }