<?php declare(strict_types = 1);
namespace Zeobv\GetNotified\Storefront\Subscriber;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Zeobv\GetNotified\Service\StockSubscriptionService;
use Zeobv\GetNotified\Struct\GetNotifiedConfigBuilder;
/**
* Class PageSubscriber
*
* @package Zeobv\GetNotified\Storefront\Subscriber
*/
class PageSubscriber implements EventSubscriberInterface
{
protected StockSubscriptionService $stockSubscriptionService;
public function __construct(
StockSubscriptionService $stockSubscriptionService
) {
$this->stockSubscriptionService = $stockSubscriptionService;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
$subscribedEvents = [
'sales_channel.product.loaded' => 'onProductsLoaded',
ProductPageLoadedEvent::class => 'onProductPageLoaded',
NavigationPageLoadedEvent::class => 'onListingPageLoaded',
WishlistPageLoadedEvent::class => 'onListingPageLoaded',
SearchPageLoadedEvent::class => 'onListingPageLoaded',
];
# Compatibility check for SwagCmsExtensions quickview pagelet
if (class_exists(QuickviewPageletLoadedEvent::class)) {
$subscribedEvents = array_merge($subscribedEvents, [
QuickviewPageletLoadedEvent::class => 'onQuickviewPageletLoaded',
]);
}
return $subscribedEvents;
}
/**
* Add notification form config information for product listings such as Navigation, Search pages and product sliders
*
* @param SalesChannelEntityLoadedEvent $event
*/
public function onProductsLoaded(SalesChannelEntityLoadedEvent $event): void
{
$salesChannelContext = $event->getSalesChannelContext();
if (!$this->stockSubscriptionService->shouldShowSubscriptionForm($salesChannelContext, true)) {
return;
}
/** @var SalesChannelProductEntity $product */
foreach ($event->getEntities() as $product) {
$product->addExtension(
'zeobvGetNotifiedConfig',
(new GetNotifiedConfigBuilder(
$this->stockSubscriptionService,
$salesChannelContext,
$product
))->build()
);
}
}
/**
* @param NavigationPageLoadedEvent|SearchPageLoadedEvent $event
*
* @return void
*/
public function onListingPageLoaded($event): void
{
$hideForm = $this->stockSubscriptionService->getHideStockSubscriptionFormOnListingPage($event->getSalesChannelContext()->getSalesChannel());
$event->getSalesChannelContext()->addExtension('zeobvGetNotifiedEnabled', new ArrayStruct(['enabled' => !$hideForm]));
}
/**
* @param ProductPageLoadedEvent $event
*/
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$salesChannelContext = $event->getSalesChannelContext();
if (!$this->stockSubscriptionService->shouldShowSubscriptionForm($salesChannelContext, false)) {
return;
}
$product = $event->getPage()->getProduct();
if ($product->hasExtension('zeobvGetNotifiedConfig')) {
return;
}
$event->getPage()->getProduct()->addExtension(
'zeobvGetNotifiedConfig',
(new GetNotifiedConfigBuilder(
$this->stockSubscriptionService,
$salesChannelContext,
$product
))->build()
);
}
/**
* @param QuickviewPageletLoadedEvent $event
*/
public function onQuickviewPageletLoaded($event): void
{
$salesChannelContext = $event->getSalesChannelContext();
if (!$this->stockSubscriptionService->shouldShowSubscriptionForm($salesChannelContext, false)) {
return;
}
$product = $event->getPagelet()->getProduct();
if ($product->hasExtension('zeobvGetNotifiedConfig')) {
return;
}
$product->addExtension(
'zeobvGetNotifiedConfig',
(new GetNotifiedConfigBuilder(
$this->stockSubscriptionService,
$salesChannelContext,
$product
))->build()
);
}
}