<?php
namespace App\Subscriber\User;
use App\Event\Messenger\MessageSentEvent;
use App\Security\ApiUser;
use Frivol\Common\Service\CacheService;
use App\Service\Client\Messenger\OperatorService;
use Frivol\Common\Service\Cache\CacheTag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
class MessageSentSubscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
protected SessionInterface $session;
/**
* @var Security
*/
protected Security $security;
/**
* @var CacheService
*/
protected CacheService $cacheService;
/**
* @param Security $security
* @param CacheService $cacheService
* @param SessionInterface $session
*/
public function __construct(Security $security, CacheService $cacheService, SessionInterface $session)
{
$this->security = $security;
$this->cacheService = $cacheService;
$this->session = $session;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
MessageSentEvent::class => [
['onMessageSentRemoveProfitableConversation', 0],
['onMessageSentClearCaches', 0]
],
];
}
/**
* @param MessageSentEvent $event
* @return void
* @throws \Psr\Cache\InvalidArgumentException
*/
public function onMessageSentClearCaches(MessageSentEvent $event)
{
/**
* @var $user ApiUser
*/
$user = $this->security->getUser();
if(!$user->hasRole('ROLE_MEMBER')) {
return;
}
if($event->getOperator() > 0) {
$this->cacheService->invalidateTags([
CacheTag::operatorTargets($event->getOperator())
]);
}
$this->cacheService->clearMemberCoinsCache($user->getMemberId());
$this->cacheService->clearConversationCache($event->getConversation());
}
/**
* @param MessageSentEvent $event
* @return void
*/
public function onMessageSentRemoveProfitableConversation(MessageSentEvent $event)
{
if(!$this->security->isGranted('ROLE_OPERATOR')) {
return;
}
}
}