Feat: Implement email notification about new disposition

This commit is contained in:
Björn Fromme
2023-11-08 11:08:27 +01:00
parent c4e0ef7d5a
commit 342119a456
14 changed files with 129 additions and 69 deletions
@@ -4,17 +4,20 @@ namespace App\Controller\Admin\Application;
use App\Entity\Application;
use App\Entity\Disposition;
use App\Event\DispositionCreatedEvent;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class DisposeController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly LoggerInterface $logger
) {
}
@@ -30,6 +33,8 @@ class DisposeController extends AbstractController
$this->entityManager->remove($application);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new DispositionCreatedEvent($disposition), DispositionCreatedEvent::NAME);
$this->addFlash('success', 'Der Teamer wurde eingeteilt');
$this->logger->info('Create disposition', [
'disposition_id' => $disposition->getId(),
-28
View File
@@ -1,28 +0,0 @@
<?php
namespace App\Email;
use App\Entity\User;
use Psr\Log\LoggerInterface;
class WelcomeEmailMailer
{
public function __construct(private readonly Mailer $mailer, private readonly LoggerInterface $logger)
{}
public function send(User $user): void
{
$this->mailer->createAndSendEmail([
'user' => $user,
], [
'to' => $user->getEmail(),
'subject' => 'Willkommen bei MyE&P Team',
'template' => 'email/welcome.html.twig',
]);
$this->logger->info('Send welcome email', [
'user_id' => $user->getId(),
'user_email' => $user->getEmail(),
]);
}
}
+2 -3
View File
@@ -51,11 +51,10 @@ class Destination implements TimestampableEntityInterface, SoftDeletableEntityIn
public function __toString(): string
{
return sprintf('%s - %s: %s, %s',
return sprintf('%s - %s %s',
$this->getDateFrom()->format('d.m.y'),
$this->getDateTo()->format('d.m.y'),
$this->getProduct(),
$this->getHotel()
$this->getProduct()
);
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Event;
use App\Entity\Disposition;
use Symfony\Contracts\EventDispatcher\Event;
class DispositionCreatedEvent extends Event
{
public const NAME = 'disposition.created';
public function __construct(private readonly Disposition $disposition)
{}
public function getDisposition(): Disposition
{
return $this->disposition;
}
}
@@ -0,0 +1,35 @@
<?php
namespace App\EventListener;
use App\Email\Mailer;
use App\Event\DispositionCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EmailNotificationSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly Mailer $mailer)
{
}
public static function getSubscribedEvents(): array
{
return [
DispositionCreatedEvent::NAME => 'onDispositionCreated',
];
}
public function onDispositionCreated(DispositionCreatedEvent $event): void
{
$disposition = $event->getDisposition();
$teamer = $disposition->getTeamer();
$this->mailer->createAndSendEmail([
'disposition' => $disposition,
], [
'to' => $teamer->getCommunication()->getEmail(),
'subject' => 'Dein Einsatz wurde angenommen',
'template' => 'email/application_accepted.html.twig',
]);
}
}