<?php
declare(strict_types=1);
namespace Harmonizely\EventSubscriber;
use Harmonizely\Mailer\UserMailer;
use Harmonizely\Model\Member;
use Harmonizely\Organization\OrganizationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
final class SendMembershipInvitationEmailSubscriber implements EventSubscriberInterface
{
public const TEMPLATE = 'join_organization.html.twig';
public const TEMPLATE_SSO = 'join_organization_sso.html.twig';
/**
* @var UserMailer
*/
private UserMailer $mailer;
/**
* SendMembershipInvitationEmailSubscriber constructor.
* @param UserMailer $mailer
*/
public function __construct(UserMailer $mailer)
{
$this->mailer = $mailer;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
OrganizationEvents::MEMBER_POST_CREATE => 'onMemberCreate',
];
}
public function onMemberCreate(GenericEvent $event): void
{
/** @var Member $member */
$member = $event->getSubject();
$organization = $member->getOrganization();
$organizationName = $organization->getName();
if ($member->getUser()->getUserSso()) {
$template = self::TEMPLATE_SSO;
$title = "Joined the '$organizationName' organization on SimplyMeet.me";
} else {
$template = self::TEMPLATE;
$title = "Join the '$organizationName' organization on SimplyMeet.me";
}
$owners = $organization->getEnabledOwners();
/** @var Member $owner */
$owner = $owners[array_key_first($owners)];
$this->mailer->send(
$owner->getUser(),
$template,
$title,
[$member->getUser()->getEmail()],
[
'member' => $member,
'organizationName' => $organizationName,
'recipient' => $member->getUser()->getEmail(),
'is_admin' => true,
],
$organizationName
);
}
}