<?php declare(strict_types=1);
namespace DreiscSeoPro\Subscriber\ProductEvents;
use DreiscSeoPro\Core\Seo\LiveTemplate\LiveTemplateConverter;
use PHPUnit\Exception;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var LiveTemplateConverter
*/
private $liveTemplateConverter;
/**
* @param LiveTemplateConverter $liveTemplateTranslator
*/
public function __construct(LiveTemplateConverter $liveTemplateTranslator)
{
$this->liveTemplateConverter = $liveTemplateTranslator;
}
/**
* @return array|void
*/
public static function getSubscribedEvents()
{
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
];
}
/**
* @param EntityLoadedEvent $entityLoadedEvent
*/
public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent): void
{
/** @var ProductEntity $productEntity */
$productEntity = current($entityLoadedEvent->getEntities());
/** Abort, if empty */
if (false === $productEntity) {
return;
}
/** At this point the seo live template will be converted */
$this->liveTemplateConverter->translateProductEntity($productEntity, $entityLoadedEvent->getContext());
}
}