<?php declare(strict_types=1);
namespace GlobusSW6\Subscriber;
use GlobusSW6\Service\BadgesPriorityService;
use GlobusSW6\Service\IaneoBusinessExtension;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BadgesPrioritySubscriber implements EventSubscriberInterface
{
/** @var BadgesPriorityService */
private $badgesPriorityService;
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtensionService;
public function __construct(BadgesPriorityService $badgesPriorityService, IaneoBusinessExtension $ianeoBusinessExtensionService)
{
$this->badgesPriorityService = $badgesPriorityService;
$this->ianeoBusinessExtensionService = $ianeoBusinessExtensionService;
}
public static function getSubscribedEvents()
{
return [
'product.search.result.loaded' => ['onProductLoaded', 100]
];
}
public function onProductLoaded(EntitySearchResultLoadedEvent $event)
{
// do not fire event in backend context
if ($event->getContext()->getSource() instanceof AdminApiSource) {
if ($event->getContext()->getSource()->isAdmin()) {
return;
}
}
/** @var ProductCollection $products */
$products = $event->getResult()->getEntities();
if ($products === null) {
return;
}
if ($event->getName() !== 'product.search.result.loaded') {
return;
}
$badgeContent = $this->badgesPriorityService->checkBadgeContent($products);
$this->ianeoBusinessExtensionService->addAttributes($products, $badgeContent, 'disturber');
}
}