<?php
namespace App\EventSubscriber;
use App\Component\Configuration\Util\Config;
use App\Event\SiteGenerationFinishedEvent;
use App\Sendgrid\Mailer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SiteGenerationSubscriber implements EventSubscriberInterface
{
protected Config $config;
protected Mailer $mailer;
public function __construct(
Config $config,
Mailer $mailer
) {
$this->config = $config;
$this->mailer = $mailer;
}
public static function getSubscribedEvents(): array
{
return [
SiteGenerationFinishedEvent::class => 'siteGenerationFinished',
];
}
public function siteGenerationFinished(SiteGenerationFinishedEvent $event)
{
if (
!$this->config->get('site_generation_send_notifications') ||
empty($this->config->get('site_generation_notification_receiver_emailaddress'))
) {
return;
}
$template = null;
$failedTemplate = $this->config->get('site_generation_creation_failed_template');
$successfulTemplate = $this->config->get('site_generation_creation_successful_template');
if (!$event->isAccountCreated() && !empty($failedTemplate)) {
$template = $failedTemplate;
}
if ($event->isAccountCreated() && !empty($successfulTemplate)) {
$template = $successfulTemplate;
}
if (!$template) {
return;
}
$this->mailer->mail(
$template,
$this->config->get('site_company_email_address'),
[$this->config->get('site_generation_notification_receiver_emailaddress')],
[
'site' => $event->getSite(),
]
);
}
}