src/EventSubscriber/SendMembershipInvitationEmailSubscriber.php line 42

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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. final class SendMembershipInvitationEmailSubscriber implements EventSubscriberInterface
  10. {
  11.     public const TEMPLATE 'join_organization.html.twig';
  12.     public const TEMPLATE_SSO 'join_organization_sso.html.twig';
  13.     /**
  14.      * @var UserMailer
  15.      */
  16.     private UserMailer $mailer;
  17.     /**
  18.      * SendMembershipInvitationEmailSubscriber constructor.
  19.      * @param UserMailer $mailer
  20.      */
  21.     public function __construct(UserMailer $mailer)
  22.     {
  23.         $this->mailer $mailer;
  24.     }
  25.     /**
  26.      * {@inheritdoc}
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             OrganizationEvents::MEMBER_POST_CREATE => 'onMemberCreate',
  32.         ];
  33.     }
  34.     public function onMemberCreate(GenericEvent $event): void
  35.     {
  36.         /** @var Member $member */
  37.         $member $event->getSubject();
  38.         $organization $member->getOrganization();
  39.         $organizationName $organization->getName();
  40.         if ($member->getUser()->getUserSso()) {
  41.             $template self::TEMPLATE_SSO;
  42.             $title "Joined the '$organizationName' organization on SimplyMeet.me";
  43.         } else {
  44.             $template self::TEMPLATE;
  45.             $title "Join the '$organizationName' organization on SimplyMeet.me";
  46.         }
  47.         $owners $organization->getEnabledOwners();
  48.         /** @var Member $owner */
  49.         $owner $owners[array_key_first($owners)];
  50.         $this->mailer->send(
  51.             $owner->getUser(),
  52.             $template,
  53.             $title,
  54.             [$member->getUser()->getEmail()],
  55.             [
  56.                 'member' => $member,
  57.                 'organizationName' => $organizationName,
  58.                 'recipient' => $member->getUser()->getEmail(),
  59.                 'is_admin' => true,
  60.             ],
  61.             $organizationName
  62.         );
  63.     }
  64. }