Files
myep/src/Entity/NewsletterConsent.php
T

150 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\NewsletterConsentRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: NewsletterConsentRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_NEWSLETTER_CONSENT_EMAIL_LIST', columns: ['email', 'mailjet_list_id'])]
class NewsletterConsent
{
private const int NAME_MAX_LENGTH = 255;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 255)]
private string $email;
#[ORM\Column(type: 'integer')]
private int $mailjetListId;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $confirmedAt = null;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $revokedAt = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $firstName = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $lastName = null;
#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $createdAt;
#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $updatedAt;
public function __construct(string $email, int $mailjetListId, ?string $firstName = null, ?string $lastName = null)
{
if ($mailjetListId <= 0) {
throw new \InvalidArgumentException('Mailjet list ID must be a positive integer.');
}
$this->email = mb_strtolower(trim($email));
$this->mailjetListId = $mailjetListId;
$this->setNames($firstName, $lastName);
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = $this->createdAt;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): string
{
return $this->email;
}
public function getMailjetListId(): int
{
return $this->mailjetListId;
}
public function getConfirmedAt(): ?\DateTimeImmutable
{
return $this->confirmedAt;
}
public function getRevokedAt(): ?\DateTimeImmutable
{
return $this->revokedAt;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function isConfirmed(): bool
{
return null !== $this->confirmedAt && null === $this->revokedAt;
}
public function isRevoked(): bool
{
return null !== $this->revokedAt;
}
public function markConfirmed(?\DateTimeImmutable $now = null, ?string $firstName = null, ?string $lastName = null): void
{
$timestamp = $now ?? new \DateTimeImmutable();
$this->confirmedAt = $timestamp;
$this->revokedAt = null;
$this->mergeNames($firstName, $lastName);
$this->updatedAt = $timestamp;
}
public function markRevoked(?\DateTimeImmutable $now = null): void
{
$timestamp = $now ?? new \DateTimeImmutable();
$this->revokedAt = $timestamp;
$this->updatedAt = $timestamp;
}
public function setNames(?string $firstName, ?string $lastName): void
{
$this->firstName = self::normalizeName($firstName);
$this->lastName = self::normalizeName($lastName);
}
public function mergeNames(?string $firstName, ?string $lastName): void
{
if (null !== $firstName) {
$this->firstName = self::normalizeName($firstName);
}
if (null !== $lastName) {
$this->lastName = self::normalizeName($lastName);
}
}
private static function normalizeName(?string $value): ?string
{
if (null === $value) {
return null;
}
$trimmed = trim($value);
if ('' === $trimmed) {
return null;
}
return mb_substr($trimmed, 0, self::NAME_MAX_LENGTH);
}
}