Files
myep-team/src/Service/Teamer/TeamerMailingDraftHandler.php
T
2026-08-11 16:25:53 +02:00

66 lines
1.8 KiB
PHP

<?php
namespace App\Service\Teamer;
use App\Model\TeamerMailingDto;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Keeps the composed mail in the session, the same way the teamer filter is kept.
*
* Adjusting the filter leaves the compose page and comes back to it, which would
* otherwise throw away everything the admin has written so far.
*/
class TeamerMailingDraftHandler
{
/**
* A blank textarea makes the placeholders easy to miss, so a new mailing already opens
* with the greeting the vast majority of them starts with.
*/
public const NEW_MESSAGE = 'Hallo '.TeamerMailingService::PLACEHOLDER_FIRST_NAME.',';
private const NAMESPACE = 'mailing:teamer';
public function __construct(private readonly RequestStack $requestStack)
{
}
public function getDraft(): TeamerMailingDto
{
$mailingDto = new TeamerMailingDto();
if (null === $data = $this->getSession()->get(self::NAMESPACE)) {
return $mailingDto->setMessage(self::NEW_MESSAGE);
}
return $mailingDto
->setSubject($data['subject'] ?? null)
->setMessage($data['message'] ?? null)
;
}
public function hasDraft(): bool
{
return $this->getSession()->has(self::NAMESPACE);
}
public function saveDraft(TeamerMailingDto $mailingDto): void
{
$this->getSession()->set(self::NAMESPACE, [
'subject' => $mailingDto->getSubject(),
'message' => $mailingDto->getMessage(),
]);
}
public function resetDraft(): void
{
$this->getSession()->remove(self::NAMESPACE);
}
private function getSession(): SessionInterface
{
return $this->requestStack->getSession();
}
}