<?php
namespace App\Entity;
use App\Entity\Traits\TimestampableTrait;
use App\Repository\StakeholderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: StakeholderRepository::class)]
#[ORM\Table(name: 'stakeholder')]
#[ORM\UniqueConstraint(
name: 'unique_email_firm_category',
columns: ['email', 'accounting_firm_id', 'stakeholder_category_id']
)]
#[UniqueEntity(
fields: ['email', 'accountingFirm', 'stakeholderCategory'],
errorPath: 'email',
message: 'Cet email existe déjà.'
)]
class Stakeholder
{
use TimestampableTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: AccountingFirm::class, inversedBy: 'stakeholders')]
#[ORM\JoinColumn(nullable: false)]
private $accountingFirm;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $companyName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $lastname;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $firstname;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $email;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $phone;
#[ORM\Column(type: 'date', nullable: true)]
private $birthdayDate;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
#[ORM\Column(type: 'uuid', unique: true, nullable: true)]
private ?Uuid $token = null;
// Stocke un tableau de couples [email => uuid]
#[ORM\Column(type: 'json')]
private $listEmails = [];
#[ORM\ManyToOne(targetEntity: StakeholderCategory::class, inversedBy: 'stakeholders')]
#[ORM\JoinColumn(nullable: false)]
private $stakeholderCategory;
#[ORM\ManyToMany(targetEntity: StakeholderTag::class, inversedBy: 'stakeholders')]
private $stakeholderTags;
#[ORM\OneToMany(mappedBy: 'stakeholder', targetEntity: StakeholderSubscription::class, orphanRemoval: true)]
private Collection $stakeholderSubscriptions;
public function __construct()
{
$this->stakeholderTags = new ArrayCollection();
$this->stakeholderSubscriptions = new ArrayCollection();
$this->token = Uuid::v4();
}
public function getId(): ?int
{
return $this->id;
}
public function getAccountingFirm(): ?AccountingFirm
{
return $this->accountingFirm;
}
public function setAccountingFirm(?AccountingFirm $accountingFirm): self
{
$this->accountingFirm = $accountingFirm;
return $this;
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function setCompanyName(?string $companyName): self
{
$this->companyName = $companyName;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(?string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(?string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getBirthdayDate(): ?\DateTimeInterface
{
return $this->birthdayDate;
}
public function setBirthdayDate(?\DateTimeInterface $birthdayDate): self
{
$this->birthdayDate = $birthdayDate;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getToken(): ?Uuid
{
return $this->token;
}
public function setToken(Uuid $token): self
{
$this->token = $token;
return $this;
}
public function getListEmails(): array
{
return $this->listEmails;
}
public function setListEmails(array $listEmails): self
{
$this->listEmails = $listEmails;
return $this;
}
public function addAtListIfNotExist(string $email, string $token): self
{
foreach ($this->listEmails as $entry) {
// Anti-doublon par email OU par token
if (
(isset($entry['email']) && strtolower($entry['email']) === strtolower($email)) ||
(isset($entry['token']) && $entry['token'] === $token)
) {
return $this;
}
}
$this->listEmails[] = [
'email' => $email,
'token' => $token,
];
return $this;
}
public function getStakeholderCategory(): ?StakeholderCategory
{
return $this->stakeholderCategory;
}
public function setStakeholderCategory(?StakeholderCategory $stakeholderCategory): self
{
$this->stakeholderCategory = $stakeholderCategory;
return $this;
}
/**
* @return Collection<int, StakeholderTag>
*/
public function getStakeholderTags(): Collection
{
return $this->stakeholderTags;
}
public function addStakeholderTag(StakeholderTag $stakeholderTag): self
{
if (!$this->stakeholderTags->contains($stakeholderTag)) {
$this->stakeholderTags[] = $stakeholderTag;
}
return $this;
}
public function removeStakeholderTag(StakeholderTag $stakeholderTag): self
{
$this->stakeholderTags->removeElement($stakeholderTag);
return $this;
}
public function isSubscribedToNewsletter(): bool
{
foreach ($this->stakeholderSubscriptions as $subscription) {
if ($subscription->getSubscriptionType()->getSlug() === StakeholderSubscriptionType::NEWSLETTER) {
return $subscription->isSubscribed();
}
}
return false;
}
}