src/Entity/User.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Service\Helper\DateTimeHelper;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Exception;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  11.  * @ORM\Table(name="users__users",
  12.  *     indexes={
  13.  *          @ORM\Index(name="password", columns={"password"})
  14.  *     })
  15.  */
  16. class User
  17. {
  18.     use IdTrait;
  19.     /**
  20.      * @ORM\Column(name="created_at", type="datetime", nullable=false)
  21.      */
  22.     private DateTime $createdAt;
  23.     /**
  24.      * @ORM\Column(name="phone", type="string", nullable=false, unique=true)
  25.      */
  26.     private string $phone;
  27.     /**
  28.      * @ORM\Column(name="password", type="string", nullable=false)
  29.      */
  30.     private string $password;
  31.     /**
  32.      * @ORM\Column(name="deprecated_user_id", type="integer", nullable=true)
  33.      */
  34.     private ?int $deprecatedUserId null;
  35.     /**
  36.      * @ORM\Column(name="cookie", type="string", nullable=false)
  37.      */
  38.     private string $cookie;
  39.     /**
  40.      * @var Collection|UserRole[]
  41.      *
  42.      * @ORM\OneToMany(targetEntity="App\Entity\UserRole", mappedBy="user")
  43.      */
  44.     private $roles;
  45.     /**
  46.      * @ORM\OneToOne(targetEntity="App\Entity\Cart", mappedBy="user")
  47.      */
  48.     private ?Cart $cart null;
  49.     /**
  50.      * @ORM\OneToOne(targetEntity="Manager", mappedBy="user")
  51.      */
  52.     private ?Manager $manager null;
  53.     /**
  54.      * @throws Exception
  55.      */
  56.     public function __construct(string $phonestring $password)
  57.     {
  58.         $this->createdAt DateTimeHelper::getNowDateTime();
  59.         $this->phone $phone;
  60.         $this->password $password;
  61.         $this->roles = new ArrayCollection();
  62.         $this->cookie $this->generateCookie();
  63.     }
  64.     /**
  65.      * @return DateTime
  66.      */
  67.     public function getCreatedAt(): DateTime
  68.     {
  69.         return $this->createdAt;
  70.     }
  71.     /**
  72.      * @return string
  73.      */
  74.     public function getPhone(): string
  75.     {
  76.         return $this->phone;
  77.     }
  78.     /**
  79.      * @return string
  80.      */
  81.     public function getPassword(): string
  82.     {
  83.         return $this->password;
  84.     }
  85.     /**
  86.      * @return UserRole[]|Collection
  87.      */
  88.     public function getRoles()
  89.     {
  90.         return $this->roles;
  91.     }
  92.     /**
  93.      * @return Cart|null
  94.      */
  95.     public function getCart(): ?Cart
  96.     {
  97.         return $this->cart;
  98.     }
  99.     public function getManager(): ?Manager
  100.     {
  101.         return $this->manager;
  102.     }
  103.     public function setCookie(string $cookie): User
  104.     {
  105.         $this->cookie $cookie;
  106.         return $this;
  107.     }
  108.     public function getCookie(): string
  109.     {
  110.         return $this->cookie;
  111.     }
  112.     public function renewCookie(): User
  113.     {
  114.         return $this->setCookie($this->generateCookie());
  115.     }
  116.     public function generateCookie(): string
  117.     {
  118.         return md5(time() . $this->getPhone() . $this->getPassword());
  119.     }
  120.     public function getDeprecatedUserId(): ?int
  121.     {
  122.         return $this->deprecatedUserId;
  123.     }
  124.     public function setDeprecatedUserId(?int $deprecatedUserId): User
  125.     {
  126.         $this->deprecatedUserId $deprecatedUserId;
  127.         return $this;
  128.     }
  129.     public function isInRoles(array $roles)
  130.     {
  131.         foreach ($this->getRoles() as $userRole) {
  132.             if(in_array($userRole->getRole(), $roles)) return true;
  133.         }
  134.         return false;
  135.     }
  136. }