<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
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: '`users`')]
#[UniqueEntity(fields: ['email'], message: 'У вас уже есть аккаунт!')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
const STATUS_USER_STEP_REGISTRATION_2 = 1;
const STATUS_USER_STEP_REGISTRATION_3 = 2;
const STATUS_USER_STEP_REGISTRATION_3_MODERATOR_VIEW = 3;
const STATUS_USER_STEP_REGISTRATION_3_MODERATOR_DECLINE = 4;
const STATUS_USER_READY = 10;
const STATUS_USER_BLOCKED = 11;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 100, unique: true)]
private ?string $email = null;
#[ORM\Column(length: 30)]
private ?string $name = null;
#[ORM\Column(length: 30, nullable: true)]
private ?string $lastname = null;
#[ORM\Column(length: 20, nullable: true)]
private ?string $phone = null;
#[ORM\Column(length: 30, nullable: true)]
private ?string $website = null;
#[ORM\Column(nullable: true)]
private ?string $about_me = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $icon_path = null;
#[ORM\Column(length: 180, nullable: true)]
private ?string $address = null;
#[ORM\Column()]
private ?int $status = null;
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(
type: 'boolean',
nullable: false,
options: ['default' => false]
)]
private bool $isVerified = false;
#[ORM\ManyToOne(inversedBy: 'user_id')]
private ?Specialties $specialties = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ShopCart::class, orphanRemoval: true)]
private Collection $shopCartItems;
#[ORM\OneToMany(mappedBy: 'seller', targetEntity: Product::class, orphanRemoval: true)]
private Collection $products;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Order::class, orphanRemoval: true)]
private Collection $orders;
#[ORM\ManyToMany(targetEntity: Roles::class, mappedBy: 'user')]
private Collection $roles;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Comment::class, orphanRemoval: true)]
private Collection $comments;
public function __construct()
{
$this->shopCartItems = new ArrayCollection();
$this->products = new ArrayCollection();
$this->orders = new ArrayCollection();
$this->roles = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): 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;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string|null $name
*/
public function setName(?string $name): void
{
$this->name = $name;
}
/**
* @return string|null
*/
public function getLastname(): ?string
{
return $this->lastname;
}
/**
* @param string|null $lastname
*/
public function setLastname(?string $lastname): void
{
$this->lastname = $lastname;
}
/**
* @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): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return string|null
*/
public function getPhone(): ?string
{
return $this->phone;
}
/**
* @param string|null $phone
*/
public function setPhone(?string $phone): void
{
$this->phone = $phone;
}
/**
* @return string|null
*/
public function getWebsite(): ?string
{
return $this->website;
}
/**
* @param string|null $website
*/
public function setWebsite(?string $website): void
{
$this->website = $website;
}
/**
* @return string|null
*/
public function getAboutMe(): ?string
{
return $this->about_me;
}
/**
* @param string|null $about_me
*/
public function setAboutMe(?string $about_me): void
{
$this->about_me = $about_me;
}
/**
* @return string|null
*/
public function getIconPath(): ?string
{
return $this->icon_path;
}
/**
* @param string|null $icon_path
*/
public function setIconPath(?string $icon_path): void
{
$this->icon_path = $icon_path;
}
/**
* @return string|null
*/
public function getAddress(): ?string
{
return $this->address;
}
/**
* @param string|null $address
*/
public function setAddress(?string $address): void
{
$this->address = $address;
}
/**
* @return int|null
*/
public function getStatus(): ?int
{
return $this->status;
}
/**
* @param int|null $status
*/
public function setStatus(?int $status): void
{
$this->status = $status;
}
/**
* @return Specialties|null
*/
public function getSpecialties(): ?Specialties
{
return $this->specialties;
}
/**
* @param Specialties|null $specialties
* @return $this
*/
public function setSpecialties(?Specialties $specialties): self
{
$this->specialties = $specialties;
return $this;
}
/**
* @return Collection<int, ShopCart>
*/
public function getShopCartItems(): Collection
{
return $this->shopCartItems;
}
/**
* @param ShopCart $shopCartItem
* @return $this
*/
public function addShopCartItem(ShopCart $shopCartItem): self
{
if (!$this->shopCartItems->contains($shopCartItem)) {
$this->shopCartItems->add($shopCartItem);
$shopCartItem->setUser($this);
}
return $this;
}
/**
* @param ShopCart $shopCartItem
* @return $this
*/
public function removeShopCartItem(ShopCart $shopCartItem): self
{
if ($this->shopCartItems->removeElement($shopCartItem)) {
// set the owning side to null (unless already changed)
if ($shopCartItem->getUser() === $this) {
$shopCartItem->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
/**
* @param Product $product
* @return $this
*/
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setSeller($this);
}
return $this;
}
/**
* @param Product $product
* @return $this
*/
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getSeller() === $this) {
$product->setSeller(null);
}
}
return $this;
}
/**
* @return Collection<int, Order>
*/
public function getOrders(): Collection
{
return $this->orders;
}
/**
* @param Order $orders
* @return $this
*/
public function addOrders(Order $orders): self
{
if (!$this->orders->contains($orders)) {
$this->orders->add($orders);
$orders->setUser($this);
}
return $this;
}
/**
* @param Order $orders
* @return $this
*/
public function removeOrders(Order $orders): self
{
if ($this->orders->removeElement($orders)) {
if ($orders->getUser() === $this) {
$orders->setUser(null);
}
}
return $this;
}
/**
* @return array|string[]
*/
public function getRoles(): array
{
$roles = $this->roles->map(function (Roles $role) {
return $role->getName();
})->toArray();
$roles[] = Roles::USER_ROLE_BUYER;
return array_unique($roles);
}
public function addRoles(Roles $roles): static
{
if (!$this->roles->contains($roles)) {
$this->roles->add($roles);
$roles->addUser($this);
}
return $this;
}
public function removeRoles(Roles $roles): static
{
if ($this->roles->removeElement($roles)) {
$roles->removeUser($this);
}
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setUser($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getUser() === $this) {
$comment->setUser(null);
}
}
return $this;
}
/**
* Есть ли у пользователя роль "Продавец"
*
* @return bool
*/
public function isSeller(): bool
{
return in_array(Roles::USER_ROLE_SELLER, $this->getRoles());
}
/**
* Есть ли у пользователя роль "Админ"
*
* @return bool
*/
public function isAdmin(): bool
{
return in_array(Roles::USER_ROLE_ADMIN, $this->getRoles());
}
/**
* Есть ли у пользователя роль "Модератор"
*
* @return bool
*/
public function isModerator(): bool
{
return in_array(Roles::USER_ROLE_MODERATOR, $this->getRoles());
}
/**
* Статус пользователя "Заблокирован"
*
* @return bool
*/
public function isBlocked(): bool
{
return $this->getStatus() === self::STATUS_USER_BLOCKED;
}
}