<?php
declare(strict_types=1);
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\PasswordHasher\Hasher\PasswordHasherFactory;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"email"}, message="This e-mail already exists in the system")
* @UniqueEntity(fields={"username"}, message="This user already exists in the system")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserToken", mappedBy="user", cascade={"all"}, orphanRemoval=true)
* @ORM\OrderBy({"id" = "ASC"})
*/
protected $tokens;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="pages.order.form.validation.email.notBlank", groups={"Registration", "Profile"})
* @Assert\Email(message="pages.order.form.validation.email.invalid", groups={"Registration", "Profile"})
*/
private $email;
/**
* @ORM\Column(type="string", length=20, nullable=true)
* @Assert\NotBlank(message="pages.order.form.validation.phone.notBlank", groups={"Registration"})
* @Assert\Regex(pattern="/^[0-9+]*$/", message="pages.order.form.validation.phone.invalidRegex", groups={"Registration"})
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="pages.user.profileEdit.form.validation.firstName.notBlank", groups={"Profile"})
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="pages.user.profileEdit.form.validation.lastName.notBlank", groups={"Profile"})
*/
private $lastName;
/**
* @ORM\Column(type="date", nullable=true)
* @Assert\NotBlank(message="pages.user.profileEdit.form.validation.birthdate.notBlank", groups={"Profile"})
* @Assert\LessThan("-18 years", message="pages.user.profileEdit.form.validation.birthdate.ageRestriction", groups={"Profile"})
*/
private $birthdate;
/**
* @ORM\Column(type="string", length=2, nullable=true)
* @Assert\NotBlank(message="pages.user.profileEdit.form.validation.countryOfResidence.notBlank", groups={"Profile"})
* @Assert\Length(min=2, max=2, minMessage="pages.user.profileEdit.form.validation.countryOfResidence.minLength", maxMessage="pages.user.profileEdit.form.validation.countryOfResidence.maxLength", groups={"Profile"})
*/
private $countryOfResidence;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\Column(type="integer", options={"default": 0})
*/
private int $agentDiscount = 0;
private ?string $passwordPlain = null;
/**
* @ORM\OneToMany(targetEntity=Coupon::class, mappedBy="partner")
*/
private $partnerCoupons;
public function __construct()
{
$this->tokens = new ArrayCollection();
$this->partnerCoupons = new ArrayCollection();
}
public function __toString(): string
{
return $this->username ?? '';
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string)$this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function setPasswordPlain(?string $password): self
{
$this->passwordPlain = $password;
return $this;
}
public function getPasswordPlain(): ?string
{
return $this->passwordPlain;
}
/**
* 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 getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
/**
* Add token
*
*
* @return User
*/
public function addToken(UserToken $token)
{
$this->tokens[] = $token;
return $this;
}
/**
* Remove token
*
*/
public function removeToken(UserToken $token)
{
$this->tokens->removeElement($token);
}
/**
* Get tokens
*
* @return Collection
*/
public function getTokens()
{
return $this->tokens;
}
public function getLocale()
{
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getFullName(): ?string
{
return $this->getFirstName() . ' ' . $this->getLastName();
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getBirthdate(): ?\DateTimeInterface
{
return $this->birthdate;
}
public function setBirthdate(?\DateTimeInterface $birthdate): self
{
$this->birthdate = $birthdate;
return $this;
}
public function getCountryOfResidence(): ?string
{
return $this->countryOfResidence;
}
public function setCountryOfResidence(?string $countryOfResidence): self
{
$this->countryOfResidence = $countryOfResidence;
return $this;
}
public function getUserIdentifier()
{
return $this->email;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getAgentDiscount(): int
{
return $this->agentDiscount;
}
public function setAgentDiscount(int $agentDiscount): self
{
$this->agentDiscount = $agentDiscount;
return $this;
}
public function isAgentAccount(): bool
{
return $this->agentDiscount > 0;
}
public function getPartnerCoupons(): Collection
{
return $this->partnerCoupons;
}
public function addPartnerCoupon(Coupon $coupon): self
{
if (!$this->partnerCoupons->contains($coupon)) {
$this->partnerCoupons[] = $coupon;
$coupon->setPartner($this);
}
return $this;
}
public function removePartnerCoupon(Coupon $coupon): self
{
if ($this->partnerCoupons->removeElement($coupon)) {
if ($coupon->getPartner() === $this) {
$coupon->setPartner(null);
}
}
return $this;
}
public function hasRole(string $role): bool
{
return in_array($role, $this->getRoles(), true);
}
}