<?php declare(strict_types=1);
namespace GlobusSW6\Subscriber\Ghs;
use GlobusSW6\Service\Ghs\HazardPictogramService;
use Psr\Log\LoggerInterface;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use GlobusSW6\Service\IaneoBusinessExtension;
class ExtendDetailPage implements EventSubscriberInterface
{
/** @var HazardPictogramService */
private $hazardPictogramService;
/** @var LoggerInterface */
private $logger;
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtension;
/**
* ExtendDetailPage constructor.
* @param HazardPictogramService $hazardPictogramService
* @param LoggerInterface $logger
* @param IaneoBusinessExtension $ianeoBusinessExtension
*/
public function __construct(HazardPictogramService $hazardPictogramService, LoggerInterface $logger, IaneoBusinessExtension $ianeoBusinessExtension)
{
$this->hazardPictogramService = $hazardPictogramService;
$this->logger = $logger;
$this->ianeoBusinessExtension = $ianeoBusinessExtension;
}
public static function getSubscribedEvents(): array
{
return [
'product.search.result.loaded' => 'onProductLoaded'
];
}
public function onProductLoaded(EntitySearchResultLoadedEvent $event)
{
/** @var ProductCollection $products */
$products = $event->getResult()->getEntities();
if ($products === null) {
return;
}
if ($event->getContext()->getSource() instanceOf SalesChannelApiSource)
{
foreach ($products as $product) {
try {
$ghs = $this->hazardPictogramService->getHazardPictogramDataByProduct($product->getId(), $event->getContext());
if ($ghs) {
$this->ianeoBusinessExtension->addSingleAttributeToProduct($product, $ghs, 'ghs');
}
} catch (\Exception $exception) {
$this->logger->error(__CLASS__ . ":" . __FUNCTION__ . ":" . __LINE__ . ":\$exception->getMessage(): " . $exception->getMessage());
}
}
}
}
}