<?php declare(strict_types=1);
namespace zenit\PlatformStratus\Subscriber;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Theme\ThemeConfigValueAccessor;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
class StorefrontRenderSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $pluginName = 'zenitPlatformStratus';
/**
* @var ThemeConfigValueAccessor
*/
private $themeConfigAccessor;
/**
* @var EntityRepositoryInterface
*/
private $themeRepository;
/**
* @var string
*/
private $themeId;
public function __construct(ThemeConfigValueAccessor $themeConfigAccessor, EntityRepositoryInterface $themeRepository)
{
$this->themeConfigAccessor = $themeConfigAccessor;
$this->themeRepository = $themeRepository;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
StorefrontRenderEvent::class => 'onStorefrontRender',
ProductSuggestCriteriaEvent::class => 'handleSuggestRequest',
ProductListingCriteriaEvent::class => 'handleListingRequest',
ProductSearchCriteriaEvent::class => 'handleSearchRequest',
WishListPageProductCriteriaEvent::class => 'handleWishlistRequest'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$config = [];
$config['system'] = 'platform';
$event->getContext()->addExtension($this->pluginName, new ArrayEntity($config));
}
public function handleListingRequest(ProductListingCriteriaEvent $event)
{
$context = $event->getContext();
$themeId = $this->getThemeIdByTechnicalName($this->pluginName, $context);
$this->themeId = $themeId;
$salesChannelContext = $event->getSalesChannelContext();
$cardImgSwitch = $this->themeConfigAccessor->get('zen-product-listing-card-img-switch', $salesChannelContext, $this->themeId );
if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
$criteria = $event->getCriteria();
$criteria->addAssociation('media');
}
}
public function handleSuggestRequest(ProductSuggestCriteriaEvent $event)
{
$context = $event->getContext();
$themeId = $this->getThemeIdByTechnicalName($this->pluginName, $context);
$this->themeId = $themeId;
$salesChannelContext = $event->getSalesChannelContext();
$cardImgSwitch = $this->themeConfigAccessor->get('zen-product-listing-card-img-switch', $salesChannelContext, $this->themeId );
if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
$criteria = $event->getCriteria();
$criteria->addAssociation('media');
}
}
public function handleSearchRequest(ProductSearchCriteriaEvent $event)
{
$context = $event->getContext();
$themeId = $this->getThemeIdByTechnicalName($this->pluginName, $context);
$this->themeId = $themeId;
$salesChannelContext = $event->getSalesChannelContext();
$cardImgSwitch = $this->themeConfigAccessor->get('zen-product-listing-card-img-switch', $salesChannelContext, $this->themeId );
if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
$criteria = $event->getCriteria();
$criteria->addAssociation('media');
}
}
public function handleWishlistRequest(WishListPageProductCriteriaEvent $event)
{
$context = $event->getContext();
$themeId = $this->getThemeIdByTechnicalName($this->pluginName, $context);
$this->themeId = $themeId;
$salesChannelContext = $event->getSalesChannelContext();
$cardImgSwitch = $this->themeConfigAccessor->get('zen-product-listing-card-img-switch', $salesChannelContext, $this->themeId );
if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
$criteria = $event->getCriteria();
$criteria->addAssociation('media');
}
}
private function getThemeIdByTechnicalName(string $technicalName, Context $context): ?string
{
return $this->themeRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('technicalName', $technicalName)),
$context
)->firstId();
}
}