<?php
namespace App\Entity;
use App\Entity\{User, TimestampableTrait};
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
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)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use TimestampableTrait;
const ACTIVE = 'active';
const INACTIVE = 'inactive';
const PENDING_CONFIRMATION = 'pending confirmation';
const AWAITING_DOCUMENTS = 'awaiting documents';
const PENDING_RYVUP = 'pending ryvup verification';
const DECLINED_RYVUP = 'declined by ryvup';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $civility;
/**
* @ORM\Column(type="string", length=60)
*/
private $lastname;
/**
* @ORM\Column(type="string", length=60)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string")
*/
private $status;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $latestLogin;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $avatar;
/**
* @Assert\Image(maxSize="2M")
*/
private $fileAvatar;
/**
* @ORM\OneToOne(targetEntity=Client::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $client;
/**
* @ORM\OneToOne(targetEntity=Transporteur::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $transporteur;
/**
* @ORM\OneToOne(targetEntity=Collaborateur::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $collaborateur;
/**
* @ORM\OneToMany(targetEntity=Devis::class, mappedBy="user", cascade={"remove"}, orphanRemoval=true)
*/
private $devis;
/**
* @ORM\Column(type="float")
*/
private $creditBalance = 0.00;
/**
* @ORM\OneToMany(targetEntity=CreditTransaction::class, mappedBy="user", orphanRemoval=true)
*/
private $creditTransactions;
/**
* @ORM\OneToMany(targetEntity=Invoice::class, mappedBy="user", orphanRemoval=true)
*/
private $invoices;
public function __construct()
{
$this->devis = new ArrayCollection();
$this->creditTransactions = new ArrayCollection();
$this->invoices = new ArrayCollection();
}
public function preUpdate()
{
$this->lastname = ucwords(strtolower($this->lastname));
$this->firstname = ucwords(strtolower($this->firstname));
$this->email = strtolower($this->email);
}
public function getId(): ?int
{
return $this->id;
}
public function getCivility(): ?string
{
return $this->civility;
}
public function setCivility(string $civility): self
{
$this->civility = $civility;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
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 (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $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);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function isStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getLatestLogin(): ?\DateTimeInterface
{
return $this->latestLogin;
}
public function setLatestLogin(?\DateTimeInterface $latestLogin): self
{
$this->latestLogin = $latestLogin;
return $this;
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getFileAvatar()
{
return $this->fileAvatar;
}
public function setFileAvatar($fileAvatar)
{
$this->fileAvatar = $fileAvatar;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
/**
* 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;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
// unset the owning side of the relation if necessary
if ($client === null && $this->client !== null) {
$this->client->setUser(null);
}
// set the owning side of the relation if necessary
if ($client !== null && $client->getUser() !== $this) {
$client->setUser($this);
}
$this->client = $client;
return $this;
}
public function getTransporteur(): ?Transporteur
{
return $this->transporteur;
}
public function setTransporteur(?Transporteur $transporteur): self
{
// unset the owning side of the relation if necessary
if ($transporteur === null && $this->transporteur !== null) {
$this->transporteur->setUser(null);
}
// set the owning side of the relation if necessary
if ($transporteur !== null && $transporteur->getUser() !== $this) {
$transporteur->setUser($this);
}
$this->transporteur = $transporteur;
return $this;
}
public function getCollaborateur(): ?Collaborateur
{
return $this->collaborateur;
}
public function setCollaborateur(?Collaborateur $collaborateur): self
{
// unset the owning side of the relation if necessary
if ($collaborateur === null && $this->collaborateur !== null) {
$this->collaborateur->setUser(null);
}
// set the owning side of the relation if necessary
if ($collaborateur !== null && $collaborateur->getUser() !== $this) {
$collaborateur->setUser($this);
}
$this->collaborateur = $collaborateur;
return $this;
}
/**
* @return Collection<int, Devis>
*/
public function getDevis(): Collection
{
return $this->devis;
}
public function addDevi(Devis $devi): self
{
if (!$this->devis->contains($devi)) {
$this->devis[] = $devi;
$devi->setUser($this);
}
return $this;
}
public function removeDevi(Devis $devi): self
{
if ($this->devis->removeElement($devi)) {
// set the owning side to null (unless already changed)
if ($devi->getUser() === $this) {
$devi->setUser(null);
}
}
return $this;
}
public function getCreditBalance(): ?float
{
return $this->creditBalance;
}
public function setCreditBalance(float $creditBalance): self
{
$this->creditBalance = $creditBalance;
return $this;
}
public function debitCredits(int $credits): bool
{
if ($this->creditBalance >= $credits) {
$this->creditBalance -= $credits;
return true;
}
return false;
}
public function rechargeCredits(int $credits): self
{
$this->creditBalance += $credits;
return $this;
}
/**
* @return Collection<int, CreditTransaction>
*/
public function getCreditTransactions(): Collection
{
return $this->creditTransactions;
}
public function addCreditTransaction(CreditTransaction $creditTransaction): self
{
if (!$this->creditTransactions->contains($creditTransaction)) {
$this->creditTransactions[] = $creditTransaction;
$creditTransaction->setUser($this);
}
return $this;
}
public function removeCreditTransaction(CreditTransaction $creditTransaction): self
{
if ($this->creditTransactions->removeElement($creditTransaction)) {
// set the owning side to null (unless already changed)
if ($creditTransaction->getUser() === $this) {
$creditTransaction->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Invoice>
*/
public function getInvoices(): Collection
{
return $this->invoices;
}
public function addInvoice(Invoice $invoice): self
{
if (!$this->invoices->contains($invoice)) {
$this->invoices[] = $invoice;
$invoice->setUser($this);
}
return $this;
}
public function removeInvoice(Invoice $invoice): self
{
if ($this->invoices->removeElement($invoice)) {
// set the owning side to null (unless already changed)
if ($invoice->getUser() === $this) {
$invoice->setUser(null);
}
}
return $this;
}
}