<?php
/**
* @copyright 2023 Crehler Sp. z o.o.
*
* https://crehler.com/
* support@crehler.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\AdvancedSort\Subscriber;
use Crehler\AdvancedSort\Services\ProductAvailableUpdater;
use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
use Shopware\Core\Content\Product\Events\ProductNoLongerAvailableEvent;
use Shopware\Core\Framework\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductAvailableUpdated implements EventSubscriberInterface
{
private ProductAvailableUpdater $availableUpdater;
public function __construct(ProductAvailableUpdater $availableUpdater)
{
$this->availableUpdater = $availableUpdater;
}
public static function getSubscribedEvents()
{
return [
ProductNoLongerAvailableEvent::class => 'productNoLongerAvailable',
ProductIndexerEvent::class => 'onProductIndexerEvent',
];
}
public function onProductIndexerEvent(ProductIndexerEvent $event)
{
$this->updateAvailable($event->getIds(), $event->getContext());
}
public function productNoLongerAvailable(ProductNoLongerAvailableEvent $event)
{
$this->updateAvailable($event->getIds(), $event->getContext());
}
private function updateAvailable(array $ids, Context $context)
{
$this->availableUpdater->update($ids, $context);
}
}