<?php
namespace Boldr\Shop\SimplePriceProviderBundle\EventSubscriber;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Boldr\Shop\ShopBundle\Entity\Product;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EaEventSubscriber implements EventSubscriberInterface
{
private TagAwareCacheInterface $cache;
public static function getSubscribedEvents(): array
{
return [
AfterEntityUpdatedEvent::class => ['onAfterEntityUpdated']
];
}
public function __construct(TagAwareCacheInterface $cache)
{
$this->cache = $cache;
}
public function onAfterEntityUpdated(AfterEntityUpdatedEvent $event)
{
$entityInstance = $event->getEntityInstance();
if ($entityInstance instanceof Product)
{
// Invalidate product prices
$tags = [
'boldr_simple_price_provider.product.'. $entityInstance->getId()
];
// Invalidate product variant prices
foreach ($entityInstance->getVariants() as $variant)
{
$tags[] = 'boldr_simple_price_provider.product_variant.'. $variant->getId();
}
$this->cache->invalidateTags($tags);
}
}
}