<?php
namespace App\Entity;
use App\Repository\ApiMailRequestRepository;
use App\Traits\DateTrait;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Requete d'envoi de mail
*
* @ORM\Entity(repositoryClass=ApiMailRequestRepository::class)
*
* @Serializer\ExclusionPolicy("ALL")
*/
class ApiMailRequest
{
use DateTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"get"})
*
*/
private ?int $id = NULL;
/**
* Type du mail que l'on envoie
*
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank
*
* @Expose
* @Groups({"get","post"})
*/
private string $mailType;
/**
* Le projet qui envoie le mail
*
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank
*
* @Expose
* @Groups({"get","post"})
*/
private string $project;
/**
* Liste des destinataires
*
* @ORM\OneToMany(targetEntity=ApiMailRecipient::class, mappedBy="apiMailRequest", orphanRemoval=true, cascade={"persist"})
*
* @Expose
* @Groups({"post"})
*/
private Collection $recipients;
/**
* Date de l'envoi réel des mails
*
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private ?DateTimeImmutable $sendAt;
/**
* @ORM\Column(type="boolean", options={"default":0})
*/
private bool $error = FALSE;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $errorMessage = NULL;
public function __construct()
{
$this->recipients = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getMailType(): string
{
return $this->mailType;
}
public function setMailType(string $mailType): self
{
$this->mailType = $mailType;
return $this;
}
public function getProject(): string
{
return $this->project;
}
public function setProject(string $project): self
{
$this->project = $project;
return $this;
}
public function getSendAt(): ?DateTimeImmutable
{
return $this->sendAt;
}
public function setSendAt(?DateTimeImmutable $sendAt): self
{
$this->sendAt = $sendAt;
return $this;
}
/**
* @return Collection<int, ApiMailRecipient>
*/
public function getRecipients(): Collection
{
return $this->recipients;
}
/**
* @param iterable<ApiMailRecipient> $recipients
*
* @return $this
*/
public function setRecipients(iterable $recipients): self
{
foreach($this->recipients as $recipient)
{
$this->removeRecipient($recipient);
}
foreach($recipients as $recipient)
{
$this->addRecipient($recipient);
}
return $this;
}
public function addRecipient(ApiMailRecipient $recipient): self
{
if (!$this->recipients->contains($recipient)) {
$this->recipients[] = $recipient;
$recipient->setApiMailRequest($this);
}
return $this;
}
public function removeRecipient(ApiMailRecipient $recipient): self
{
if ($this->recipients->removeElement($recipient)) {
// set the owning side to null (unless already changed)
if ($recipient->getApiMailRequest() === $this) {
$recipient->setApiMailRequest(NULL);
}
}
return $this;
}
public function getError(): ?bool
{
return $this->error;
}
public function setError(bool $error): self
{
$this->error = $error;
return $this;
}
public function getErrorMessage(): ?string
{
return $this->errorMessage;
}
public function setErrorMessage(?string $errorMessage): self
{
$this->errorMessage = $errorMessage;
return $this;
}
}