src/EventSubscriber/CommonCommentSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Entity\JobQueue;
  5. use App\Service\EsCache;
  6. use App\Entity\Container;
  7. use App\Service\EsJobQueue;
  8. use App\Entity\CommonComment;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Event\CommonCommentPostUpdateEvent;
  11. use App\Event\CommonCommentPostPersistEvent;
  12. use App\Service\JobQueueAction\SendCommentEmailAction;
  13. use App\Service\JobQueueAction\SendUserAssignEmailAction;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CommonCommentSubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(
  18.         private EsCache $esCache,
  19.         private EsJobQueue $esJobQueue,
  20.         private EntityManagerInterface $_em
  21.     ) {
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CommonCommentPostPersistEvent::NAME => 'onCreate',
  27.             CommonCommentPostUpdateEvent::NAME => 'onUpdate',
  28.         ];
  29.     }
  30.     public function onCreate(CommonCommentPostPersistEvent $event)
  31.     {
  32.         $this->createNotificationMailQueue($event);
  33.         // $this->createNotificationMailQueueForRelatedUsers($event);
  34.     }
  35.     public function onUpdate(CommonCommentPostUpdateEvent $event)
  36.     {
  37.         // $this->createNotificationMailQueue($event);
  38.     }
  39.     private function createNotificationMailQueue($event)
  40.     {
  41.         $object $event->getObject();
  42.         $userIds $object->getAssignUsers();
  43.         $sourceElementUsers $object->getSourceElementUsers();
  44.         $userIds $this->getUserIds($sourceElementUsers$userIds);
  45.         if (is_array($userIds) && count($userIds) > 0) {
  46.             $container $this->esCache->getContainer();
  47.             $container $this->_em->getRepository(Container::class)
  48.                 ->find($container->getId());
  49.             $uIds $ugIds = [];
  50.             foreach ($userIds as $d) {
  51.                 if (isset($d['type']) && $d['type'] === 'group') {
  52.                     $ugIds[] = $d['id'];
  53.                 } else {
  54.                     $uIds[] = $d['id'];
  55.                 }
  56.             }
  57.             if (count($uIds) > 0) {
  58.                 $payload = [
  59.                     'userIds' => $uIds,
  60.                     'type' => 'CommonComment',
  61.                     'objId' => $object->getId()
  62.                 ];
  63.                 $this->esJobQueue->create(
  64.                     SendUserAssignEmailAction::NAME,
  65.                     json_encode($payload),
  66.                     JobQueue::PRIORITY_HIGH,
  67.                     $container
  68.                 );
  69.             }
  70.             if (count($ugIds) > 0) {
  71.                 $offset 0;
  72.                 $limit 100;
  73.                 $found true;
  74.                 do {
  75.                     $objs $this->_em->getRepository(User::class)
  76.                         ->findByUserGroupIds($ugIds$limit$offset);
  77.                     if (!$objs) {
  78.                         $found false;
  79.                         break;
  80.                     }
  81.                     $uIds = [];
  82.                     foreach ($objs as $o) {
  83.                         $uIds[] = $o->getId();
  84.                     }
  85.                     if (count($uIds) > 0) {
  86.                         $payload = [
  87.                             'userIds' => $uIds,
  88.                             'type' => 'CommonComment',
  89.                             'objId' => $object->getId()
  90.                         ];
  91.                         $this->esJobQueue->create(
  92.                             SendUserAssignEmailAction::NAME,
  93.                             json_encode($payload),
  94.                             JobQueue::PRIORITY_HIGH,
  95.                             $container
  96.                         );
  97.                     }
  98.                     $offset += $limit;
  99.                 } while($found);
  100.             }
  101.         }
  102.     }
  103.     private function getUserIds($sourceElementUsers$userIds)
  104.     {
  105.         if (is_array($sourceElementUsers)) {
  106.             foreach ($sourceElementUsers as $ua) {
  107.                 foreach ($ua as $user) {
  108.                     foreach ($userIds as $k => $uid) {
  109.                         if ($user->getId() === $uid['id']) {
  110.                             if (isset($uid['type']) && $uid['type'] === 'group') {
  111.                                 continue;
  112.                             }
  113.                             unset($userIds[$k]);
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.         return $userIds;
  120.     }
  121.     private function createNotificationMailQueueForRelatedUsers($event)
  122.     {
  123.         $object $event->getObject();
  124.         if (
  125.             in_array(
  126.                 $object->getSource(),
  127.                 [
  128.                     CommonComment::SOURCE_SESSION,
  129.                     CommonComment::SOURCE_ELTASK,
  130.                     CommonComment::SOURCE_DOCFILEBLOCK,
  131.                     CommonComment::SOURCE_VIDEOGALLERY
  132.                 ]
  133.             )
  134.         ) {
  135.             $container $this->esCache->getContainer();
  136.             $container $this->_em->getRepository(Container::class)
  137.                 ->find($container->getId());
  138.             $targetUsers = [];
  139.             $users $object->getSourceElementUsers();
  140.             foreach ($users as $user) {
  141.                 foreach ($user as $u) {
  142.                     $targetUsers[$u->getId()] = $u->getId();
  143.                 }
  144.             }
  145.             if (count($targetUsers) > 0) {
  146.                 $payload = [
  147.                     'userIds' => $targetUsers,
  148.                     'commonCommentId' => $object->getId()
  149.                 ];
  150.                 $this->esJobQueue->create(
  151.                     SendCommentEmailAction::NAME,
  152.                     json_encode($payload),
  153.                     JobQueue::PRIORITY_HIGH,
  154.                     $container
  155.                 );
  156.             }
  157.         }
  158.     }
  159. }