<?php
namespace App\Entity;
use App\Entity\Traits\TimestampableTrait;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\UserInterface;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @Vich\Uploadable
* @ApiResource(
* normalizationContext={"groups"={"user:read"}},
* denormalizationContext={"groups"={"user:write"}},
* collectionOperations={
* "get"={},
* "post"={
* "denormalization_context"={"groups"={"user:write"}}
* }
* },
* itemOperations={
* "get"={},
* "put"={},
* "delete"={}
* }
* )
*/
#[ORM\Table(name: 'user')]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'Il existe déjà un compte avec cet e-mail')]
class User implements UserInterface, \Serializable
{
use TimestampableTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['user:read'])]
private $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Groups(['user:read', 'user:write'])]
private $email;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['user:read', 'user:write'])]
private $firstname;
#[ORM\Column(type: 'json')]
#[Groups(['user:read', 'user:write'])]
private $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column(type: 'string')]
private $password;
/**
* Utiliser pour changer de mot de passe.
*/
#[Groups(['user:read', 'user:write'])]
private $plainPassword = null;
#[ORM\ManyToOne(targetEntity: AccountingFirm::class, inversedBy: 'users', fetch: 'EAGER')]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['user:read', 'user:write'])]
private $accountingFirm;
private $oldPassword;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $avatar = null;
/**
* @Vich\UploadableField(mapping="avatars", fileNameProperty="avatar")
* @var File|null
*/
private $tmpAvatar = null;
#[ORM\Column(type: 'boolean')]
private $activation_token = false;
#[ORM\ManyToOne(targetEntity: Affiliate::class, inversedBy: 'users')]
private $affiliate;
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: ShortUrl::class, orphanRemoval: true)]
private Collection $shortUrls;
#[ORM\Column(type: 'boolean')]
private $notificationSent = false;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Qrcode::class, cascade: ['persist', 'remove'])]
private Collection $qrcodes;
public function __construct()
{
$this->qrcodes = new ArrayCollection();
$this->shortUrls = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = is_string($this->roles) ? json_decode($this->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 UserInterface
*/
public function getPassword(): string
{
return $this->password;
}
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;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
public function getAccountingFirm(): ?AccountingFirm
{
return $this->accountingFirm;
}
public function setAccountingFirm(?AccountingFirm $accountingFirm): self
{
$this->accountingFirm = $accountingFirm;
return $this;
}
/**
* @return string|null
*/
public function getOldPassword(): ?string
{
return $this->oldPassword;
}
/**
* @param string|null $oldPassword
* @return User
*/
public function setOldPassword(?string $oldPassword): self
{
$this->oldPassword = $oldPassword;
return $this;
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
/**
* @return File|null
*/
public function getTmpAvatar(): ?File
{
return $this->tmpAvatar;
}
/**
* @param File|null $tmpAvatar
* @return User
*/
public function setTmpAvatar(?File $tmpAvatar): self
{
$this->tmpAvatar = $tmpAvatar;
$this->updatedAt = new \DateTime('now');
return $this;
}
public function getActivationToken(): ?bool
{
return $this->activation_token;
}
public function setActivationToken(bool $activation_token): self
{
$this->activation_token = $activation_token;
return $this;
}
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->email,
$this->password,
$this->plainPassword,
));
}
public function unserialize($serialized)
{
list(
$this->id,
$this->email,
$this->email,
$this->password,
$this->plainPassword,
) = unserialize($serialized);
}
public function getAffiliate(): ?Affiliate
{
return $this->affiliate;
}
public function setAffiliate(?Affiliate $affiliate): self
{
$this->affiliate = $affiliate;
return $this;
}
public function isNotificationSent(): bool
{
return $this->notificationSent;
}
public function setNotificationSent(bool $notificationSent): self
{
$this->notificationSent = $notificationSent;
return $this;
}
public function getQrcodes(): Collection
{
return $this->qrcodes;
}
public function addQrcode(Qrcode $qrcode): self
{
if (!$this->qrcodes->contains($qrcode)) {
$this->qrcodes[] = $qrcode;
$qrcode->setUser($this);
}
return $this;
}
public function removeQrcode(Qrcode $qrcode): self
{
if ($this->qrcodes->removeElement($qrcode)) {
if ($qrcode->getUser() === $this) {
$qrcode->setUser(null);
}
}
return $this;
}
public function getShortUrls(): Collection
{
return $this->shortUrls;
}
}