<?php
namespace GlobusSW6\Subscriber;
use GlobusSW6\Service\Deposit\DepositService;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use GlobusSW6\Service\IaneoBusinessExtension;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
class DepositSubscriber implements EventSubscriberInterface
{
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtension;
/** @var DepositService */
private $depositService;
/**
* @param IaneoBusinessExtension $ianeoBusinessExtension
* @param DepositService $depositService
*/
public function __construct(IaneoBusinessExtension $ianeoBusinessExtension, DepositService $depositService)
{
$this->ianeoBusinessExtension = $ianeoBusinessExtension;
$this->depositService = $depositService;
}
public static function getSubscribedEvents()
{
return [
'product.search.result.loaded' => 'onProductLoaded',
];
}
// Check if product contains deposits and add the deposits into extension IaneoExtensions
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 (is_null($products)){
return;
}
$deposits = $this->depositService->collectDeposits($products);
// Add deposits into extension
$this->ianeoBusinessExtension->addAttributes($event->getResult()->getEntities(), $deposits, 'deposits');
}
}