src/Entity/TokenUser.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Services\TokenGenerator;
  4. use DateTime;
  5. use DateTimeInterface;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Exception;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\TokenUserRepository")
  11.  */
  12. class TokenUser
  13. {
  14.     use TimestampableEntity;
  15.     public const TOKEN_RECUPERATE_PASSWORD 'recuperate_password';
  16.     public const TOKEN_REGISTER 'register';
  17.     public const TOKEN_ACTIVE_USER 'active_user';
  18.     /**
  19.      *  @var int
  20.      * @ORM\Id()
  21.      * @ORM\GeneratedValue()
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     private $id;
  25.     /**
  26.      *  @var string
  27.      * @ORM\Column(type="text")
  28.      */
  29.     private $token;
  30.     /**
  31.      * @ORM\ManyToOne(targetEntity="User", inversedBy="tokens" ,cascade={"persist"})
  32.      */
  33.     private $user;
  34.     /**
  35.      * @ORM\Column(type="datetime",nullable=true)
  36.      */
  37.     private $expiredAt;
  38.     /**
  39.      * @var string
  40.      * @ORM\Column(type="string", length=255)
  41.      */
  42.     private $type;
  43.     /**
  44.      * @var bool
  45.      * @ORM\Column(type="boolean")
  46.      */
  47.     private $enabled;
  48.     /**
  49.      * TokenUser constructor.
  50.      *
  51.      * @throws Exception
  52.      */
  53.     public function __construct()
  54.     {
  55.         $generator = new TokenGenerator();
  56.         $this->token $generator->generateToken();
  57.         $this->enabled true;
  58.     }
  59.     public function getId(): ?int
  60.     {
  61.         return $this->id;
  62.     }
  63.     public function getToken(): ?string
  64.     {
  65.         return $this->token;
  66.     }
  67.     /**
  68.      * @return $this
  69.      */
  70.     public function setToken(string $token): self
  71.     {
  72.         $this->token $token;
  73.         return $this;
  74.     }
  75.     public function getCreatedAt(): ?DateTimeInterface
  76.     {
  77.         return $this->createdAt;
  78.     }
  79.     /**
  80.      * @return $this
  81.      */
  82.     public function setCreatedAt(DateTimeInterface $createdAt): self
  83.     {
  84.         $this->createdAt $createdAt;
  85.         return $this;
  86.     }
  87.     public function getUpdatedAt(): ?DateTimeInterface
  88.     {
  89.         return $this->updatedAt;
  90.     }
  91.     /**
  92.      * @return $this
  93.      */
  94.     public function setUpdatedAt(DateTimeInterface $updatedAt): self
  95.     {
  96.         $this->updatedAt $updatedAt;
  97.         return $this;
  98.     }
  99.     public function getExpiredAt(): ?DateTimeInterface
  100.     {
  101.         return $this->expiredAt;
  102.     }
  103.     /**
  104.      * @return $this
  105.      */
  106.     public function setExpiredAt(DateTimeInterface $expiredAt): self
  107.     {
  108.         $this->expiredAt $expiredAt;
  109.         return $this;
  110.     }
  111.     public function getType(): ?string
  112.     {
  113.         return $this->type;
  114.     }
  115.     /**
  116.      * @return $this
  117.      */
  118.     public function setType(string $type): self
  119.     {
  120.         $this->type $type;
  121.         //Set expired time
  122.         switch ($type) {
  123.             case $this::TOKEN_RECUPERATE_PASSWORD:
  124.                 $this->expiredAt = new DateTime('+3 day');
  125.                 break;
  126.             case $this::TOKEN_ACTIVE_USER:
  127.                 $this->expiredAt = new DateTime('+1 month');
  128.                 break;
  129.             default:
  130.                 $this->expiredAt = new DateTime();
  131.                 break;
  132.         }
  133.         return $this;
  134.     }
  135.     public function getUser(): ?User
  136.     {
  137.         return $this->user;
  138.     }
  139.     /**
  140.      * @return $this
  141.      */
  142.     public function setUser(?User $user): self
  143.     {
  144.         $this->user $user;
  145.         return $this;
  146.     }
  147.     public function getEnabled(): ?bool
  148.     {
  149.         return $this->enabled;
  150.     }
  151.     /**
  152.      * @return $this
  153.      */
  154.     public function setEnabled(bool $enabled): self
  155.     {
  156.         $this->enabled $enabled;
  157.         return $this;
  158.     }
  159. }