<?php
namespace App\Entity;
use App\DBAL\Types\UserStatusType;
use App\Dto\SearchSubjectInterface;
use App\Repository\UserRepository;
use App\Service\PremiumService;
use App\Validator\Nip;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
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")
* @UniqueEntity(fields="nip", message="Istnieje już konto dla takiego numeru NIP")
* @ORM\HasLifecycleCallbacks()
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=true, hardDelete=false)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface, SearchSubjectInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\Email()
* @Assert\NotBlank()
* @Assert\NotNull()
*/
private string $email;
/**
* @ORM\Column(type="json")
* @var string[]
*/
private array $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private string $password;
/**
* @ORM\Column(type="userstatus", options={"default":UserStatusType::NEW})
* @var string
*/
private string $status = UserStatusType::NEW;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\NotNull()
* @Nip()
* @Assert\Length(min=10, max=10)
*/
private string $nip;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private bool $pkdVerified = false;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $name = null;
/**
* @ORM\Column(type="boolean")
*/
private bool $isVerified = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTime $premiumTo = null;
/**
* @ORM\Column(type="datetime")
*/
private DateTime $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTime $updatedAt = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTime $deletedAt = null;
/**
* @ORM\OneToMany(targetEntity=Logging::class, mappedBy="user")
* @var Collection<Logging>
*/
private Collection $loggings;
/**
* @ORM\OneToOne(targetEntity=Address::class, cascade={"persist", "remove"})
*/
private ?Address $address = null;
/**
* @ORM\OneToMany(targetEntity=OpinionRate::class, mappedBy="user", orphanRemoval=true)
* @var Collection<OpinionRate>
*/
private Collection $opinionRates;
/**
* @ORM\OneToMany(targetEntity=Opinion::class, mappedBy="user", orphanRemoval=true)
* @var Collection<Opinion>
*/
private Collection $opinions;
public function __construct()
{
$this->loggings = new ArrayCollection();
$this->opinionRates = new ArrayCollection();
$this->opinions = new ArrayCollection();
}
public function __toString()
{
return $this->email;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string $email
*
* @return $this
*/
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* @param array $roles
*
* @return $this
*/
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @param string $role
*
* @return $this
*/
public function addRole(string $role): self
{
$this->roles[] = $role;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
/**
* @param string $password
*
* @return $this
*/
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* 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;
}
/**
* @inheritDoc
*/
public function getUsername(): string
{
return $this->getUserIdentifier();
}
/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
/**
* @param string $status
*
* @return $this
*/
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
/**
* @return string
*/
public function getNip(): string
{
return $this->nip;
}
/**
* @param string $nip
*
* @return $this
*/
public function setNip(string $nip): self
{
$this->nip = $nip;
return $this;
}
/**
* @return bool
*/
public function isPkdVerified(): bool
{
return $this->pkdVerified;
}
/**
* @param bool $pkdVerified
*
* @return $this
*/
public function setPkdVerified(bool $pkdVerified): self
{
$this->pkdVerified = $pkdVerified;
return $this;
}
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string|null $name
*
* @return $this
*/
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return DateTime|null
*/
public function getPremiumTo(): ?DateTime
{
return $this->premiumTo;
}
/**
* @param DateTime|null $premiumTo
*
* @return $this
*/
public function setPremiumTo(?DateTime $premiumTo): self
{
$this->premiumTo = $premiumTo;
return $this;
}
/**
* @return DateTime
*/
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
/**
* @ORM\PrePersist()
* @return $this
*/
public function setCreatedAt(): self
{
$this->createdAt = new DateTime();
return $this;
}
/**
* @return DateTime|null
*/
public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}
/**
* @ORM\PreUpdate()
* @return $this
*/
public function setUpdatedAt(): self
{
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return DateTime|null
*/
public function getDeletedAt(): ?DateTime
{
return $this->deletedAt;
}
/**
* @param DateTime|null $deletedAt
*
* @return $this
*/
public function setDeletedAt(?DateTime $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* @return Collection<int, Logging>
*/
public function getLoggings(): Collection
{
return $this->loggings;
}
public function addLogging(Logging $logging): self
{
if (!$this->loggings->contains($logging)) {
$this->loggings[] = $logging;
$logging->setUser($this);
}
return $this;
}
public function removeLogging(Logging $logging): self
{
if ($this->loggings->removeElement($logging)) {
// set the owning side to null (unless already changed)
if ($logging->getUser() === $this) {
$logging->setUser(null);
}
}
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
/**
* @return Collection<int, OpinionRate>
*/
public function getOpinionRates(): Collection
{
return $this->opinionRates;
}
public function addOpinionRate(OpinionRate $opinionRate): self
{
if (!$this->opinionRates->contains($opinionRate)) {
$this->opinionRates[] = $opinionRate;
$opinionRate->setUser($this);
}
return $this;
}
public function removeOpinionRate(OpinionRate $opinionRate): self
{
if ($this->opinionRates->removeElement($opinionRate)) {
// set the owning side to null (unless already changed)
if ($opinionRate->getUser() === $this) {
$opinionRate->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Opinion>
*/
public function getOpinions(): Collection
{
return $this->opinions;
}
public function addOpinion(Opinion $opinion): self
{
if (!$this->opinions->contains($opinion)) {
$this->opinions[] = $opinion;
$opinion->setUser($this);
}
return $this;
}
public function removeOpinion(Opinion $opinion): self
{
if ($this->opinions->removeElement($opinion)) {
// set the owning side to null (unless already changed)
if ($opinion->getUser() === $this) {
$opinion->setUser(null);
}
}
return $this;
}
/**
* @return bool
*/
public function isVerified(): bool
{
return $this->isVerified;
}
/**
* @param bool $isVerified
*
* @return $this
*/
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return bool
*/
public function isPremium(): bool
{
return PremiumService::isPremium($this->getPremiumTo());
}
}