<?php declare(strict_types=1);
namespace NetInventors\NetiNextGoogleCustomerReviews\Subscriber;
use NetInventors\NetiNextGoogleCustomerReviews\Service\PluginConfig;
use NetInventors\NetiNextGoogleCustomerReviews\Struct\GoogleRatingBadge;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Page;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Frontend implements EventSubscriberInterface
{
/**
* @var PluginConfig
*/
private $pluginConfig;
public function __construct(PluginConfig $pluginConfig)
{
$this->pluginConfig = $pluginConfig;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender',
];
}
public function onStorefrontRender(StorefrontRenderEvent $event): void
{
if (!$this->pluginConfig->isActive() || !$this->pluginConfig->isShowBadge()) {
return;
}
$struct = new GoogleRatingBadge();
$struct->setMerchantId($this->pluginConfig->getMerchantId());
$struct->setPosition($this->pluginConfig->getBadgePosition());
$parameters = $event->getParameters();
$page = $parameters['page'] ?? null;
if ($page instanceof Page) {
$struct = new GoogleRatingBadge();
$struct->setMerchantId($this->pluginConfig->getMerchantId());
$struct->setPosition($this->pluginConfig->getBadgePosition());
$page->addExtension('NetiNextGoogleCustomerReviewsFrontend', $struct);
}
}
}