src/Entity/Customer.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  4. use Doctrine\ORM\PersistentCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\Common\Collections\{CollectionArrayCollection};
  8. use App\Repository\CustomerRepository;
  9. use App\Enums\CustomerPlatformType;
  10. use App\Entity\Module;
  11. /**
  12.  * @UniqueEntity({"title"}, message="Имя клиента должно быть уникальным")
  13.  */
  14. #[ORM\Table('customer')]
  15. #[ORM\Entity(CustomerRepository::class)]
  16. #[ORM\HasLifecycleCallbacks]
  17. class Customer implements \JsonSerializable
  18. {
  19.     #[ORM\Column('id''integer')]
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue('IDENTITY')]
  22.     private ?int $id null;
  23.     #[ORM\Column('active''boolean')]
  24.     private bool $active true;
  25.     #[ORM\Column('title''string'500)]
  26.     private ?string $title null;
  27.     #[ORM\Column('url''string'500)]
  28.     private ?string $url null;
  29.     #[ORM\Column('platform''string')]
  30.     private string $platform;
  31.     #[ORM\Column('token''string'500)]
  32.     private ?string $token null;
  33.     #[ORM\OneToOne('customer'targetEntityLicense::class, cascade: ['persist''remove'])]
  34.     private ?License $license null;
  35.     #[ORM\ManyToOne(Project::class)]
  36.     #[ORM\JoinColumn('project')]
  37.     private ?Project $project null;
  38.     /**
  39.      * @var ArrayCollection<ModuleLicense>|PersistentCollection<ModuleLicense>
  40.      */
  41.     #[ORM\OneToMany('customer'ModuleLicense::class, cascade: ['persist'])]
  42.     private Collection $moduleLicense;
  43.     /**
  44.      * @var ArrayCollection<User>|PersistentCollection<User>
  45.      */
  46.     #[ORM\ManyToMany(User::class)]
  47.     #[ORM\JoinTable('customer_responsible')]
  48.     #[ORM\JoinColumn('customerId')]
  49.     #[ORM\InverseJoinColumn('responsibleId')]
  50.     private Collection $responsible;
  51.     /**
  52.      * @var ArrayCollection<User>|PersistentCollection<User>
  53.      */
  54.     #[ORM\ManyToMany(User::class)]
  55.     #[ORM\JoinTable('customer_recipient')]
  56.     #[ORM\JoinColumn('customerId')]
  57.     #[ORM\InverseJoinColumn('recipientId')]
  58.     private Collection $recipient;
  59.     #[ORM\Column('telegram_token'Types::STRINGnullabletrue)]
  60.     private ?string $telegramToken null;
  61.     public function __construct()
  62.     {
  63.         $this->moduleLicense = new ArrayCollection();
  64.         $this->recipient = new ArrayCollection();
  65.         $this->responsible = new ArrayCollection();
  66.         $this->recipient = new ArrayCollection();
  67.         $this->license = new License($this);
  68.         $this->platform CustomerPlatformType::TEST->value;
  69.     }
  70.     public function getId(): ?int
  71.     {
  72.         return $this->id;
  73.     }
  74.     public function setId(int $id): self
  75.     {
  76.         $this->id $id;
  77.         return $this;
  78.     }
  79.     public function isActive(): bool
  80.     {
  81.         return $this->active;
  82.     }
  83.     public function setActive(bool $active): self
  84.     {
  85.         $this->active $active;
  86.         return $this;
  87.     }
  88.     public function getTitle(): ?string
  89.     {
  90.         return $this->title;
  91.     }
  92.     public function setTitle(?string $title): self
  93.     {
  94.         $this->title $title;
  95.         return $this;
  96.     }
  97.     public function getToken(): ?string
  98.     {
  99.         return $this->token;
  100.     }
  101.     public function setToken(string $token): self
  102.     {
  103.         $this->token $token;
  104.         return $this;
  105.     }
  106.     public function getLicense(): License
  107.     {
  108.         return $this->license;
  109.     }
  110.     public function setLicense(License $license): self
  111.     {
  112.         $this->license $license;
  113.         return $this;
  114.     }
  115.     public function getProject(): ?Project
  116.     {
  117.         return $this->project;
  118.     }
  119.     public function setProject(?Project $project): self
  120.     {
  121.         $this->project $project;
  122.         return $this;
  123.     }
  124.     public function getModuleLicense(): ?iterable
  125.     {
  126.         return $this->moduleLicense;
  127.     }
  128.     public function getActiveModuleLicense(): Collection
  129.     {
  130.         return $this->moduleLicense->filter(
  131.             fn (ModuleLicense $moduleLicense) =>
  132.                 $moduleLicense->getModule()->isActive() && !$moduleLicense->getModule()->isDeleted()
  133.         );
  134.     }
  135.     public function isModuleExists(Module $module): bool
  136.     {
  137.         return $this->moduleLicense->exists(fn ($key$license) => $license->getModule() === $module);
  138.     }
  139.     public function getResponsible(): Collection
  140.     {
  141.         return $this->responsible;
  142.     }
  143.     public function getResponsibleIds(): array
  144.     {
  145.         $result = [];
  146.         foreach ($this->responsible as $responsible) {
  147.             $result[] = $responsible->getId();
  148.         }
  149.         return $result;
  150.     }
  151.     public function getRecipientIds(): array
  152.     {
  153.         $result = [];
  154.         foreach ($this->recipient as $recipient) {
  155.             $result[] = $recipient->getId();
  156.         }
  157.         return $result;
  158.     }
  159.     public function setResponsible(?iterable $responsible): self
  160.     {
  161.         $this->responsible $responsible;
  162.         return $this;
  163.     }
  164.     public function setModuleLicense($moduleLicense): self
  165.     {
  166.         $this->moduleLicense $moduleLicense;
  167.         return $this;
  168.     }
  169.     public function getUrl(): ?string
  170.     {
  171.         return $this->url;
  172.     }
  173.     public function setUrl(?string $url): self
  174.     {
  175.         $this->url $url;
  176.         return $this;
  177.     }
  178.     public function getPlatform(): string
  179.     {
  180.         return $this->platform;
  181.     }
  182.     public function setPlatform(string $platform): self
  183.     {
  184.         try {
  185.             CustomerPlatformType::from($platform);
  186.         } catch (\Throwable $th) {
  187.             throw new \Error('Wrong platform type');
  188.         }
  189.         $this->platform $platform;
  190.         return $this;
  191.     }
  192.     public function getRecipient(): ?iterable
  193.     {
  194.         return $this->recipient;
  195.     }
  196.     public function setRecipient($recipient): self
  197.     {
  198.         $this->recipient $recipient;
  199.         return $this;
  200.     }
  201.     public function getTelegramToken(): ?string
  202.     {
  203.         return $this->telegramToken;
  204.     }
  205.     public function setTelegramToken(?string $token): self
  206.     {
  207.         $this->telegramToken $token;
  208.         return $this;
  209.     }
  210.     #[ORM\PrePersist]
  211.     public function prePersist()
  212.     {
  213.         $this->token or $this->token hash('sha512'$this->title 'motivity');
  214.     }
  215.     public function jsonSerialize(): array
  216.     {
  217.         return [
  218.             'id' => $this->getId(),
  219.             'title' => $this->getTitle(),
  220.         ];
  221.     }
  222. }