<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table(name="app_user")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=2, nullable=true)
* @Assert\Locale(
* canonicalize = true
* )
*/
protected $locale;
/**
* @ORM\Column(type="string", length=180, nullable=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=180, nullable=true)
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private $password;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $gender;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": false})
*/
private $enabled;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastActivityAt;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private $deleted;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @ORM\OneToMany(targetEntity=Image::class, mappedBy="author", cascade={"persist", "remove"})
*/
private $images;
/**
* @ORM\Column(type="string", length=100, nullable=true)
* @Assert\Length(
* min = 10,
* max = 20
* )
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $resetToken;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $firstName;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $address;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $postalCode;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $city;
/**
* @ORM\Column(type="boolean", options={"default": true})
*/
private $optin;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $companyName;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $internalMemo;
/**
* @ORM\Column(type="string", length=13, nullable=true)
*/
private $uniqueId;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $publicId;
/**
* @ORM\Column(type="string", length=20, nullable=true)
* @Assert\Length(
* min = 2,
* max = 20
* )
*/
private $pseudo;
/**
* @ORM\ManyToOne(targetEntity=Platform::class, inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*/
private $platform;
/**
* @ORM\OneToMany(targetEntity=Subscription::class, mappedBy="user")
*/
private $subscriptions;
/**
* @ORM\OneToMany(targetEntity=Ticket::class, mappedBy="user")
*/
private $tickets;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private $hidden;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $callbackUrl;
/**
* @ORM\Column(type="string", unique=true, length=255, nullable=true)
*/
private $apiToken;
/**
* @ORM\OneToMany(targetEntity=Stat::class, mappedBy="publisher")
*/
private $stats;
public function __construct()
{
$this->deleted = false;
$this->createdAt = new \Datetime();
$this->updatedAt = new \Datetime();
$this->enabled = false;
$this->optin = true;
$this->uniqueId = uniqid();
$this->publicId = uniqid();
$this->subscriptions = new ArrayCollection();
$this->tickets = new ArrayCollection();
$this->images = new ArrayCollection();
$this->hidden = false;
$this->apiToken = self::newApiToken();
$this->stats = new ArrayCollection();
}
public static function newApiToken()
{
return bin2hex(random_bytes(64));
}
public function getId(): ?int
{
return $this->id;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function addRole($role): self
{
if (!in_array($role, $this->roles)) {
$this->roles[] = $role;
}
return $this;
}
public function hasRole($role): ?bool
{
foreach($this->roles as $r) {
if ($r == $role) {
return true;
}
}
return false;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword($password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getGender(): ?bool
{
return $this->gender;
}
public function setGender(?bool $gender): self
{
$this->gender = $gender;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(?bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getLocale(): ?string
{
return $this->locale;
}
public function setLocale($locale): self
{
$this->locale = $locale;
return $this;
}
public function getLastActivityAt(): ?\DateTimeInterface
{
return $this->lastActivityAt;
}
public function setLastActivityAt(?\DateTimeInterface $lastActivityAt): self
{
$this->lastActivityAt = $lastActivityAt;
return $this;
}
public function isActiveNow()
{
// Delay during wich the user will be considered as still active
$delay = new \DateTime('1 minute ago');
return ( $this->getLastActivityAt() > $delay );
}
public function getDeleted(): ?bool
{
return $this->deleted;
}
public function setDeleted(bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
public function getDeletedAt(): ?\DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail($email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone($phone): self
{
$this->phone = $phone;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName($lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername($username): self
{
$this->username = $username;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName($firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress($address): self
{
$this->address = $address;
return $this;
}
public function getPostalCode(): ?string
{
return $this->postalCode;
}
public function setPostalCode($postalCode): self
{
$this->postalCode = $postalCode;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity($city): self
{
$this->city = $city;
return $this;
}
public function getOptin(): ?bool
{
return $this->optin;
}
public function setOptin(bool $optin): self
{
$this->optin = $optin;
return $this;
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function setCompanyName($companyName): self
{
$this->companyName = $companyName;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getInternalMemo(): ?string
{
return $this->internalMemo;
}
public function setInternalMemo($internalMemo): self
{
$this->internalMemo = $internalMemo;
return $this;
}
public function getResetToken(): ?string
{
return $this->resetToken;
}
public function setResetToken($resetToken): self
{
$this->resetToken = $resetToken;
return $this;
}
public function generateNewResetToken()
{
return md5(random_bytes(20));
}
public function setNewResetToken(): self
{
$this->resetToken = $this->generateNewResetToken();
return $this;
}
public function getUniqueId(): ?string
{
return $this->uniqueId;
}
public function setUniqueId($uniqueId): self
{
$this->uniqueId = $uniqueId;
return $this;
}
public function getPlatform(): ?Platform
{
return $this->platform;
}
public function setPlatform(?Platform $platform): self
{
$this->platform = $platform;
return $this;
}
/**
* @return Collection|Subscription[]
*/
public function getSubscriptions(): Collection
{
return $this->subscriptions;
}
public function addSubscription(Subscription $subscription): self
{
if (!$this->subscriptions->contains($subscription)) {
$this->subscriptions[] = $subscription;
$subscription->setUser($this);
}
return $this;
}
public function removeSubscription(Subscription $subscription): self
{
if ($this->subscriptions->removeElement($subscription)) {
// set the owning side to null (unless already changed)
if ($subscription->getUser() === $this) {
$subscription->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Ticket[]
*/
public function getTickets(): Collection
{
return $this->tickets;
}
public function addTicket(Ticket $ticket): self
{
if (!$this->tickets->contains($ticket)) {
$this->tickets[] = $ticket;
$ticket->setUser($this);
}
return $this;
}
public function removeTicket(Ticket $ticket): self
{
if ($this->tickets->removeElement($ticket)) {
// set the owning side to null (unless already changed)
if ($ticket->getUser() === $this) {
$ticket->setUser(null);
}
}
return $this;
}
public function getPublicId(): ?string
{
return $this->publicId;
}
public function setPublicId(?string $publicId): self
{
$this->publicId = $publicId;
return $this;
}
public function getPseudo(): ?string
{
return $this->pseudo;
}
public function setPseudo(?string $pseudo): self
{
$this->pseudo = $pseudo;
return $this;
}
/**
* @return Collection|Image[]
*/
public function getImages(): Collection
{
return $this->images;
}
public function addImage(Image $image): self
{
if (!$this->images->contains($image)) {
$this->images[] = $image;
$image->setAuthor($this);
}
return $this;
}
public function removeImage(Image $image): self
{
if ($this->images->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getAuthor() === $this) {
$image->setAuthor(null);
}
}
return $this;
}
public function getUniqueName(): ?string
{
return '#'.$this->id.' '.$this->pseudo;
}
public function getAutocompletionLabel(): ?string
{
return $this->getUniqueName();
}
public function getHidden(): ?bool
{
return $this->hidden;
}
public function setHidden(bool $hidden): self
{
$this->hidden = $hidden;
return $this;
}
public function getCallbackUrl(): ?string
{
return $this->callbackUrl;
}
public function setCallbackUrl(?string $callbackUrl): self
{
$this->callbackUrl = $callbackUrl;
return $this;
}
public function getApiToken(): ?string
{
return $this->apiToken;
}
public function setApiToken(?string $apiToken): self
{
$this->apiToken = $apiToken;
return $this;
}
/**
* @return Collection|Stat[]
*/
public function getStats(): Collection
{
return $this->stats;
}
public function addStat(Stat $stat): self
{
if (!$this->stats->contains($stat)) {
$this->stats[] = $stat;
$stat->setPublisher($this);
}
return $this;
}
public function removeStat(Stat $stat): self
{
if ($this->stats->removeElement($stat)) {
// set the owning side to null (unless already changed)
if ($stat->getPublisher() === $this) {
$stat->setPublisher(null);
}
}
return $this;
}
}