fix: type mismatches and handle nullable values

This commit is contained in:
2026-08-27 10:16:03 +02:00
parent c33c1b6bf0
commit e7f3233dbc
9 changed files with 67 additions and 47 deletions
@@ -32,18 +32,15 @@ class EditController extends AbstractController
public function index(EmailTextKey $key, Request $request): Response
{
$definition = $this->catalog->get($key);
$emailText = $this->emailTextRepository->findByKey($key);
$isNew = null === $emailText;
$existing = $this->emailTextRepository->findByKey($key);
// Editing a mail for the first time starts from the delivered wording rather than
// from an empty form, so an admin adjusts a sentence instead of rewriting the mail.
if (true === $isNew) {
$emailText = (new EmailText($key))
->setSubject($definition->defaultSubject)
->setHeadline($definition->defaultHeadline)
->setBody($definition->defaultBody)
;
}
$emailText = $existing ?? (new EmailText($key))
->setSubject($definition->defaultSubject)
->setHeadline($definition->defaultHeadline)
->setBody($definition->defaultBody)
;
$form = $this->createForm(EmailTextType::class, $emailText, [
'definition' => $definition,
@@ -51,10 +48,7 @@ class EditController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (true === $isNew) {
$this->entityManager->persist($emailText);
}
$this->entityManager->persist($emailText);
$this->entityManager->flush();
$this->addFlash('success', 'Der E-Mail-Text wurde aktualisiert');
@@ -2,6 +2,7 @@
namespace App\Controller\Admin\Teamer;
use App\Controller\Traits\AuthenticatedUserTrait;
use App\Controller\Traits\ReturnUrlTrait;
use App\Email\MailBodyRenderer;
use App\Form\TeamerMailingType;
@@ -21,6 +22,7 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
class MailingController extends AbstractController
{
use AuthenticatedUserTrait;
use ReturnUrlTrait;
public function __construct(
@@ -47,7 +49,7 @@ class MailingController extends AbstractController
}
if ($form->isSubmitted() && $form->isValid()) {
$this->mailingService->sendPreview($form->getData(), $this->getUser());
$this->mailingService->sendPreview($form->getData(), $this->getAuthenticatedUser());
$this->addFlash('success', sprintf(
'Die Vorschau wurde an %s gesendet',
@@ -164,7 +166,7 @@ class MailingController extends AbstractController
*/
private function renderPreview(TeamerMailingDto $mailingDto): array
{
$values = $this->mailingService->placeholderValuesForUser($this->getUser());
$values = $this->mailingService->placeholderValuesForUser($this->getAuthenticatedUser());
return [
'subject' => $this->mailingService->render($mailingDto->getSubject(), $values),
@@ -0,0 +1,20 @@
<?php
namespace App\Controller\Traits;
use App\Entity\User;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
trait AuthenticatedUserTrait
{
public function getAuthenticatedUser(): User
{
$user = $this->getUser();
if (null === $user) {
throw new AuthenticationException();
}
return $user;
}
}