<?php
namespace App\Entity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;
use Doctrine\Common\Collections\{Collection, ArrayCollection};
use App\Repository\CustomerRepository;
use App\Enums\CustomerPlatformType;
use App\Entity\Module;
/**
* @UniqueEntity({"title"}, message="Имя клиента должно быть уникальным")
*/
#[ORM\Table('customer')]
#[ORM\Entity(CustomerRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Customer implements \JsonSerializable
{
#[ORM\Column('id', 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue('IDENTITY')]
private ?int $id = null;
#[ORM\Column('active', 'boolean')]
private bool $active = true;
#[ORM\Column('title', 'string', 500)]
private ?string $title = null;
#[ORM\Column('url', 'string', 500)]
private ?string $url = null;
#[ORM\Column('platform', 'string')]
private string $platform;
#[ORM\Column('token', 'string', 500)]
private ?string $token = null;
#[ORM\OneToOne('customer', targetEntity: License::class, cascade: ['persist', 'remove'])]
private ?License $license = null;
#[ORM\ManyToOne(Project::class)]
#[ORM\JoinColumn('project')]
private ?Project $project = null;
/**
* @var ArrayCollection<ModuleLicense>|PersistentCollection<ModuleLicense>
*/
#[ORM\OneToMany('customer', ModuleLicense::class, cascade: ['persist'])]
private Collection $moduleLicense;
/**
* @var ArrayCollection<User>|PersistentCollection<User>
*/
#[ORM\ManyToMany(User::class)]
#[ORM\JoinTable('customer_responsible')]
#[ORM\JoinColumn('customerId')]
#[ORM\InverseJoinColumn('responsibleId')]
private Collection $responsible;
/**
* @var ArrayCollection<User>|PersistentCollection<User>
*/
#[ORM\ManyToMany(User::class)]
#[ORM\JoinTable('customer_recipient')]
#[ORM\JoinColumn('customerId')]
#[ORM\InverseJoinColumn('recipientId')]
private Collection $recipient;
#[ORM\Column('telegram_token', Types::STRING, nullable: true)]
private ?string $telegramToken = null;
public function __construct()
{
$this->moduleLicense = new ArrayCollection();
$this->recipient = new ArrayCollection();
$this->responsible = new ArrayCollection();
$this->recipient = new ArrayCollection();
$this->license = new License($this);
$this->platform = CustomerPlatformType::TEST->value;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function isActive(): bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
public function getLicense(): License
{
return $this->license;
}
public function setLicense(License $license): self
{
$this->license = $license;
return $this;
}
public function getProject(): ?Project
{
return $this->project;
}
public function setProject(?Project $project): self
{
$this->project = $project;
return $this;
}
public function getModuleLicense(): ?iterable
{
return $this->moduleLicense;
}
public function getActiveModuleLicense(): Collection
{
return $this->moduleLicense->filter(
fn (ModuleLicense $moduleLicense) =>
$moduleLicense->getModule()->isActive() && !$moduleLicense->getModule()->isDeleted()
);
}
public function isModuleExists(Module $module): bool
{
return $this->moduleLicense->exists(fn ($key, $license) => $license->getModule() === $module);
}
public function getResponsible(): Collection
{
return $this->responsible;
}
public function getResponsibleIds(): array
{
$result = [];
foreach ($this->responsible as $responsible) {
$result[] = $responsible->getId();
}
return $result;
}
public function getRecipientIds(): array
{
$result = [];
foreach ($this->recipient as $recipient) {
$result[] = $recipient->getId();
}
return $result;
}
public function setResponsible(?iterable $responsible): self
{
$this->responsible = $responsible;
return $this;
}
public function setModuleLicense($moduleLicense): self
{
$this->moduleLicense = $moduleLicense;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function getPlatform(): string
{
return $this->platform;
}
public function setPlatform(string $platform): self
{
try {
CustomerPlatformType::from($platform);
} catch (\Throwable $th) {
throw new \Error('Wrong platform type');
}
$this->platform = $platform;
return $this;
}
public function getRecipient(): ?iterable
{
return $this->recipient;
}
public function setRecipient($recipient): self
{
$this->recipient = $recipient;
return $this;
}
public function getTelegramToken(): ?string
{
return $this->telegramToken;
}
public function setTelegramToken(?string $token): self
{
$this->telegramToken = $token;
return $this;
}
#[ORM\PrePersist]
public function prePersist()
{
$this->token or $this->token = hash('sha512', $this->title . 'motivity');
}
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'title' => $this->getTitle(),
];
}
}