<?php
declare(strict_types=1);
namespace App\Program\Doctrine\Entity;
use App\Client\Doctrine\Entity\Client;
use App\Program\Doctrine\Repository\ProgramRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\Valid;
#[Entity(repositoryClass: ProgramRepository::class)]
#[Table(name: '`program`')]
class Program implements UserInterface
{
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
private ?int $id = null;
#[Column]
#[NotBlank]
private string $name = '';
#[Column]
#[NotBlank]
#[Url]
private string $url = '';
#[Column(type: 'date_immutable', nullable: true)]
#[NotBlank]
private ?DateTimeImmutable $startedAt = null;
#[Column(type: 'date_immutable', nullable: true)]
#[GreaterThan(propertyPath: 'startedAt')]
private ?DateTimeImmutable $endedAt = null;
#[ManyToOne(targetEntity: Client::class)]
#[JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[NotNull(message: 'Veuillez sélectionner un client.')]
private Client $client;
#[Column(type: 'point_evaluator')]
#[NotNull(message: 'Veuillez saisir une expression.')]
#[Valid]
private PointEvaluator $currencyToPoint;
#[Column(type: 'point_evaluator')]
#[NotNull(message: 'Veuillez saisir une expression.')]
#[Valid]
private PointEvaluator $pointToCurrency;
/**
* @var Collection<array-key, Profile>
*/
#[OneToMany(mappedBy: 'program', targetEntity: Profile::class)]
private Collection $profiles;
/**
* @var Collection<array-key, ProgramEmail>
*/
#[OneToMany(mappedBy: 'program', targetEntity: ProgramEmail::class, cascade: ['persist'])]
private Collection $emails;
#[Column(unique: true)]
private string $apiKey;
#[Column]
#[NotBlank]
#[Email]
private string $email;
public function __construct()
{
$this->apiKey = (string) Uuid::v4();
$this->profiles = new ArrayCollection();
$this->emails = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getUrl(): string
{
return $this->url;
}
public function setUrl(string $url): void
{
$this->url = $url;
}
public function getStartedAt(): ?DateTimeImmutable
{
return $this->startedAt;
}
public function setStartedAt(?DateTimeImmutable $startedAt): void
{
$this->startedAt = $startedAt;
}
public function getEndedAt(): ?DateTimeImmutable
{
return $this->endedAt;
}
public function setEndedAt(?DateTimeImmutable $endedAt): void
{
$this->endedAt = $endedAt;
}
public function isClosed(): bool
{
return $this->endedAt < new DateTimeImmutable();
}
public function isUpcoming(): bool
{
return $this->startedAt > new DateTimeImmutable();
}
public function isPending(): bool
{
return $this->startedAt <= new DateTimeImmutable()
&& (null === $this->endedAt || $this->endedAt > new DateTimeImmutable());
}
public function getClient(): Client
{
return $this->client;
}
public function setClient(Client $client): void
{
$this->client = $client;
}
public function getCurrencyToPoint(): PointEvaluator
{
return $this->currencyToPoint;
}
public function setCurrencyToPoint(PointEvaluator $currencyToPoint): void
{
$this->currencyToPoint = $currencyToPoint;
}
public function getPointToCurrency(): PointEvaluator
{
return $this->pointToCurrency;
}
public function setPointToCurrency(PointEvaluator $pointToCurrency): void
{
$this->pointToCurrency = $pointToCurrency;
}
public function getApiKey(): string
{
return $this->apiKey;
}
/**
* @return array<int, string>
*/
public function getRoles(): array
{
return ['ROLE_API', 'ROLE_USER'];
}
public function getPassword(): string
{
return '';
}
public function getSalt(): ?string
{
return null;
}
public function eraseCredentials(): void
{
}
public function getUsername(): string
{
return $this->apiKey;
}
public function getUserIdentifier(): string
{
return $this->apiKey;
}
/**
* @return Collection<array-key, Profile>
*/
public function getProfiles(): Collection
{
return $this->profiles;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
public function getEmails(): Collection
{
return $this->emails;
}
public function getEmailByCode(string $code): ProgramEmail
{
foreach ($this->emails as $email) {
if ($email->getEmail()->getCode() === $code) {
return $email;
}
}
}
}