<?php
namespace App\Entity\Generic;
use App\Entity\BaseEntity;
use App\Entity\BaseSite\Ticket\Ticket;
use App\Entity\BaseSite\Ticket\TicketMessage;
use App\Entity\BaseSite\Transaction;
use App\Entity\Shop\Shop\Shop;
use App\Repository\Generic\UserRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`' , uniqueConstraints: [
new ORM\UniqueConstraint(name: 'shop_identifier_unique', columns: [ 'identifier' , 'phone' , 'email'])
])]
#[UniqueEntity(fields: ['email'], message: 'این پست الکترونیک از قبل موجود می باشد')]
#[UniqueEntity(fields: ['mobile'], message: 'این شماره موبایل از قبل موجود می باشد')]
#[UniqueEntity(fields: ['identifier'], message: 'این پست الکترونیک یا شماره موبایل از قبل موجود می باشد')]
class User extends BaseEntity implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\Column(type: 'guid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?string $id;
#[ORM\Column(length: 180, unique: true)]
private string $identifier;
#[ORM\Column(length: 180, unique: true, nullable: true)]
private ?string $email = null;
#[ORM\Column(length: 20, unique: true, nullable: true)]
private ?string $mobile = null;
#[ORM\Column(type: 'json')]
private array $roles = [];
#[ORM\Column(type: 'string')]
private string $password;
#[ORM\Column(type: 'boolean')]
private bool $isVerified = false;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private mixed $firstName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private mixed $lastName;
#[ORM\Column(type: 'bigint')]
private int $wallet = 0;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTimeInterface $lastLogin = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTimeInterface $lastBuy = null;
#[ORM\Column(nullable: true)]
private ?int $verifyCode = null;
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: Transaction::class)]
private Collection $transactions;
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: Shop::class)]
private Collection $shops;
#[ORM\OneToMany(mappedBy: 'creator', targetEntity: Ticket::class)]
private Collection $tickets;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: TicketMessage::class)]
private Collection $ticketMessages;
public function __construct()
{
parent::__construct();
$this->transactions = new ArrayCollection();
$this->shops = new ArrayCollection();
$this->setVerifyCode(random_int(00000 , 99999));
$this->tickets = new ArrayCollection();
$this->ticketMessages = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function __toString(): string
{
return $this->getIdentifier();
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string)$this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string)$this->email;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): void
{
$this->isVerified = $isVerified;
}
/**
* @return mixed
*/
public function getFirstName(): mixed
{
return $this->firstName;
}
/**
* @param mixed $firstName
*/
public function setFirstName(mixed $firstName): void
{
$this->firstName = $firstName;
}
/**
* @return mixed
*/
public function getLastName(): mixed
{
return $this->lastName;
}
/**
* @param mixed $lastName
*/
public function setLastName(mixed $lastName): void
{
$this->lastName = $lastName;
}
public function getWallet(): int
{
return $this->wallet;
}
public function setWallet(int $wallet): void
{
$this->wallet = $wallet;
}
public function getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(?string $mobile): void
{
$this->mobile = $mobile;
}
public function getLastLogin(): ?DateTimeInterface
{
return $this->lastLogin;
}
public function setLastLogin(?DateTimeInterface $lastLogin): void
{
$this->lastLogin = $lastLogin;
}
public function getLastBuy(): ?DateTimeInterface
{
return $this->lastBuy;
}
public function setLastBuy(?DateTimeInterface $lastBuy): void
{
$this->lastBuy = $lastBuy;
}
public function getVerifyCode(): ?int
{
return $this->verifyCode;
}
public function setVerifyCode(?int $verifyCode): void
{
$this->verifyCode = $verifyCode;
}
public function hasRole($role): bool
{
return in_array($role, $this->getRoles(), true);
}
/**
* @return Collection<int, Transaction>
*/
public function getTransactions(): Collection
{
return $this->transactions;
}
public function addTransaction(Transaction $transaction): static
{
if (!$this->transactions->contains($transaction)) {
$this->transactions->add($transaction);
$transaction->setOwner($this);
}
return $this;
}
public function removeTransaction(Transaction $transaction): static
{
if ($this->transactions->removeElement($transaction)) {
// set the owning side to null (unless already changed)
if ($transaction->getOwner() === $this) {
$transaction->setOwner(null);
}
}
return $this;
}
/**
* @return Collection<int, Shop>
*/
public function getShops(): Collection
{
return $this->shops;
}
public function addShop(Shop $shop): static
{
if (!$this->shops->contains($shop)) {
$this->shops->add($shop);
$shop->setOwner($this);
}
return $this;
}
public function removeShop(Shop $shop): static
{
if ($this->shops->removeElement($shop)) {
// set the owning side to null (unless already changed)
if ($shop->getOwner() === $this) {
$shop->setOwner(null);
}
}
return $this;
}
public function getIdentifier(): string
{
return $this->identifier;
}
public function setIdentifier(string $identifier): void
{
$this->identifier = $identifier;
}
/**
* @return Collection<int, Ticket>
*/
public function getTickets(): Collection
{
return $this->tickets;
}
public function addTicket(Ticket $ticket): static
{
if (!$this->tickets->contains($ticket)) {
$this->tickets->add($ticket);
$ticket->setCreator($this);
}
return $this;
}
public function removeTicket(Ticket $ticket): static
{
if ($this->tickets->removeElement($ticket)) {
// set the owning side to null (unless already changed)
if ($ticket->getCreator() === $this) {
$ticket->setCreator(null);
}
}
return $this;
}
/**
* @return Collection<int, TicketMessage>
*/
public function getTicketMessages(): Collection
{
return $this->ticketMessages;
}
public function addTicketMessage(TicketMessage $ticketMessage): static
{
if (!$this->ticketMessages->contains($ticketMessage)) {
$this->ticketMessages->add($ticketMessage);
$ticketMessage->setAuthor($this);
}
return $this;
}
public function removeTicketMessage(TicketMessage $ticketMessage): static
{
if ($this->ticketMessages->removeElement($ticketMessage)) {
// set the owning side to null (unless already changed)
if ($ticketMessage->getAuthor() === $this) {
$ticketMessage->setAuthor(null);
}
}
return $this;
}
}