src/Modules/Chat/Event/Subscriber/UpdateChatUpdatedAt.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Chat\Event\Subscriber;
  3. use App\Modules\Chat\Event\MessageSent;
  4. use App\Modules\Chat\Repository\ChatRepositoryInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class UpdateChatUpdatedAt implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var ChatRepositoryInterface
  10.      */
  11.     private $chatRepository;
  12.     public function __construct(ChatRepositoryInterface $chatRepository)
  13.     {
  14.         $this->chatRepository $chatRepository;
  15.     }
  16.     /**
  17.      * Returns an array of event names this subscriber wants to listen to.
  18.      *
  19.      * The array keys are event names and the value can be:
  20.      *
  21.      *  * The method name to call (priority defaults to 0)
  22.      *  * An array composed of the method name to call and the priority
  23.      *  * An array of arrays composed of the method names to call and respective
  24.      *    priorities, or 0 if unset
  25.      *
  26.      * For instance:
  27.      *
  28.      *  * ['eventName' => 'methodName']
  29.      *  * ['eventName' => ['methodName', $priority]]
  30.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  31.      *
  32.      * @return array The event names to listen to
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             MessageSent::class => 'onMessageSent'
  38.         ];
  39.     }
  40.     public function onMessageSent(MessageSent $event)
  41.     {
  42.         $chat $this->chatRepository->find($event->getChatId());
  43.         $chat->setUpdatedAt(new \DateTime());
  44.         $this->chatRepository->save($chat);
  45.     }
  46. }