src/EventSubscriber/GeneralEventSubscriber.php line 329

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Grigor
  5.  * Date: 4/10/19
  6.  * Time: 4:55 PM
  7.  */
  8. namespace App\EventSubscriber;
  9. use App\Entity\Account;
  10. use App\Entity\AccountContentUnit;
  11. use App\Entity\ContentUnitTag;
  12. use App\Entity\NotificationType;
  13. use App\Entity\Subscription;
  14. use App\Entity\UserPreference;
  15. use App\Event\ArticleBoostedByOtherEvent;
  16. use App\Event\ArticleNewEvent;
  17. use App\Event\ArticleShareEvent;
  18. use App\Event\ExchangeCompletedEvent;
  19. use App\Event\PublicationInvitationAcceptEvent;
  20. use App\Event\PublicationInvitationCancelEvent;
  21. use App\Event\PublicationInvitationRejectEvent;
  22. use App\Event\PublicationInvitationRequestEvent;
  23. use App\Event\PublicationMembershipCancelEvent;
  24. use App\Event\PublicationMembershipLeaveEvent;
  25. use App\Event\PublicationMembershipRequestAcceptEvent;
  26. use App\Event\PublicationMembershipRequestCancelEvent;
  27. use App\Event\PublicationMembershipRequestEvent;
  28. use App\Event\PublicationMembershipRequestRejectEvent;
  29. use App\Event\SubscribeUserEvent;
  30. use App\Event\UnsubscribeUserEvent;
  31. use App\Event\UserPreferenceEvent;
  32. use App\Service\UserNotification;
  33. use Doctrine\ORM\EntityManager;
  34. use Doctrine\ORM\EntityManagerInterface;
  35. use Symfony\Component\DependencyInjection\ContainerInterface;
  36. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  37. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  38. use Symfony\Component\HttpKernel\KernelEvents;
  39. class GeneralEventSubscriber implements EventSubscriberInterface
  40. {
  41.     /**
  42.      * @var ContainerInterface $container
  43.      */
  44.     private $container;
  45.     /**
  46.      * @var EntityManager
  47.      */
  48.     public $em;
  49.     /**
  50.      * @var UserNotification
  51.      */
  52.     private $userNotificationService;
  53.     /**
  54.      * @var \Swift_Mailer $swiftMailer
  55.      */
  56.     private $swiftMailer;
  57.     /**
  58.      * @var \Twig_Environment $twig
  59.      */
  60.     private $twig;
  61.     public function __construct(ContainerInterface $containerEntityManagerInterface $emUserNotification $userNotificationService, \Swift_Mailer $swiftMailer, \Twig_Environment $twig)
  62.     {
  63.         $this->container $container;
  64.         $this->em $em;
  65.         $this->userNotificationService $userNotificationService;
  66.         $this->swiftMailer $swiftMailer;
  67.         $this->twig $twig;
  68.     }
  69.     /**
  70.      * Returns an array of event names this subscriber wants to listen to.
  71.      *
  72.      * The array keys are event names and the value can be:
  73.      *
  74.      *  * The method name to call (priority defaults to 0)
  75.      *  * An array composed of the method name to call and the priority
  76.      *  * An array of arrays composed of the method names to call and respective
  77.      *    priorities, or 0 if unset
  78.      *
  79.      * For instance:
  80.      *
  81.      *  * array('eventName' => 'methodName')
  82.      *  * array('eventName' => array('methodName', $priority))
  83.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  84.      *
  85.      * @return array The event names to listen to
  86.      */
  87.     public static function getSubscribedEvents()
  88.     {
  89.         return [
  90.             KernelEvents::REQUEST => 'onKernelRequest',
  91.             PublicationInvitationRequestEvent::NAME => 'onPublicationInvitationRequest',
  92.             PublicationInvitationCancelEvent::NAME => 'onPublicationInvitationCancel',
  93.             PublicationInvitationAcceptEvent::NAME => 'onPublicationInvitationAccept',
  94.             PublicationInvitationRejectEvent::NAME => 'onPublicationInvitationReject',
  95.             PublicationMembershipRequestEvent::NAME => 'onPublicationMembershipRequest',
  96.             PublicationMembershipRequestCancelEvent::NAME => 'onPublicationMembershipRequestCancel',
  97.             PublicationMembershipRequestAcceptEvent::NAME => 'onPublicationMembershipRequestAcceptEvent',
  98.             PublicationMembershipRequestRejectEvent::NAME => 'onPublicationMembershipRequestRejectEvent',
  99.             PublicationMembershipCancelEvent::NAME => 'onPublicationMembershipCancelEvent',
  100.             PublicationMembershipLeaveEvent::NAME => 'onPublicationMembershipLeaveEvent',
  101.             UserPreferenceEvent::NAME => 'onUserPreferenceEvent',
  102.             ArticleNewEvent::NAME => 'onArticleNewEvent',
  103.             ArticleShareEvent::NAME => 'onArticleShareEvent',
  104.             SubscribeUserEvent::NAME => 'onSubscribeUserEvent',
  105.             UnsubscribeUserEvent::NAME => 'onUnsubscribeUserEvent',
  106.             ArticleBoostedByOtherEvent::NAME => 'onArticleBoostedByOtherEvent',
  107.             ExchangeCompletedEvent::NAME => 'onExchangeCompletedEvent',
  108.         ];
  109.     }
  110.     /**
  111.      * Called whenever a new request is made
  112.      * @param GetResponseEvent $event
  113.      * @return null
  114.      */
  115.     public function onKernelRequest(GetResponseEvent $event)
  116.     {
  117.         $request $event->getRequest();
  118.         //  enable channel exclude filter
  119.         $this->em->getFilters()->enable('channel_exclude_filter');
  120.         return null;
  121.     }
  122.     /**
  123.      * @param PublicationInvitationRequestEvent $event
  124.      */
  125.     public function onPublicationInvitationRequest(PublicationInvitationRequestEvent $event)
  126.     {
  127.         try {
  128.             $publication $event->getPublication();
  129.             $performer $event->getPerformer();
  130.             $user $event->getUser();
  131.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_invitation_new']['key'], $performer, ($user->getPublicKey() ? $user->getPublicKey() : $user->getEmail()), $publication);
  132.             $this->userNotificationService->notify($user$notification);
  133.         } catch (\Throwable $e) {
  134.             // ignore all exceptions for now
  135.         }
  136.     }
  137.     /**
  138.      * @param PublicationInvitationCancelEvent $event
  139.      */
  140.     public function onPublicationInvitationCancel(PublicationInvitationCancelEvent $event)
  141.     {
  142.         try {
  143.             $publication $event->getPublication();
  144.             $performer $event->getPerformer();
  145.             $user $event->getUser();
  146.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_invitation_cancelled']['key'], $performer, ($user->getPublicKey() ? $user->getPublicKey() : $user->getEmail()), $publication);
  147.             $this->userNotificationService->notify($user$notification);
  148.         } catch (\Throwable $e) {
  149.             // ignore all exceptions for now
  150.         }
  151.     }
  152.     /**
  153.      * @param PublicationInvitationAcceptEvent $event
  154.      */
  155.     public function onPublicationInvitationAccept(PublicationInvitationAcceptEvent $event)
  156.     {
  157.         try {
  158.             $publication $event->getPublication();
  159.             $performer $event->getPerformer();
  160.             $user $event->getUser();
  161.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_invitation_accepted']['key'], $performer'Invitation accepted'$publication);
  162.             $this->userNotificationService->notify($user$notification);
  163.         } catch (\Throwable $e) {
  164.             // ignore all exceptions for now
  165.         }
  166.     }
  167.     /**
  168.      * @param PublicationInvitationRejectEvent $event
  169.      */
  170.     public function onPublicationInvitationReject(PublicationInvitationRejectEvent $event)
  171.     {
  172.         try {
  173.             $publication $event->getPublication();
  174.             $performer $event->getPerformer();
  175.             $user $event->getUser();
  176.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_invitation_rejected']['key'], $performer'Invitation rejected'$publication);
  177.             $this->userNotificationService->notify($user$notification);
  178.         } catch (\Throwable $e) {
  179.             // ignore all exceptions for now
  180.         }
  181.     }
  182.     /**
  183.      * @param PublicationMembershipRequestEvent $event
  184.      */
  185.     public function onPublicationMembershipRequest(PublicationMembershipRequestEvent $event)
  186.     {
  187.         try {
  188.             $publication $event->getPublication();
  189.             $performer $event->getPerformer();
  190.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_request_new']['key'], $performer'New request'$publication);
  191.             //  OWNER
  192.             $publicationOwner $this->em->getRepository(Account::class)->getPublicationOwner($publication);
  193.             $this->userNotificationService->notify($publicationOwner$notification);
  194.             //  EDITORS
  195.             $publicationEditors $this->em->getRepository(Account::class)->getPublicationEditors($publication);
  196.             if ($publicationEditors) {
  197.                 foreach ($publicationEditors as $publicationEditor) {
  198.                     $this->userNotificationService->notify($publicationEditor$notification);
  199.                 }
  200.             }
  201.         } catch (\Throwable $e) {
  202.             // ignore all exceptions for now
  203.         }
  204.     }
  205.     /**
  206.      * @param PublicationMembershipRequestCancelEvent $event
  207.      */
  208.     public function onPublicationMembershipRequestCancel(PublicationMembershipRequestCancelEvent $event)
  209.     {
  210.         try {
  211.             $publication $event->getPublication();
  212.             $performer $event->getPerformer();
  213.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_request_cancelled']['key'], $performer'Request cancelled'$publication);
  214.             //  OWNER
  215.             $publicationOwner $this->em->getRepository(Account::class)->getPublicationOwner($publication);
  216.             $this->userNotificationService->notify($publicationOwner$notification);
  217.             //  EDITORS
  218.             $publicationEditors $this->em->getRepository(Account::class)->getPublicationEditors($publication);
  219.             if ($publicationEditors) {
  220.                 foreach ($publicationEditors as $publicationEditor) {
  221.                     $this->userNotificationService->notify($publicationEditor$notification);
  222.                 }
  223.             }
  224.         } catch (\Throwable $e) {
  225.             // ignore all exceptions for now
  226.         }
  227.     }
  228.     /**
  229.      * @param PublicationMembershipRequestAcceptEvent $event
  230.      */
  231.     public function onPublicationMembershipRequestAcceptEvent(PublicationMembershipRequestAcceptEvent $event)
  232.     {
  233.         try {
  234.             $publication $event->getPublication();
  235.             $performer $event->getPerformer();
  236.             $user $event->getUser();
  237.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_request_accepted']['key'], $performer'Request accepted'$publication);
  238.             $this->userNotificationService->notify($user$notification);
  239.         } catch (\Throwable $e) {
  240.             // ignore all exceptions for now
  241.         }
  242.     }
  243.     /**
  244.      * @param PublicationMembershipRequestRejectEvent $event
  245.      */
  246.     public function onPublicationMembershipRequestRejectEvent(PublicationMembershipRequestRejectEvent $event)
  247.     {
  248.         try {
  249.             $publication $event->getPublication();
  250.             $performer $event->getPerformer();
  251.             $user $event->getUser();
  252.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_request_rejected']['key'], $performer'Request rejected'$publication);
  253.             $this->userNotificationService->notify($user$notification);
  254.         } catch (\Throwable $e) {
  255.             // ignore all exceptions for now
  256.         }
  257.     }
  258.     /**
  259.      * @param PublicationMembershipCancelEvent $event
  260.      */
  261.     public function onPublicationMembershipCancelEvent(PublicationMembershipCancelEvent $event)
  262.     {
  263.         try {
  264.             $publication $event->getPublication();
  265.             $performer $event->getPerformer();
  266.             $user $event->getUser();
  267.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_membership_cancelled']['key'], $performer'Membership cancelled'$publication);
  268.             $this->userNotificationService->notify($user$notification);
  269.         } catch (\Throwable $e) {
  270.             // ignore all exceptions for now
  271.         }
  272.     }
  273.     /**
  274.      * @param PublicationMembershipLeaveEvent $event
  275.      */
  276.     public function onPublicationMembershipLeaveEvent(PublicationMembershipLeaveEvent $event)
  277.     {
  278.         try {
  279.             $publication $event->getPublication();
  280.             $performer $event->getPerformer();
  281.             $publicationOwner $this->em->getRepository(Account::class)->getPublicationOwner($publication);
  282.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['publication_membership_cancelled_by_user']['key'], $performer'Membership cancelled by User'$publication);
  283.             $this->userNotificationService->notify($publicationOwner$notification);
  284.         } catch (\Throwable $e) {
  285.             // ignore all exceptions for now
  286.         }
  287.     }
  288.     /**
  289.      * @param ArticleNewEvent $event
  290.      */
  291.     public function onArticleNewEvent(ArticleNewEvent $event)
  292.     {
  293.         try {
  294.             $article $event->getArticle();
  295.             /**
  296.              * @var AccountContentUnit[] $authors
  297.              */
  298.             $authors $article->getAuthors();
  299.             foreach ($authors as $author) {
  300.                 //  get subscribers
  301.                 /**
  302.                  * @var Subscription[] $subscribers
  303.                  */
  304.                 $subscribers $author->getAccount()->getSubscribers();
  305.                 if (count($subscribers)) {
  306.                     $notification $this->userNotificationService->createNotification(NotificationType::TYPES['new_article']['key'], $author->getAccount(), $article->getUri());
  307.                     foreach ($subscribers as $subscriber) {
  308.                         $this->userNotificationService->notify($subscriber->getSubscriber(), $notificationtrue);
  309.                     }
  310.                 }
  311.             }
  312.         } catch (\Throwable $e) {
  313.             // ignore all exceptions for now
  314.         }
  315.     }
  316.     /**
  317.      * @param ArticleShareEvent $event
  318.      */
  319.     public function onArticleShareEvent(ArticleShareEvent $event)
  320.     {
  321.         try {
  322.             $article $event->getArticle();
  323.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['share_article']['key'], null$article->getUri(), null$article);
  324.             /**
  325.              * @var AccountContentUnit[] $authors
  326.              */
  327.             $authors $article->getAuthors();
  328.             foreach ($authors as $author) {
  329.                 $this->userNotificationService->notify($author->getAccount(), $notification);
  330.             }
  331.         } catch (\Throwable $e) {
  332.             // ignore all exceptions for now
  333.         }
  334.     }
  335.     /**
  336.      * @param SubscribeUserEvent $event
  337.      */
  338.     public function onSubscribeUserEvent(SubscribeUserEvent $event)
  339.     {
  340.         try {
  341.             $performer $event->getPerformer();
  342.             $author $event->getAuthor();
  343.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['subscribe_user']['key'], $performer"New subscription");
  344.             $this->userNotificationService->notify($author$notification);
  345.         } catch (\Throwable $e) {
  346.             // ignore all exceptions for now
  347.         }
  348.     }
  349.     /**
  350.      * @param UnsubscribeUserEvent $event
  351.      */
  352.     public function onUnsubscribeUserEvent(UnsubscribeUserEvent $event)
  353.     {
  354.         try {
  355.             $performer $event->getPerformer();
  356.             $author $event->getAuthor();
  357.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['unsubscribe_user']['key'], $performer"New unsubscription");
  358.             $this->userNotificationService->notify($author$notification);
  359.         } catch (\Throwable $e) {
  360.             // ignore all exceptions for now
  361.         }
  362.     }
  363.     /**
  364.      * @param UserPreferenceEvent $event
  365.      */
  366.     public function onUserPreferenceEvent(UserPreferenceEvent $event)
  367.     {
  368.         try {
  369.             $user $event->getUser();
  370.             $article $event->getArticle();
  371.             /**
  372.              * @var AccountContentUnit[] $articleAuthors
  373.              */
  374.             $articleAuthors $article->getAuthors();
  375.             $articleTags $article->getTags();
  376.             //  AUTHOR
  377.             foreach ($articleAuthors as $articleAuthor) {
  378.                 $authorPreference $this->em->getRepository(UserPreference::class)->findOneBy(['account' => $user'author' => $articleAuthor->getAccount()]);
  379.                 if (!$authorPreference) {
  380.                     $authorPreference = new UserPreference();
  381.                     $authorPreference->setAccount($user);
  382.                     $authorPreference->setAuthor($articleAuthor->getAccount());
  383.                 }
  384.                 $count $authorPreference->getCount();
  385.                 $authorPreference->setCount(++$count);
  386.                 $this->em->persist($authorPreference);
  387.             }
  388.             //  TAGS
  389.             if ($articleTags) {
  390.                 /**
  391.                  * @var ContentUnitTag $articleTag
  392.                  */
  393.                 foreach ($articleTags as $articleTag) {
  394.                     $tagPreference $this->em->getRepository(UserPreference::class)->findOneBy(['account' => $user'tag' => $articleTag->getTag()]);
  395.                     if (!$tagPreference) {
  396.                         $tagPreference = new UserPreference();
  397.                         $tagPreference->setAccount($user);
  398.                         $tagPreference->setTag($articleTag->getTag());
  399.                     }
  400.                     $count $tagPreference->getCount();
  401.                     $tagPreference->setCount(++$count);
  402.                     $this->em->persist($tagPreference);
  403.                 }
  404.             }
  405.             $this->em->flush();
  406.         } catch (\Throwable $e) {
  407.             // ignore all exceptions for now
  408.         }
  409.     }
  410.     /**
  411.      * @param ArticleBoostedByOtherEvent $event
  412.      */
  413.     public function onArticleBoostedByOtherEvent(ArticleBoostedByOtherEvent $event)
  414.     {
  415.         try {
  416.             $performer $event->getPerformer();
  417.             $article $event->getArticle();
  418.             /**
  419.              * @var AccountContentUnit[] $articleAuthors
  420.              */
  421.             $articleAuthors $article->getAuthors();
  422.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['article_boosted_by_other']['key'], $performer$article->getUri(), null$article);
  423.             foreach ($articleAuthors as $articleAuthor) {
  424.                 $this->userNotificationService->notify($articleAuthor->getAccount(), $notificationtrue);
  425.             }
  426.             //  send email
  427.             $backendEndpoint $this->container->getParameter('backend_endpoint');
  428.             $frontendEndpoint $this->container->getParameter('frontend_endpoint');
  429.             $performerName trim($performer->getFirstName() . ' ' $performer->getLastName());
  430.             if (!$performerName) {
  431.                 $performerName $performer->getPublicKey();
  432.             }
  433.             $sponsorUrl $frontendEndpoint '/a/' $performer->getPublicKey();
  434.             $articleUrl $frontendEndpoint '/s/' $article->getUri();
  435.             $emailBody $this->twig->render(
  436.                 'emails/boosted_article.html.twig',
  437.                 ['name' => $performerName'title' => $article->getTitle(), 'sponsorUrl' => $sponsorUrl'articleUrl' => $articleUrl'backendEndpoint' => $backendEndpoint]
  438.             );
  439.             foreach ($articleAuthors as $articleAuthor) {
  440.                 $messageObj = (new \Swift_Message('Your story goes viral'))
  441.                     ->setFrom('no-reply@publiq.network''Slog')
  442.                     ->setTo($articleAuthor->getAccount()->getEmail())
  443.                     ->setBody($emailBody'text/html');
  444.                 $this->swiftMailer->send($messageObj);
  445.             }
  446.         } catch (\Throwable $e) {
  447.             // ignore all exceptions for now
  448.         }
  449.     }
  450.     /**
  451.      * @param ExchangeCompletedEvent $event
  452.      */
  453.     public function onExchangeCompletedEvent(ExchangeCompletedEvent $event)
  454.     {
  455.         try {
  456.             $exchange $event->getExchange();
  457.             $notification $this->userNotificationService->createNotification(NotificationType::TYPES['ataix_exchange_completed']['key'], null$exchange->getExchangeId(), nullnull$exchange);
  458.             $this->userNotificationService->notify($exchange->getAccount(), $notificationtrue);
  459.         } catch (\Throwable $e) {
  460.             // ignore all exceptions for now
  461.         }
  462.     }
  463. }