add success email (#14)

Co-authored-by: Jan-Marlon Leibl <jleibl@proton.me>
Reviewed-on: #14
Reviewed-by: jank1619 <jan@kjan.email>
This commit is contained in:
2025-02-13 20:14:34 +00:00
parent 265ba6a8f5
commit f11f97a9c2
12 changed files with 389 additions and 16 deletions

View File

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Controller\Admin\TicketCrudController;
use App\Entity\Payment;
use App\Entity\Ticket;
use chillerlan\QRCode\QRCode;
use Nucleos\DompdfBundle\Factory\DompdfFactory;
use Nucleos\DompdfBundle\Wrapper\DompdfWrapper;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
class TicketEmailService
{
public function __construct(
private readonly MailerInterface $mailer,
#[Autowire(env: 'SENDER_MAIL')]
private readonly string $senderMail,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly Environment $twig,
) {
}
public function sendSuccessEmail(Payment $payment): void
{
$mail = (new TemplatedEmail())
->htmlTemplate('email/order.html.twig')
->subject('Abiball Ticket')
->from(new Address($this->senderMail, 'Noreply'))
->to(new Address($payment->getCustomer()?->getEmail(), $payment->getCustomer()?->getFirstname() . ' ' . $payment->getCustomer()?->getLastname()))
->context([
'payment' => $payment,
]);
$i = 0;
foreach ($payment->getCustomer()?->getTickets() as $ticket) {
++$i;
$mail->addPart(new DataPart($this->generateTicket($ticket), "ticket-$i.pdf", 'application/pdf'));
}
$this->mailer->send($mail);
}
private function generateTicket(Ticket $ticket): string
{
return (new DompdfWrapper(new DompdfFactory()))->getPdf($this->twig->render('test.html.twig', ['qr' => (new QRCode())->render($this->generateUrl($ticket))]));
}
private function generateUrl(Ticket $ticket): string
{
return $this->urlGenerator->generate('admin', [
'crudAction' => 'detail',
'crudControllerFqcn' => TicketCrudController::class,
'entityId' => $ticket->getCustomer()?->getId()
], UrlGeneratorInterface::ABSOLUTE_URL);
}
}