src/Entity/User.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\UserRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * @ORM\Entity(repositoryClass=UserRepository::class)
  15.  * @UniqueEntity(fields={"email"}, message="This e-mail already exists in the system")
  16.  * @UniqueEntity(fields={"username"}, message="This user already exists in the system")
  17.  */
  18. class User implements UserInterfacePasswordAuthenticatedUserInterface
  19. {
  20.     /**
  21.      * @ORM\OneToMany(targetEntity="App\Entity\UserToken", mappedBy="user", cascade={"all"}, orphanRemoval=true)
  22.      * @ORM\OrderBy({"id" = "ASC"})
  23.      */
  24.     protected $tokens;
  25.     /**
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue
  28.      * @ORM\Column(type="integer")
  29.      */
  30.     private $id;
  31.     /**
  32.      * @ORM\Column(type="string", length=180, unique=true)
  33.      */
  34.     private $username;
  35.     /**
  36.      * @ORM\Column(type="json")
  37.      */
  38.     private $roles = [];
  39.     /**
  40.      * @var string The hashed password
  41.      * @ORM\Column(type="string")
  42.      */
  43.     private $password;
  44.     /**
  45.      * @ORM\Column(type="string", length=255, nullable=true)
  46.      * @Assert\NotBlank(message="pages.order.form.validation.email.notBlank", groups={"Registration", "Profile"})
  47.      * @Assert\Email(message="pages.order.form.validation.email.invalid", groups={"Registration", "Profile"})
  48.      */
  49.     private $email;
  50.     /**
  51.      * @ORM\Column(type="string", length=20, nullable=true)
  52.      * @Assert\NotBlank(message="pages.order.form.validation.phone.notBlank", groups={"Registration"})
  53.      * @Assert\Regex(pattern="/^[0-9+]*$/", message="pages.order.form.validation.phone.invalidRegex", groups={"Registration"})
  54.      */
  55.     private $phone;
  56.     /**
  57.      * @ORM\Column(type="string", length=255, nullable=true)
  58.      * @Assert\NotBlank(message="pages.user.profileEdit.form.validation.firstName.notBlank", groups={"Profile"})
  59.      */
  60.     private $firstName;
  61.     /**
  62.      * @ORM\Column(type="string", length=255, nullable=true)
  63.      * @Assert\NotBlank(message="pages.user.profileEdit.form.validation.lastName.notBlank", groups={"Profile"})
  64.      */
  65.     private $lastName;
  66.     /**
  67.      * @ORM\Column(type="date", nullable=true)
  68.      * @Assert\NotBlank(message="pages.user.profileEdit.form.validation.birthdate.notBlank", groups={"Profile"})
  69.      * @Assert\LessThan("-18 years", message="pages.user.profileEdit.form.validation.birthdate.ageRestriction", groups={"Profile"})
  70.      */
  71.     private $birthdate;
  72.     /**
  73.      * @ORM\Column(type="string", length=2, nullable=true)
  74.      * @Assert\NotBlank(message="pages.user.profileEdit.form.validation.countryOfResidence.notBlank", groups={"Profile"})
  75.      * @Assert\Length(min=2, max=2, minMessage="pages.user.profileEdit.form.validation.countryOfResidence.minLength", maxMessage="pages.user.profileEdit.form.validation.countryOfResidence.maxLength", groups={"Profile"})
  76.      */
  77.     private $countryOfResidence;
  78.     /**
  79.      * @ORM\Column(type="boolean")
  80.      */
  81.     private $isVerified false;
  82.     /**
  83.      * @ORM\Column(type="integer", options={"default": 0})
  84.      */
  85.     private int $agentDiscount 0;
  86.     private ?string $passwordPlain null;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity=Coupon::class, mappedBy="partner")
  89.      */
  90.     private $partnerCoupons;
  91.     public function __construct()
  92.     {
  93.         $this->tokens = new ArrayCollection();
  94.         $this->partnerCoupons = new ArrayCollection();
  95.     }
  96.     public function __toString(): string
  97.     {
  98.         return $this->username ?? '';
  99.     }
  100.     public function getId(): ?int
  101.     {
  102.         return $this->id;
  103.     }
  104.     /**
  105.      * A visual identifier that represents this user.
  106.      *
  107.      * @see UserInterface
  108.      */
  109.     public function getUsername(): string
  110.     {
  111.         return (string)$this->username;
  112.     }
  113.     public function setUsername(string $username): self
  114.     {
  115.         $this->username $username;
  116.         return $this;
  117.     }
  118.     /**
  119.      * @see UserInterface
  120.      */
  121.     public function getRoles(): array
  122.     {
  123.         $roles $this->roles;
  124.         $roles[] = 'ROLE_USER';
  125.         return array_unique($roles);
  126.     }
  127.     public function setRoles(array $roles): self
  128.     {
  129.         $this->roles $roles;
  130.         return $this;
  131.     }
  132.     /**
  133.      * @see UserInterface
  134.      */
  135.     public function getPassword(): string
  136.     {
  137.         return $this->password;
  138.     }
  139.     public function setPassword(string $password): self
  140.     {
  141.         $this->password $password;
  142.         return $this;
  143.     }
  144.     public function setPasswordPlain(?string $password): self
  145.     {
  146.         $this->passwordPlain $password;
  147.         return $this;
  148.     }
  149.     public function getPasswordPlain(): ?string
  150.     {
  151.         return $this->passwordPlain;
  152.     }
  153.     /**
  154.      * Returning a salt is only needed, if you are not using a modern
  155.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  156.      *
  157.      * @see UserInterface
  158.      */
  159.     public function getSalt(): ?string
  160.     {
  161.         return null;
  162.     }
  163.     /**
  164.      * @see UserInterface
  165.      */
  166.     public function eraseCredentials()
  167.     {
  168.         // If you store any temporary, sensitive data on the user, clear it here
  169.         //$this->plainPassword = null;
  170.     }
  171.     public function getEmail(): ?string
  172.     {
  173.         return $this->email;
  174.     }
  175.     public function setEmail(?string $email): self
  176.     {
  177.         $this->email $email;
  178.         return $this;
  179.     }
  180.     /**
  181.      * Add token
  182.      *
  183.      *
  184.      * @return User
  185.      */
  186.     public function addToken(UserToken $token)
  187.     {
  188.         $this->tokens[] = $token;
  189.         return $this;
  190.     }
  191.     /**
  192.      * Remove token
  193.      *
  194.      */
  195.     public function removeToken(UserToken $token)
  196.     {
  197.         $this->tokens->removeElement($token);
  198.     }
  199.     /**
  200.      * Get tokens
  201.      *
  202.      * @return Collection
  203.      */
  204.     public function getTokens()
  205.     {
  206.         return $this->tokens;
  207.     }
  208.     public function getLocale()
  209.     {
  210.     }
  211.     public function getPhone(): ?string
  212.     {
  213.         return $this->phone;
  214.     }
  215.     public function setPhone(?string $phone): self
  216.     {
  217.         $this->phone $phone;
  218.         return $this;
  219.     }
  220.     public function getFullName(): ?string
  221.     {
  222.         return $this->getFirstName() . ' ' $this->getLastName();
  223.     }
  224.     public function getFirstName(): ?string
  225.     {
  226.         return $this->firstName;
  227.     }
  228.     public function setFirstName(?string $firstName): self
  229.     {
  230.         $this->firstName $firstName;
  231.         return $this;
  232.     }
  233.     public function getLastName(): ?string
  234.     {
  235.         return $this->lastName;
  236.     }
  237.     public function setLastName(string $lastName): self
  238.     {
  239.         $this->lastName $lastName;
  240.         return $this;
  241.     }
  242.     public function getBirthdate(): ?\DateTimeInterface
  243.     {
  244.         return $this->birthdate;
  245.     }
  246.     public function setBirthdate(?\DateTimeInterface $birthdate): self
  247.     {
  248.         $this->birthdate $birthdate;
  249.         return $this;
  250.     }
  251.     public function getCountryOfResidence(): ?string
  252.     {
  253.         return $this->countryOfResidence;
  254.     }
  255.     public function setCountryOfResidence(?string $countryOfResidence): self
  256.     {
  257.         $this->countryOfResidence $countryOfResidence;
  258.         return $this;
  259.     }
  260.     public function getUserIdentifier()
  261.     {
  262.         return $this->email;
  263.     }
  264.     public function isVerified(): bool
  265.     {
  266.         return $this->isVerified;
  267.     }
  268.     public function setIsVerified(bool $isVerified): self
  269.     {
  270.         $this->isVerified $isVerified;
  271.         return $this;
  272.     }
  273.     public function getAgentDiscount(): int
  274.     {
  275.         return $this->agentDiscount;
  276.     }
  277.     public function setAgentDiscount(int $agentDiscount): self
  278.     {
  279.         $this->agentDiscount $agentDiscount;
  280.         return $this;
  281.     }
  282.     public function isAgentAccount(): bool
  283.     {
  284.         return $this->agentDiscount 0;
  285.     }
  286.     public function getPartnerCoupons(): Collection
  287.     {
  288.         return $this->partnerCoupons;
  289.     }
  290.     public function addPartnerCoupon(Coupon $coupon): self
  291.     {
  292.         if (!$this->partnerCoupons->contains($coupon)) {
  293.             $this->partnerCoupons[] = $coupon;
  294.             $coupon->setPartner($this);
  295.         }
  296.         return $this;
  297.     }
  298.     public function removePartnerCoupon(Coupon $coupon): self
  299.     {
  300.         if ($this->partnerCoupons->removeElement($coupon)) {
  301.             if ($coupon->getPartner() === $this) {
  302.                 $coupon->setPartner(null);
  303.             }
  304.         }
  305.         return $this;
  306.     }
  307.     public function hasRole(string $role): bool
  308.     {
  309.         return in_array($role$this->getRoles(), true);
  310.     }
  311. }