<?php
namespace App\EventSubscriber;
use App\Handler\ContainerHandler;
use App\Event\BackgroundPostUpdateEvent;
use App\Event\BackgroundPostRemoveEvent;
use App\Event\BackgroundPostPersistEvent;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BackgroundSubscriber implements EventSubscriberInterface
{
public function __construct(
private TagAwareCacheInterface $esComCache
) {}
public static function getSubscribedEvents(): array
{
return [
BackgroundPostPersistEvent::NAME => 'onCreate',
BackgroundPostUpdateEvent::NAME => 'onUpdate',
BackgroundPostRemoveEvent::NAME => 'onDelete'
];
}
public function onCreate(BackgroundPostPersistEvent $event)
{
$this->removeContainerFromCache($event);
}
public function onUpdate(BackgroundPostUpdateEvent $event)
{
$this->removeContainerFromCache($event);
}
public function onDelete(BackgroundPostRemoveEvent $event)
{
$this->removeContainerFromCache($event);
}
private function removeContainerFromCache($event)
{
$object = $event->getObject();
$containerId = $object->getContainer()->getId();
//$this->esComCache->invalidateTags(['ES_CONTAINER_'.$containerId, 'ES_MY_CONTAINER_'.$containerId]);
$this->esComCache->invalidateTags(['ES_CONTAINER', 'ES_MY_CONTAINER']);
}
}