src/Subscriber/UserGroups/UserGroupPostSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\UserGroups;
  3. use App\Event\UserGroups\UserGroupPostCreatedEvent;
  4. use App\Service\UserGroups\GroupService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class UserGroupPostSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var GroupService
  10.      */
  11.     protected $service;
  12.     /**
  13.      * UserGroupPostSubscriber constructor.
  14.      */
  15.     public function __construct(GroupService $service)
  16.     {
  17.         $this->service $service;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             UserGroupPostCreatedEvent::class => [
  23.                 ['onPostCalculateGroupStatPostCount'0],
  24.                 ['onPostCreatedUpdateLastPostFields'0],
  25.             ],
  26.         ];
  27.     }
  28.     /**
  29.      * @throws \Doctrine\DBAL\Exception
  30.      * @throws \Doctrine\ORM\ORMException
  31.      * @throws \Doctrine\ORM\OptimisticLockException
  32.      */
  33.     public function onPostCalculateGroupStatPostCount(UserGroupPostCreatedEvent $event)
  34.     {
  35.         $group $event->getEntity()->getThread()->getUserGroup();
  36.         $this->service->calculateUserGroupStatPostCount($group);
  37.     }
  38.     /**
  39.      * @throws \Doctrine\ORM\ORMException
  40.      * @throws \Doctrine\ORM\OptimisticLockException
  41.      */
  42.     public function onPostCreatedUpdateLastPostFields(UserGroupPostCreatedEvent $event)
  43.     {
  44.         $post $event->getEntity();
  45.         $thread $post->getThread();
  46.         // update thread
  47.         $thread->setLastPost($post);
  48.         $thread->setPostCount($thread->getPostCount() + 1);
  49.         $this->service->storeEntity($thread);
  50.         // update last_post_at datetime
  51.         $stat $thread->getUserGroup()->getUserGroupStat();
  52.         $stat->setLastPostAt($post->getCreatedAt());
  53.         $this->service->storeEntity($stat);
  54.     }
  55. }