This commit is contained in:
2022-04-13 23:17:16 +02:00
parent 2a53fb9341
commit 35c7d55b8c
5 changed files with 153 additions and 149 deletions

View File

@@ -19,6 +19,8 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use function count;
use function in_array;
/**
* @ORM\Entity
@@ -32,6 +34,11 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
*/
class Notification implements TrackUpdateInterface
{
/**
* @ORM\Column(type="text", nullable=false)
*/
private string $accessKey;
private array $addedAddresses = [];
/**
@@ -40,6 +47,21 @@ class Notification implements TrackUpdateInterface
*/
private Collection $addressees;
/**
* a list of destinee which will receive notifications.
*
* @var array|string[]
* @ORM\Column(type="json")
*/
private array $addressesEmails = [];
/**
* a list of emails adresses which were added to the notification.
*
* @var array|string[]
*/
private array $addressesEmailsAdded = [];
private ?ArrayCollection $addressesOnLoad = null;
/**
@@ -105,25 +127,6 @@ class Notification implements TrackUpdateInterface
*/
private ?User $updatedBy;
/**
* a list of destinee which will receive notifications
* @var array|string[]
* @ORM\Column(type="json")
*/
private array $addressesEmails = [];
/**
* a list of emails adresses which were added to the notification
*
* @var array|string[]
*/
private array $addressesEmailsAdded = [];
/**
* @ORM\Column(type="text", nullable=false)
*/
private string $accessKey;
public function __construct()
{
$this->addressees = new ArrayCollection();
@@ -133,46 +136,6 @@ class Notification implements TrackUpdateInterface
$this->accessKey = bin2hex(openssl_random_pseudo_bytes(24));
}
/**
* @return array|string[]
*/
public function getAddressesEmails(): array
{
return $this->addressesEmails;
}
/**
* @return array|string[]
*/
public function getAddressesEmailsAdded(): array
{
return $this->addressesEmailsAdded;
}
/**
* @return string
*/
public function getAccessKey(): string
{
return $this->accessKey;
}
public function addAddressesEmail(string $email)
{
if (!in_array($email, $this->addressesEmails, true)) {
$this->addressesEmails[] = $email;
$this->addressesEmailsAdded[] = $email;
}
}
public function removeAddressesEmail(string $email)
{
if (in_array($email, $this->addressesEmails, true)) {
$this->addressesEmails = array_filter($this->addressesEmails, fn ($e) => $e !== $email);
$this->addressesEmailsAdded = array_filter($this->addressesEmailsAdded, fn ($e) => $e !== $email);
}
}
public function addAddressee(User $addressee): self
{
if (!$this->addressees->contains($addressee)) {
@@ -183,12 +146,12 @@ class Notification implements TrackUpdateInterface
return $this;
}
/**
* @return array
*/
public function getAddedAddresses(): array
public function addAddressesEmail(string $email)
{
return $this->addedAddresses;
if (!in_array($email, $this->addressesEmails, true)) {
$this->addressesEmails[] = $email;
$this->addressesEmailsAdded[] = $email;
}
}
public function addComment(NotificationComment $comment): self
@@ -210,6 +173,30 @@ class Notification implements TrackUpdateInterface
return $this;
}
/**
* @Assert\Callback
*
* @param array $payload
*/
public function assertCountAddresses(ExecutionContextInterface $context, $payload): void
{
if (0 === (count($this->getAddressesEmails()) + count($this->getAddressees()))) {
$context->buildViolation('notification.At least one addressee')
->atPath('addressees')
->addViolation();
}
}
public function getAccessKey(): string
{
return $this->accessKey;
}
public function getAddedAddresses(): array
{
return $this->addedAddresses;
}
/**
* @return Collection|User[]
*/
@@ -223,6 +210,22 @@ class Notification implements TrackUpdateInterface
return $this->addressees;
}
/**
* @return array|string[]
*/
public function getAddressesEmails(): array
{
return $this->addressesEmails;
}
/**
* @return array|string[]
*/
public function getAddressesEmailsAdded(): array
{
return $this->addressesEmailsAdded;
}
public function getComments(): Collection
{
return $this->comments;
@@ -339,6 +342,14 @@ class Notification implements TrackUpdateInterface
return $this;
}
public function removeAddressesEmail(string $email)
{
if (in_array($email, $this->addressesEmails, true)) {
$this->addressesEmails = array_filter($this->addressesEmails, static fn ($e) => $e !== $email);
$this->addressesEmailsAdded = array_filter($this->addressesEmailsAdded, static fn ($e) => $e !== $email);
}
}
public function removeComment(NotificationComment $comment): self
{
$this->comments->removeElement($comment);
@@ -408,19 +419,4 @@ class Notification implements TrackUpdateInterface
return $this;
}
/**
* @Assert\Callback()
* @param ExecutionContextInterface $context
* @param array $payload
* @return void
*/
public function assertCountAddresses(ExecutionContextInterface $context, $payload): void
{
if (0 === (count($this->getAddressesEmails()) + count($this->getAddressees()))) {
$context->buildViolation('notification.At least one addressee')
->atPath('addressees')
->addViolation();
}
}
}