src/Entity/User.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\DBAL\Types\UserStatusType;
  4. use App\Dto\SearchSubjectInterface;
  5. use App\Repository\UserRepository;
  6. use App\Service\PremiumService;
  7. use App\Validator\Nip;
  8. use DateTime;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Mapping\Annotation as Gedmo;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ORM\Entity(repositoryClass=UserRepository::class)
  19.  * @UniqueEntity(fields="email")
  20.  * @UniqueEntity(fields="nip", message="Istnieje już konto dla takiego numeru NIP")
  21.  * @ORM\HasLifecycleCallbacks()
  22.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=true, hardDelete=false)
  23.  */
  24. class User implements UserInterfacePasswordAuthenticatedUserInterfaceSearchSubjectInterface
  25. {
  26.     /**
  27.      * @ORM\Id
  28.      * @ORM\GeneratedValue
  29.      * @ORM\Column(type="integer")
  30.      */
  31.     private int $id;
  32.     /**
  33.      * @ORM\Column(type="string", length=180, unique=true)
  34.      * @Assert\Email()
  35.      * @Assert\NotBlank()
  36.      * @Assert\NotNull()
  37.      */
  38.     private string $email;
  39.     /**
  40.      * @ORM\Column(type="json")
  41.      * @var string[]
  42.      */
  43.     private array $roles = [];
  44.     /**
  45.      * @var string The hashed password
  46.      * @ORM\Column(type="string")
  47.      */
  48.     private string $password;
  49.     /**
  50.      * @ORM\Column(type="userstatus", options={"default":UserStatusType::NEW})
  51.      * @var string
  52.      */
  53.     private string $status UserStatusType::NEW;
  54.     /**
  55.      * @ORM\Column(type="string", length=255)
  56.      * @Assert\NotBlank()
  57.      * @Assert\NotNull()
  58.      * @Nip()
  59.      * @Assert\Length(min=10, max=10)
  60.      */
  61.     private string $nip;
  62.     /**
  63.      * @ORM\Column(type="boolean", options={"default": false})
  64.      */
  65.     private bool $pkdVerified false;
  66.     /**
  67.      * @ORM\Column(type="string", length=255, nullable=true)
  68.      */
  69.     private ?string $name null;
  70.     /**
  71.      * @ORM\Column(type="boolean")
  72.      */
  73.     private bool $isVerified false;
  74.     /**
  75.      * @ORM\Column(type="datetime", nullable=true)
  76.      */
  77.     private ?DateTime $premiumTo null;
  78.     /**
  79.      * @ORM\Column(type="datetime")
  80.      */
  81.     private DateTime $createdAt;
  82.     /**
  83.      * @ORM\Column(type="datetime", nullable=true)
  84.      */
  85.     private ?DateTime $updatedAt null;
  86.     /**
  87.      * @ORM\Column(type="datetime", nullable=true)
  88.      */
  89.     private ?DateTime $deletedAt null;
  90.     /**
  91.      * @ORM\OneToMany(targetEntity=Logging::class, mappedBy="user")
  92.      * @var Collection<Logging>
  93.      */
  94.     private Collection $loggings;
  95.     /**
  96.      * @ORM\OneToOne(targetEntity=Address::class, cascade={"persist", "remove"})
  97.      */
  98.     private ?Address $address null;
  99.     /**
  100.      * @ORM\OneToMany(targetEntity=OpinionRate::class, mappedBy="user", orphanRemoval=true)
  101.      * @var Collection<OpinionRate>
  102.      */
  103.     private Collection $opinionRates;
  104.     /**
  105.      * @ORM\OneToMany(targetEntity=Opinion::class, mappedBy="user", orphanRemoval=true)
  106.      * @var Collection<Opinion>
  107.      */
  108.     private Collection $opinions;
  109.     public function __construct()
  110.     {
  111.         $this->loggings = new ArrayCollection();
  112.         $this->opinionRates = new ArrayCollection();
  113.         $this->opinions = new ArrayCollection();
  114.     }
  115.     public function __toString()
  116.     {
  117.         return $this->email;
  118.     }
  119.     /**
  120.      * @return int|null
  121.      */
  122.     public function getId(): ?int
  123.     {
  124.         return $this->id;
  125.     }
  126.     /**
  127.      * @return string|null
  128.      */
  129.     public function getEmail(): ?string
  130.     {
  131.         return $this->email;
  132.     }
  133.     /**
  134.      * @param string $email
  135.      *
  136.      * @return $this
  137.      */
  138.     public function setEmail(string $email): self
  139.     {
  140.         $this->email $email;
  141.         return $this;
  142.     }
  143.     /**
  144.      * A visual identifier that represents this user.
  145.      * @see UserInterface
  146.      */
  147.     public function getUserIdentifier(): string
  148.     {
  149.         return $this->email;
  150.     }
  151.     /**
  152.      * @see UserInterface
  153.      */
  154.     public function getRoles(): array
  155.     {
  156.         $roles $this->roles;
  157.         // guarantee every user at least has ROLE_USER
  158.         $roles[] = 'ROLE_USER';
  159.         return array_unique($roles);
  160.     }
  161.     /**
  162.      * @param array $roles
  163.      *
  164.      * @return $this
  165.      */
  166.     public function setRoles(array $roles): self
  167.     {
  168.         $this->roles $roles;
  169.         return $this;
  170.     }
  171.     /**
  172.      * @param string $role
  173.      *
  174.      * @return $this
  175.      */
  176.     public function addRole(string $role): self
  177.     {
  178.         $this->roles[] = $role;
  179.         return $this;
  180.     }
  181.     /**
  182.      * @see PasswordAuthenticatedUserInterface
  183.      */
  184.     public function getPassword(): string
  185.     {
  186.         return $this->password;
  187.     }
  188.     /**
  189.      * @param string $password
  190.      *
  191.      * @return $this
  192.      */
  193.     public function setPassword(string $password): self
  194.     {
  195.         $this->password $password;
  196.         return $this;
  197.     }
  198.     /**
  199.      * Returning a salt is only needed, if you are not using a modern
  200.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  201.      * @see UserInterface
  202.      */
  203.     public function getSalt(): ?string
  204.     {
  205.         return null;
  206.     }
  207.     /**
  208.      * @see UserInterface
  209.      */
  210.     public function eraseCredentials()
  211.     {
  212.         // If you store any temporary, sensitive data on the user, clear it here
  213.         // $this->plainPassword = null;
  214.     }
  215.     /**
  216.      * @inheritDoc
  217.      */
  218.     public function getUsername(): string
  219.     {
  220.         return $this->getUserIdentifier();
  221.     }
  222.     /**
  223.      * @return string
  224.      */
  225.     public function getStatus(): string
  226.     {
  227.         return $this->status;
  228.     }
  229.     /**
  230.      * @param string $status
  231.      *
  232.      * @return $this
  233.      */
  234.     public function setStatus(string $status): self
  235.     {
  236.         $this->status $status;
  237.         return $this;
  238.     }
  239.     /**
  240.      * @return string
  241.      */
  242.     public function getNip(): string
  243.     {
  244.         return $this->nip;
  245.     }
  246.     /**
  247.      * @param string $nip
  248.      *
  249.      * @return $this
  250.      */
  251.     public function setNip(string $nip): self
  252.     {
  253.         $this->nip $nip;
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return bool
  258.      */
  259.     public function isPkdVerified(): bool
  260.     {
  261.         return $this->pkdVerified;
  262.     }
  263.     /**
  264.      * @param bool $pkdVerified
  265.      *
  266.      * @return $this
  267.      */
  268.     public function setPkdVerified(bool $pkdVerified): self
  269.     {
  270.         $this->pkdVerified $pkdVerified;
  271.         return $this;
  272.     }
  273.     /**
  274.      * @return string|null
  275.      */
  276.     public function getName(): ?string
  277.     {
  278.         return $this->name;
  279.     }
  280.     /**
  281.      * @param string|null $name
  282.      *
  283.      * @return $this
  284.      */
  285.     public function setName(?string $name): self
  286.     {
  287.         $this->name $name;
  288.         return $this;
  289.     }
  290.     /**
  291.      * @return DateTime|null
  292.      */
  293.     public function getPremiumTo(): ?DateTime
  294.     {
  295.         return $this->premiumTo;
  296.     }
  297.     /**
  298.      * @param DateTime|null $premiumTo
  299.      *
  300.      * @return $this
  301.      */
  302.     public function setPremiumTo(?DateTime $premiumTo): self
  303.     {
  304.         $this->premiumTo $premiumTo;
  305.         return $this;
  306.     }
  307.     /**
  308.      * @return DateTime
  309.      */
  310.     public function getCreatedAt(): DateTime
  311.     {
  312.         return $this->createdAt;
  313.     }
  314.     /**
  315.      * @ORM\PrePersist()
  316.      * @return $this
  317.      */
  318.     public function setCreatedAt(): self
  319.     {
  320.         $this->createdAt = new DateTime();
  321.         return $this;
  322.     }
  323.     /**
  324.      * @return DateTime|null
  325.      */
  326.     public function getUpdatedAt(): ?DateTime
  327.     {
  328.         return $this->updatedAt;
  329.     }
  330.     /**
  331.      * @ORM\PreUpdate()
  332.      * @return $this
  333.      */
  334.     public function setUpdatedAt(): self
  335.     {
  336.         $this->updatedAt = new DateTime();
  337.         return $this;
  338.     }
  339.     /**
  340.      * @return DateTime|null
  341.      */
  342.     public function getDeletedAt(): ?DateTime
  343.     {
  344.         return $this->deletedAt;
  345.     }
  346.     /**
  347.      * @param DateTime|null $deletedAt
  348.      *
  349.      * @return $this
  350.      */
  351.     public function setDeletedAt(?DateTime $deletedAt): self
  352.     {
  353.         $this->deletedAt $deletedAt;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @return Collection<int, Logging>
  358.      */
  359.     public function getLoggings(): Collection
  360.     {
  361.         return $this->loggings;
  362.     }
  363.     public function addLogging(Logging $logging): self
  364.     {
  365.         if (!$this->loggings->contains($logging)) {
  366.             $this->loggings[] = $logging;
  367.             $logging->setUser($this);
  368.         }
  369.         return $this;
  370.     }
  371.     public function removeLogging(Logging $logging): self
  372.     {
  373.         if ($this->loggings->removeElement($logging)) {
  374.             // set the owning side to null (unless already changed)
  375.             if ($logging->getUser() === $this) {
  376.                 $logging->setUser(null);
  377.             }
  378.         }
  379.         return $this;
  380.     }
  381.     public function getAddress(): ?Address
  382.     {
  383.         return $this->address;
  384.     }
  385.     public function setAddress(?Address $address): self
  386.     {
  387.         $this->address $address;
  388.         return $this;
  389.     }
  390.     /**
  391.      * @return Collection<int, OpinionRate>
  392.      */
  393.     public function getOpinionRates(): Collection
  394.     {
  395.         return $this->opinionRates;
  396.     }
  397.     public function addOpinionRate(OpinionRate $opinionRate): self
  398.     {
  399.         if (!$this->opinionRates->contains($opinionRate)) {
  400.             $this->opinionRates[] = $opinionRate;
  401.             $opinionRate->setUser($this);
  402.         }
  403.         return $this;
  404.     }
  405.     public function removeOpinionRate(OpinionRate $opinionRate): self
  406.     {
  407.         if ($this->opinionRates->removeElement($opinionRate)) {
  408.             // set the owning side to null (unless already changed)
  409.             if ($opinionRate->getUser() === $this) {
  410.                 $opinionRate->setUser(null);
  411.             }
  412.         }
  413.         return $this;
  414.     }
  415.     /**
  416.      * @return Collection<int, Opinion>
  417.      */
  418.     public function getOpinions(): Collection
  419.     {
  420.         return $this->opinions;
  421.     }
  422.     public function addOpinion(Opinion $opinion): self
  423.     {
  424.         if (!$this->opinions->contains($opinion)) {
  425.             $this->opinions[] = $opinion;
  426.             $opinion->setUser($this);
  427.         }
  428.         return $this;
  429.     }
  430.     public function removeOpinion(Opinion $opinion): self
  431.     {
  432.         if ($this->opinions->removeElement($opinion)) {
  433.             // set the owning side to null (unless already changed)
  434.             if ($opinion->getUser() === $this) {
  435.                 $opinion->setUser(null);
  436.             }
  437.         }
  438.         return $this;
  439.     }
  440.     /**
  441.      * @return bool
  442.      */
  443.     public function isVerified(): bool
  444.     {
  445.         return $this->isVerified;
  446.     }
  447.     /**
  448.      * @param bool $isVerified
  449.      *
  450.      * @return $this
  451.      */
  452.     public function setIsVerified(bool $isVerified): self
  453.     {
  454.         $this->isVerified $isVerified;
  455.         return $this;
  456.     }
  457.     /**
  458.      * @return bool
  459.      */
  460.     public function isPremium(): bool
  461.     {
  462.         return PremiumService::isPremium($this->getPremiumTo());
  463.     }
  464. }