72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Message;
|
|
|
|
use App\Model\TeamerMailingRecipientsDto;
|
|
|
|
/**
|
|
* One composed mailing, on its way to the recipients the admin confirmed.
|
|
*
|
|
* The recipients travel as a snapshot of plain scalars rather than as a filter to run
|
|
* later: re-running the filter in the worker could reach a different set of people than
|
|
* the one the confirmation modal showed, and the filter itself carries hydrated entities
|
|
* that have no business being serialized into a queue row. Plain arrays also survive a
|
|
* deploy that changes TeamerMailingRecipient while messages are still queued.
|
|
*/
|
|
class SendTeamerMailing
|
|
{
|
|
/**
|
|
* @param array<int, array{email: string, firstName: string, lastName: string, fullName: string}> $recipients
|
|
*/
|
|
public function __construct(
|
|
private readonly string $subject,
|
|
private readonly string $message,
|
|
private readonly array $recipients,
|
|
) {
|
|
}
|
|
|
|
public static function fromRecipients(string $subject, string $message, TeamerMailingRecipientsDto $recipients): self
|
|
{
|
|
$snapshot = [];
|
|
|
|
foreach ($recipients->getEligible() as $recipient) {
|
|
// resolveRecipients() already guaranteed the address, this only narrows the type
|
|
if (null === $email = $recipient->getEmail()) {
|
|
continue;
|
|
}
|
|
|
|
$snapshot[] = [
|
|
'email' => $email,
|
|
'firstName' => $recipient->getFirstName(),
|
|
'lastName' => $recipient->getLastName(),
|
|
'fullName' => $recipient->getFullName(),
|
|
];
|
|
}
|
|
|
|
return new self($subject, $message, $snapshot);
|
|
}
|
|
|
|
public function getSubject(): string
|
|
{
|
|
return $this->subject;
|
|
}
|
|
|
|
public function getMessage(): string
|
|
{
|
|
return $this->message;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{email: string, firstName: string, lastName: string, fullName: string}>
|
|
*/
|
|
public function getRecipients(): array
|
|
{
|
|
return $this->recipients;
|
|
}
|
|
|
|
public function getRecipientCount(): int
|
|
{
|
|
return count($this->recipients);
|
|
}
|
|
}
|