src/Subscriber/User/MessageSentSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\User;
  3. use App\Event\Messenger\MessageSentEvent;
  4. use App\Security\ApiUser;
  5. use Frivol\Common\Service\CacheService;
  6. use App\Service\Client\Messenger\OperatorService;
  7. use Frivol\Common\Service\Cache\CacheTag;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class MessageSentSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SessionInterface
  15.      */
  16.     protected SessionInterface $session;
  17.     /**
  18.      * @var Security
  19.      */
  20.     protected Security $security;
  21.     /**
  22.      * @var CacheService
  23.      */
  24.     protected CacheService $cacheService;
  25.     /**
  26.      * @param Security $security
  27.      * @param CacheService $cacheService
  28.      * @param SessionInterface $session
  29.      */
  30.     public function __construct(Security $securityCacheService $cacheServiceSessionInterface $session)
  31.     {
  32.         $this->security $security;
  33.         $this->cacheService $cacheService;
  34.         $this->session $session;
  35.     }
  36.     /**
  37.      * @return array
  38.      */
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             MessageSentEvent::class => [
  43.                 ['onMessageSentRemoveProfitableConversation'0],
  44.                 ['onMessageSentClearCaches'0]
  45.             ],
  46.         ];
  47.     }
  48.     /**
  49.      * @param MessageSentEvent $event
  50.      * @return void
  51.      * @throws \Psr\Cache\InvalidArgumentException
  52.      */
  53.     public function onMessageSentClearCaches(MessageSentEvent $event)
  54.     {
  55.         /**
  56.          * @var $user ApiUser
  57.          */
  58.         $user $this->security->getUser();
  59.         if(!$user->hasRole('ROLE_MEMBER')) {
  60.             return;
  61.         }
  62.         if($event->getOperator() > 0) {
  63.             $this->cacheService->invalidateTags([
  64.                 CacheTag::operatorTargets($event->getOperator())
  65.             ]);
  66.         }
  67.         $this->cacheService->clearMemberCoinsCache($user->getMemberId());
  68.         $this->cacheService->clearConversationCache($event->getConversation());
  69.     }
  70.     /**
  71.      * @param MessageSentEvent $event
  72.      * @return void
  73.      */
  74.     public function onMessageSentRemoveProfitableConversation(MessageSentEvent $event)
  75.     {
  76.         if(!$this->security->isGranted('ROLE_OPERATOR')) {
  77.             return;
  78.         }
  79.     }
  80. }