src/EventSubscriber/SendMembershipInvitationEmailSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Harmonizely\Mailer\UserMailer;
  5. use Harmonizely\Model\Member;
  6. use Harmonizely\Organization\OrganizationEvents;
  7. use Harmonizely\Service\Core\Contract\ICompanySelector;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. final class SendMembershipInvitationEmailSubscriber implements EventSubscriberInterface
  11. {
  12.     public const TEMPLATE = 'join_organization.html.twig';
  13.     public const TEMPLATE_SSO = 'join_organization_sso.html.twig';
  14.     /**
  15.      * @var UserMailer
  16.      */
  17.     private UserMailer $mailer;
  18.     /**
  19.      * @var ICompanySelector
  20.      */
  21.     private ICompanySelector $companySelector;
  22.     /**
  23.      * SendMembershipInvitationEmailSubscriber constructor.
  24.      * @param UserMailer $mailer
  25.      * @param ICompanySelector $companySelector
  26.      */
  27.     public function __construct(
  28.         UserMailer $mailer,
  29.         ICompanySelector $companySelector
  30.     )
  31.     {
  32.         $this->mailer = $mailer;
  33.         $this->companySelector = $companySelector;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             OrganizationEvents::MEMBER_POST_CREATE => 'onMemberCreate',
  42.         ];
  43.     }
  44.     public function onMemberCreate(GenericEvent $event): void
  45.     {
  46.         if (!$this->companySelector->isDefaultCompany()) {
  47.             return;
  48.         }
  49.         /** @var Member $member */
  50.         $member = $event->getSubject();
  51.         $organization = $member->getOrganization();
  52.         $organizationName = $organization->getName();
  53.         if ($member->getUser()->getUserSso()) {
  54.             $template = self::TEMPLATE_SSO;
  55.             $title = "Joined the '$organizationName' organization on SimplyMeet.me";
  56.         } else {
  57.             $template = self::TEMPLATE;
  58.             $title = "Join the '$organizationName' organization on SimplyMeet.me";
  59.         }
  60.         $owners = $organization->getEnabledOwners();
  61.         /** @var Member $owner */
  62.         $owner = $owners[array_key_first($owners)];
  63.         // When cloning is requested for an already existing user, the source is
  64.         // stored on the member and the copy (which replaces the invitee's own
  65.         // Meeting Types and appearance) happens on acceptance. In that case we
  66.         // show an additional warning about the replacement alongside the
  67.         // administrative-access notice, not instead of it (CY1SMDV1-578).
  68.         $cloneSettings = null !== $member->getCopyFromUser();
  69.         $this->mailer->send(
  70.             $owner->getUser(),
  71.             $template,
  72.             $title,
  73.             [$member->getUser()->getEmail()],
  74.             [
  75.                 'user' => $owner->getUser(),
  76.                 'member' => $member,
  77.                 'organizationName' => $organizationName,
  78.                 'allowLoginAsUser' => $organization->isAllowLoginAsUser(),
  79.                 'cloneSettings' => $cloneSettings,
  80.                 'recipient' => $member->getUser()->getEmail(),
  81.                 'is_admin' => true,
  82.             ],
  83.             $organizationName
  84.         );
  85.     }
  86. }