<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Controller\ControllerChangePassword;
use App\Controller\ControllerResetPassword;
use App\Dto\ConfirmationOtpInput;
use App\Dto\ConfirmationOtpOuput;
use App\Dto\OtpInput;
use App\Dto\OtpOuput;
use App\Controller\CreateAcademicienAction;
use App\Repository\UserRepository;
use App\Service\Utils;
use DateTimeImmutable;
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\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields:'phone', message:'Le numéro existe déjà')]
// #[UniqueEntity(fields:'email', message:'L\'email existe déjà')]
#[ApiResource(
normalizationContext:[
'groups'=>['user:read']
],
collectionOperations: [
"get" ,
"post",
'changePassword' => [
'method' => 'GET',
'path' => "users/change-password/{userId}/{lastpassword}/{newpassword}",
'controller' => ControllerChangePassword::class,
'read' => false,
],
'resetPassword' => [
'method' => 'GET',
'path' => 'users/reset-password/{phone}',
'controller' => ControllerResetPassword::class,
'read' => false,
],
'createAcademicien' => [
'method' => 'POST',
'path' => "users/academicien",
'controller' => CreateAcademicienAction::class,
'read' => false,
'denormalization_context' => ['groups' => 'academicien'],
],
],
itemOperations: [
"get" ,
"put" ,
"delete"
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'firstName'=>'ipartial',
'lastName' =>'ipartial',
'reference'=>'exact',
'academy' =>'exact',
'parent' =>'exact',
'typeUser' =>'exact',
'academy' =>'exact'
])]
#[ApiFilter(DateFilter::class, properties: ['dateBirth'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['reservation:read','user:read','payement:read','abonnement:read'])]
private $id;
#[ORM\Column(type: 'string', length: 60)]
#[Assert\NotBlank]
#[Assert\NotNull]
#[Groups(['reservation:read','academicien','user:read','payement:read','abonnement:read'])]
private $firstName;
#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
#[Assert\NotNull]
#[Groups(['reservation:read','academicien','user:read','abonnement:read'])]
private $lastName;
#[ORM\Column(type: 'string', length: 16, nullable:true)]
#[Groups(['reservation:read','academicien','user:read','abonnement:read'])]
private $phone = null;
#[ORM\Column(type: 'string', length: 255, nullable:true)]
private $password = null;
#[ORM\OneToMany(mappedBy: 'customer', targetEntity: Reservation::class)]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
#[Groups(['user:read'])]
private $reservations;
#[ORM\Column(type: 'string', length: 10)]
#[Groups(['reservation:read','academicien','user:read','abonnement:read'])]
private $reference;
#[ORM\Column(type: 'string', length: 180, nullable: true)]
#[Groups(['user:read','abonnement:read'])]
private $email;
#[ORM\Column(type: 'datetime')]
#[Groups(['user:read'])]
private $createdAt;
#[ORM\ManyToOne(targetEntity: TypeUser::class)]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['academicien','user:read'])]
private $typeUser;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'users')]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
/**
* @ORM\JoinColumn(onDelete="SET NULL")
*/
#[Groups(['academicien','user:read'])]
private $parent;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private $users;
#[ORM\Column(type: 'string', length: 20, nullable: true)]
#[Groups(['user:read'])]
private $code;
#[ORM\ManyToOne(targetEntity: Academy::class, inversedBy: 'users')]
#[Groups(['academicien','user:read'])]
private $academy;
#[ORM\OneToMany(mappedBy: 'academicien', targetEntity: Abonnement::class)]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
/**
* @ORM\JoinColumn(onDelete="SET NULL")
*/
#[Groups(['academicien','user:read'])]
private $abonnements;
#[ORM\Column(type: 'date', nullable: true)]
#[Groups(['academicien','user:read'])]
private $dateBirth;
#[ORM\Column(type: 'datetime', nullable: true)]
#[Groups(['academicien','user:read'])]
private $dateLimited;
#[ORM\Column(type: 'boolean', nullable: true)]
#[Groups(['academicien','user:read'])]
private $isOld;
public function __construct()
{
$this->reservations = new ArrayCollection();
$this->createdAt = new DateTimeImmutable();
$this->users = new ArrayCollection();
$this->abonnements = new ArrayCollection();
$this->isOld = false;
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = strtolower($firstName);
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = strtolower($lastName);
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returns the roles granted to the user.
*
* public function getRoles()
* {
* return ['ROLE_USER'];
* }
*
* Alternatively, the roles might be stored in a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return string[]
*/
public function getRoles(): array
{
return array('ROLE_USER');
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
}
/**
* Returns the identifier for this user (e.g. its username or email address).
*/
public function getUserIdentifier(): string
{
return $this->phone;
}
/**
* @return Collection<int, Reservation>
*/
public function getReservations(): Collection
{
return $this->reservations;
}
public function addReservation(Reservation $reservation): self
{
if (!$this->reservations->contains($reservation)) {
$this->reservations[] = $reservation;
$reservation->setCustomer($this);
}
return $this;
}
public function removeReservation(Reservation $reservation): self
{
if ($this->reservations->removeElement($reservation)) {
// set the owning side to null (unless already changed)
if ($reservation->getCustomer() === $this) {
$reservation->setCustomer(null);
}
}
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getTypeUser(): ?TypeUser
{
return $this->typeUser;
}
public function setTypeUser(?TypeUser $typeUser): self
{
$this->typeUser = $typeUser;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(self $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setParent($this);
}
return $this;
}
public function removeUser(self $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getParent() === $this) {
$user->setParent(null);
}
}
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getAcademy(): ?Academy
{
return $this->academy;
}
public function setAcademy(?Academy $academy): self
{
$this->academy = $academy;
return $this;
}
/**
* @return Collection<int, Abonnement>
*/
public function getAbonnements(): Collection
{
return $this->abonnements;
}
public function addAbonnement(Abonnement $abonnement): self
{
if (!$this->abonnements->contains($abonnement)) {
$this->abonnements[] = $abonnement;
$abonnement->setAcademicien($this);
}
return $this;
}
public function removeAbonnement(Abonnement $abonnement): self
{
if ($this->abonnements->removeElement($abonnement)) {
// set the owning side to null (unless already changed)
if ($abonnement->getAcademicien() === $this) {
$abonnement->setAcademicien(null);
}
}
return $this;
}
public function getDateBirth(): ?\DateTimeInterface
{
return $this->dateBirth;
}
public function setDateBirth(?\DateTimeInterface $dateBirth): self
{
$this->dateBirth = $dateBirth;
return $this;
}
public function getDateLimited(): ?\DateTimeInterface
{
return $this->dateLimited;
}
public function setDateLimited(?\DateTimeInterface $dateLimited): self
{
$this->dateLimited = $dateLimited;
return $this;
}
public function isIsOld(): ?bool
{
return $this->isOld;
}
public function setIsOld(?bool $isOld): self
{
$this->isOld = $isOld;
return $this;
}
}