src/EventSubscriber/NewsfeedSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Entity\JobQueue;
  5. use App\Entity\Newsfeed;
  6. use App\Service\EsCache;
  7. use App\Entity\Container;
  8. use App\Service\EsSocket;
  9. use App\Service\EsJobQueue;
  10. use App\Event\NewsfeedPostUpdateEvent;
  11. use App\Event\NewsfeedPrePersistEvent;
  12. use App\Event\NewsfeedPostPersistEvent;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use App\Entity\Interfaces\NewsfeedAssetCreatorInterface;
  15. use App\Service\JobQueueAction\SendUserAssignEmailAction;
  16. use App\Service\JobQueueAction\SendCompanyFeedEmailAction;
  17. use App\Service\JobQueueAction\NewsfeedSubscriptionEmailAction;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class NewsfeedSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         private EsCache $esCache,
  23.         private EsJobQueue $esJobQueue,
  24.         private EntityManagerInterface $_em,
  25.         private EsSocket $esSocket
  26.     ) {
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             NewsfeedPrePersistEvent::NAME => 'preCreate',
  32.             NewsfeedPostPersistEvent::NAME => 'onCreate',
  33.             // NewsfeedPostUpdateEvent::NAME => 'onUpdate',
  34.         ];
  35.     }
  36.     public function preCreate(NewsfeedPrePersistEvent $event)
  37.     {
  38.         $object $event->getObject();
  39.         $this->addVoteExpiry($object);
  40.         $this->addAssetDetails($object);
  41.         // $this->addCompanyDetails($object);
  42.     }
  43.     public function onCreate(NewsfeedPostPersistEvent $event)
  44.     {
  45.         $object $event->getObject();
  46.         if ($object->getPostType() === Newsfeed::POSTTYPE_COMPANY && $object->getCompany()) {
  47.             $container $this->esCache->getContainer();
  48.             $payload = ['newsfeedId' => $object->getId()];
  49.             $this->esJobQueue->create(
  50.                 SendCompanyFeedEmailAction::NAME,
  51.                 json_encode($payload),
  52.                 JobQueue::PRIORITY_MEDIUM,
  53.                 $container
  54.             );
  55.         }
  56.         $this->createNotificationMailQueue($event);
  57.         $this->createSubscriptionMailQueue($event);
  58.         $this->esSocket->emit(
  59.             'backend-command',
  60.             [
  61.                 'type' => 'newsfeed_update',
  62.                 'payload' => ['newsfeedId' => $object->getId(), 'newsfeedPostType' => Newsfeed::POSTTYPE_TEXT'containerId' => $object->getContainer()->getId(), 'userId' => $object->getUser()->getId()]
  63.             ]
  64.         );
  65.     }
  66.     public function onUpdate(NewsfeedPostUpdateEvent $event)
  67.     {
  68.         // $this->createNotificationMailQueue($event);
  69.     }
  70.     private function createSubscriptionMailQueue($event)
  71.     {
  72.         $object $event->getObject();
  73.         $newsfeedCategory $object->getNewsfeedCategory();
  74.         if ($newsfeedCategory) {
  75.             $sharingContainerIds $this->_em->getRepository(Container::class)
  76.                 ->getSharingContainerIds($object->getContainer()->getId());
  77.             $sharingContainerIds[] = $object->getContainer()->getId();
  78.             $userIds $this->_em->getRepository(User::class)
  79.                 ->getNewsfeedSubscribedUsers($newsfeedCategory$sharingContainerIds$object->getUserGroups());
  80.             if ($userIds) {
  81.                 $payload = [
  82.                     'userIds' => $userIds,
  83.                     'newsfeedId' => $object->getId()
  84.                 ];
  85.                 $this->esJobQueue->create(
  86.                     NewsfeedSubscriptionEmailAction::NAME,
  87.                     json_encode($payload),
  88.                     JobQueue::PRIORITY_MEDIUM
  89.                 );
  90.             }
  91.         }
  92.     }
  93.     private function createNotificationMailQueue($event)
  94.     {
  95.         $object $event->getObject();
  96.         $userIds $object->getAssignUsers();
  97.         if (is_array($userIds) && count($userIds) > 0) {
  98.             $container $this->esCache->getContainer();
  99.             $container $this->_em->getRepository(Container::class)
  100.                 ->find($container->getId());
  101.             $uIds $ugIds = [];
  102.             foreach ($userIds as $d) {
  103.                 if (isset($d['type']) && $d['type'] === 'group') {
  104.                     $ugIds[] = $d['id'];
  105.                 } else {
  106.                     $uIds[] = $d['id'];
  107.                 }
  108.             }
  109.             if (count($uIds) > 0) {
  110.                 $payload = [
  111.                     'userIds' => $uIds,
  112.                     'type' => 'Newsfeed',
  113.                     'objId' => $object->getId()
  114.                 ];
  115.                 $this->esJobQueue->create(
  116.                     SendUserAssignEmailAction::NAME,
  117.                     json_encode($payload),
  118.                     JobQueue::PRIORITY_MEDIUM,
  119.                     $container
  120.                 );
  121.             }
  122.             if (count($ugIds) > 0) {
  123.                 $offset 0;
  124.                 $limit 100;
  125.                 $found true;
  126.                 do {
  127.                     $objs $this->_em->getRepository(User::class)
  128.                         ->findByUserGroupIds($ugIds$limit$offset);
  129.                     if (!$objs) {
  130.                         $found false;
  131.                         break;
  132.                     }
  133.                     $uIds = [];
  134.                     foreach ($objs as $o) {
  135.                         $uIds[] = $o->getId();
  136.                     }
  137.                     if (count($uIds) > 0) {
  138.                         $payload = [
  139.                             'userIds' => $uIds,
  140.                             'type' => 'Newsfeed',
  141.                             'objId' => $object->getId()
  142.                         ];
  143.                         $this->esJobQueue->create(
  144.                             SendUserAssignEmailAction::NAME,
  145.                             json_encode($payload),
  146.                             JobQueue::PRIORITY_MEDIUM,
  147.                             $container
  148.                         );
  149.                     }
  150.                     $offset += $limit;
  151.                 } while($found);
  152.             }
  153.         }
  154.     }
  155.     private function addAssetDetails(Newsfeed $newsfeed)
  156.     {
  157.         $typeId $newsfeed->getTypeId();
  158.         $entityClass $newsfeed->getPostSubTypeEntity();
  159.         if (!$entityClass) {
  160.             return;
  161.         }
  162.         $sourceElement $this->_em->getRepository($entityClass)
  163.             ->find($typeId);
  164.         if (!$sourceElement || !($sourceElement instanceof NewsfeedAssetCreatorInterface)) {
  165.             return;
  166.         }
  167.         $newsfeed
  168.             ->setPostMetadata($sourceElement->getPostMetadata())
  169.             ->setUserGroups($sourceElement->getPostUserGroups())
  170.             ->setMediaFileNames([$sourceElement->getPostPoster()])
  171.             ->setIsPublished(true)
  172.         ;
  173.     }
  174.     private function addVoteExpiry(Newsfeed $newsfeed)
  175.     {
  176.         if ($newsfeed->getPostType() === Newsfeed::POSTTYPE_TEXT && $newsfeed->getMediaFileType() === Newsfeed::MEDIAFILETYPE_VOTE) {
  177.             $meta $newsfeed->getPostMetadata();
  178.             if (isset($meta['vote']['duration'])) {
  179.                 $hours $this->getHoursFromDuration($meta['vote']['duration']);
  180.                 $expiry = new \DateTime();
  181.                 $expiry->add(new \DateInterval('PT'.$hours.'H'));
  182.                 $newsfeed->setVoteExpiry($expiry);
  183.             }
  184.         }
  185.     }
  186.     private function addCompanyDetails(Newsfeed $newsfeed)
  187.     {
  188.         if ($newsfeed->getPostType() === Newsfeed::POSTTYPE_COMPANY && $newsfeed->getCompany()) {
  189.             $postMeta = [];
  190.             if (is_array($newsfeed->getPostMetadata())) {
  191.                 $postMeta $newsfeed->getPostMetadata();
  192.             }
  193.             $companyMeta $newsfeed->getCompany()->getMetadata();
  194.             $postMeta['company'] = $companyMeta['company'];
  195.             $newsfeed->setPostMetadata($postMeta);
  196.         }
  197.     }
  198.     private function getHoursFromDuration($duration)
  199.     {
  200.         $hours 0;
  201.         switch($duration) {
  202.             case 'd3':
  203.                 $hours 72;
  204.                 break;
  205.             case 'w1':
  206.                 $hours 168;
  207.                 break;
  208.             case 'w2':
  209.                 $hours 336;
  210.                 break;
  211.             case 'w4':
  212.                 $hours 672;
  213.                 break;
  214.             default:
  215.                 $hours 24;
  216.         }
  217.         return $hours;
  218.     }
  219. }