fix emails and some other stuff (#24)

Reviewed-on: #24
This commit is contained in:
Constantin Simonis 2025-02-13 20:45:49 +00:00
parent fcabbaae65
commit 3b8b099ae9
6 changed files with 28 additions and 58 deletions

View File

@ -1,24 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use chillerlan\QRCode\QRCode;
use Nucleos\DompdfBundle\Factory\DompdfFactory;
use Nucleos\DompdfBundle\Wrapper\DompdfWrapper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TestController extends AbstractController
{
#[Route('/test')]
public function index(Filesystem $filesystem): Response
{
$filesystem->dumpFile('test.pdf', $content);
return $this->render('test.html.twig', ['qr' => (new QRCode())->render('https://www.google.com')]);
}
}

View File

@ -6,7 +6,7 @@ class TicketData
{ {
public const TICKET_DATA = [ public const TICKET_DATA = [
1 => [ 1 => [
'name' => 'Ticket', 'name' => 'All-Inclusive Ticket',
'price' => 50, 'price' => 50,
], ],
2 => [ 2 => [

View File

@ -3,10 +3,8 @@ declare(strict_types=1);
namespace App\Service; namespace App\Service;
use App\Controller\Admin\TicketCrudController;
use App\Entity\Payment; use App\Entity\Payment;
use App\Entity\Ticket; use Doctrine\Common\Collections\Collection;
use chillerlan\QRCode\QRCode;
use Nucleos\DompdfBundle\Factory\DompdfFactory; use Nucleos\DompdfBundle\Factory\DompdfFactory;
use Nucleos\DompdfBundle\Wrapper\DompdfWrapper; use Nucleos\DompdfBundle\Wrapper\DompdfWrapper;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
@ -14,7 +12,6 @@ use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment; use Twig\Environment;
class TicketEmailService class TicketEmailService
@ -23,7 +20,6 @@ class TicketEmailService
private readonly MailerInterface $mailer, private readonly MailerInterface $mailer,
#[Autowire(env: 'SENDER_MAIL')] #[Autowire(env: 'SENDER_MAIL')]
private readonly string $senderMail, private readonly string $senderMail,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly Environment $twig, private readonly Environment $twig,
) { ) {
} }
@ -37,28 +33,14 @@ class TicketEmailService
->to(new Address($payment->getCustomer()?->getEmail(), $payment->getCustomer()?->getFirstname() . ' ' . $payment->getCustomer()?->getLastname())) ->to(new Address($payment->getCustomer()?->getEmail(), $payment->getCustomer()?->getFirstname() . ' ' . $payment->getCustomer()?->getLastname()))
->context([ ->context([
'payment' => $payment, 'payment' => $payment,
]); ])
->addPart(new DataPart($this->generateTicket($payment->getCustomer()?->getTickets()), 'tickets.pdf', 'application/pdf'));
$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); $this->mailer->send($mail);
} }
private function generateTicket(Ticket $ticket): string private function generateTicket(Collection $tickets): string
{ {
return (new DompdfWrapper(new DompdfFactory()))->getPdf($this->twig->render('test.html.twig', ['qr' => (new QRCode())->render($this->generateUrl($ticket))])); return (new DompdfWrapper(new DompdfFactory()))->getPdf($this->twig->render('email/pdf.html.twig', ['tickets' => $tickets]));
}
private function generateUrl(Ticket $ticket): string
{
return $this->urlGenerator->generate('admin', [
'crudAction' => 'detail',
'crudControllerFqcn' => TicketCrudController::class,
'entityId' => $ticket->getCustomer()?->getId()
], UrlGeneratorInterface::ABSOLUTE_URL);
} }
} }

View File

@ -2,20 +2,26 @@
namespace App\Twig; namespace App\Twig;
use App\Controller\Admin\TicketCrudController;
use App\Enum\FoodData; use App\Enum\FoodData;
use App\Enum\TicketData; use App\Enum\TicketData;
use chillerlan\QRCode\QRCode;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFilter; use Twig\TwigFilter;
use Twig\TwigFunction;
class Ticket extends AbstractExtension class Ticket extends AbstractExtension
{ {
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
{
}
public function getFilters(): array public function getFilters(): array
{ {
return [ return [
new TwigFilter('food', $this->getFoodName(...)), new TwigFilter('food', $this->getFoodName(...)),
new TwigFilter('ticket', $this->getTicket(...)), new TwigFilter('ticket', $this->getTicket(...)),
new TwigFilter('qr', $this->generateUrl(...)),
]; ];
} }
@ -28,4 +34,13 @@ class Ticket extends AbstractExtension
{ {
return TicketData::TICKET_DATA[$id] ?? ['name' => 'N/A', 'price' => 'N/A']; return TicketData::TICKET_DATA[$id] ?? ['name' => 'N/A', 'price' => 'N/A'];
} }
public function generateUrl(\App\Entity\Ticket $ticket): string
{
return (new QRCode)->render($this->urlGenerator->generate('admin', [
'crudAction' => 'detail',
'crudControllerFqcn' => TicketCrudController::class,
'entityId' => $ticket->getCustomer()?->getId()
], UrlGeneratorInterface::ABSOLUTE_URL));
}
} }

View File

@ -44,11 +44,6 @@
padding: 15px !important; padding: 15px !important;
margin: 15px 0 !important; margin: 15px 0 !important;
} }
.cta-button {
width: 100% !important;
box-sizing: border-box !important;
margin: 10px 0 !important;
}
} }
</style> </style>
</head> </head>

File diff suppressed because one or more lines are too long