<?php
namespace App\Entity;
use App\Controller\Api\UserCreation;
use App\State\UserProcessor;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
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;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Entity\Client;
use App\Entity\Docteur;
use App\Entity\Laboratoire;
use DateTime;
/**
* @ORM\Table()
* @UniqueEntity("email")
* @ORM\Entity()
*
*/
#[ApiResource(
collectionOperations: [
'get' => ['method' => 'GET'],
'post' => [
'method' => 'POST',
'controller' => UserCreation::class
/*'route_name' => 'api_user_create', */
/*'processor' => UserProcessor::class,*/
],
],
itemOperations: [
'get' => ['method' => 'GET'],
'put' => ['method' => 'PUT'],
'patch' => ['method' => 'PATCH'],
'delete' => ['method' => 'DELETE']
],
attributes: [
'pagination_client_items_per_page' => true,
'force_eager' => false,
'normalization_context' => ['groups' => ['user', 'read']],
'denormalization_context' => ['groups' => ['user', 'user-write', 'read']]
]
)]
#[ApiFilter(SearchFilter::class, properties: ["email" => "exact", "type" => "exact"])]
#[ApiFilter(BooleanFilter::class,properties: ["docteur.actif"])]
#[ApiFilter(BooleanFilter::class, properties: ["client.actif"])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
const TYPE_DOCTOR = 'docteur';
/**
* @var integer $id
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Groups({"user"})
*/
protected $id;
/**
* @ORM\Column(name="email", type="string", length=180, nullable=true)
* @Groups({"user"})
*/
protected $email;
/**
* @ORM\Column(name="salt", type="string", length=1000, nullable=true)
*/
private $salt;
/**
* @ORM\Column(name="password", type="string", length=255, nullable=true)
*/
private ?string $password = null;
/**
* @ORM\Column(name="newPassword", type="string", length=255, nullable=true)
*/
private $newPassword;
/**
* @Groups({"user-write"})
*/
protected $plainPassword;
/**
* @ORM\Column(name="firstname", type="string", length=64, nullable=true)
* @var string
* @Groups({"user"})
*/
protected $firstname;
/**
* @ORM\Column(name="lastname", type="string", length=64, nullable=true)
* @var string
* @Groups({"user"})
*/
protected $lastname;
/**
* @ORM\Column(name="phone", type="string", length=64, nullable=true)
* @var string
* @Groups({"user"})
*/
protected $phone;
/**
* @ORM\Column(name="date_of_birth", type="datetime", nullable=true)
* @var ?DateTime
* @Groups({"user"})
*/
protected $dateOfBirth;
/**
* @ORM\Column(name="enabled", type="boolean", nullable=true)
* @var bool
*/
protected $enabled;
/**
* @ORM\Column(name="gender", type="string", length=1, nullable=true)
* @var string
* @Groups({"user"})
*/
protected $gender;
/**
* @var array
*
* @ORM\Column(name="roles", type="array", nullable=false)
*/
private $roles;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=30, nullable=true)
* @Assert\NotBlank()
* @Groups({"user"})
*/
private $type;
/**
* @var Client
*
* @ORM\OneToOne(
* targetEntity=Client::class,
* cascade={"persist"},
* inversedBy="user"
* )
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
* @Groups({"user", "read"})
*/
private $client;
/**
* @var Docteur
* EN UTILISATION.
* @ORM\OneToOne(
* targetEntity="App\Entity\Docteur",
* cascade={"persist"},
* inversedBy="user"
* )
* @ORM\JoinColumn(name="docteur_id", referencedColumnName="id", onDelete="CASCADE")
* @Groups({"user"})
*/
private $docteur;
/**
* @var Laboratoire
*
* @ORM\OneToOne(
* targetEntity="App\Entity\Laboratoire",
* cascade={"persist"},
* inversedBy="user"
* )
* @ORM\JoinColumn(name="laboratoire_id", referencedColumnName="id", onDelete="CASCADE")
* @Groups({"user"})
*/
private $laboratoire;
/**
* @var string
*
* @ORM\Column(type="text", nullable=true)
* @Groups({"user"})
*/
private $photo;
/**
* @ORM\Column(name="reset_password_token", type="string", length=255, nullable=true)
*/
private $resetPasswordToken;
/**
* @ORM\Column(name="reset_password_token_expires_at", type="datetime", nullable=true)
*/
private $resetPasswordTokenExpiresAt;
/**
* @var datetime|null
*
* @ORM\Column(name="dateCreation", type="datetime", nullable=true)
*
* @Groups({"user"})
*/
private $dateCreation;
/**
* @var datetime|null
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*
* @Groups({"user"})
*/
private $createdAt;
/**
* Constructor of Docteur Entity
*/
public function __construct()
{
$this->type = 'user'; // user / client / docteur / laboratoire
$this->dateCreation = new \DateTime();
}
public function __toString(): string
{
return $this->firstname . ' ' . $this->lastname;
}
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set type
*
* @param string $type
*
* @return User
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get client
*
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* Set client
*
* @param Client $client
*
* @return User
*/
public function setClient(?Client $client)
{
$this->client = $client;
return $this;
}
/**
* Get docteur
*
* @return Docteur
*/
public function getDocteur()
{
return $this->docteur;
}
/**
* Set docteur
*
* @param Docteur $docteur
*
* @return User
*/
public function setDocteur($docteur)
{
$this->docteur = $docteur;
return $this;
}
/**
* Get laboratoire
*
* @return stdClass
*/
public function getLaboratoire()
{
return $this->laboratoire;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email): void
{
$this->email = $email;
}
/**
* @return string
*/
public function getFirstname(): string
{
return $this->firstname;
}
/**
* @param string $firstname
*/
public function setFirstname(?string $firstname): void
{
$this->firstname = $firstname;
}
/**
* @return string
*/
public function getLastname(): string
{
return $this->lastname;
}
/**
* @param string $lastname
*/
public function setLastname(?string $lastname): void
{
$this->lastname = $lastname;
}
/**
* @return string
*/
public function getPhone(): ?string
{
return $this->phone;
}
/**
* @param string $phone
*/
public function setPhone(?string $phone): void
{
$this->phone = $phone;
}
/**
* @return string|null
*/
public function getGender(): ?string
{
return $this->gender;
}
/**
* @param string $gender
*/
public function setGender(?string $gender): void
{
$this->gender = $gender;
}
/**
* Set laboratoire
*
* @param Laboratoire $laboratoire
*
* @return User
*/
public function setLaboratoire($laboratoire)
{
$this->laboratoire = $laboratoire;
return $this;
}
/**
* Set photo
*
* @param string photo
*
* @return User
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* @return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* @return mixed
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @param mixed $plainPassword
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
}
/**
* @return ?DateTime
*/
public function getDateOfBirth()
{
return $this->dateOfBirth;
}
/**
* @param DateTime $dateOfBirth
*/
public function setDateOfBirth(?DateTime $dateOfBirth): void
{
$this->dateOfBirth = $dateOfBirth;
}
/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
/**
* @inheritDoc
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* @inheritDoc
*/
public function setPassword(?string $password): self
{
if (!is_null($password)) {
$this->password = $password;
}
return $this;
}
/**
* @return mixed
*/
public function getSalt()
{
return $this->salt;
}
/**
* @param mixed $salt
*/
public function setSalt($salt): void
{
$this->salt = $salt;
}
/**
* @return mixed
*/
public function getNewPassword()
{
return $this->newPassword;
}
/**
* @param mixed $newPassword
*/
public function setNewPassword($newPassword): void
{
$this->newPassword = $newPassword;
}
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function getUsername()
{
return $this->email;
}
/**
* Set the reset password token.
*
* @param string|null $resetPasswordToken
* @return $this
*/
public function setResetPasswordToken(?string $resetPasswordToken): self
{
$this->resetPasswordToken = $resetPasswordToken;
return $this;
}
/**
* Get the reset password token.
*
* @return string|null
*/
public function getResetPasswordToken(): ?string
{
return $this->resetPasswordToken;
}
/**
* Set the reset password token expiration date.
*
* @param \DateTimeInterface|null $resetPasswordTokenExpiresAt
* @return $this
*/
public function setResetPasswordTokenExpiresAt(?\DateTimeInterface $resetPasswordTokenExpiresAt): self
{
$this->resetPasswordTokenExpiresAt = $resetPasswordTokenExpiresAt;
return $this;
}
/**
* Get the reset password token expiration date.
*
* @return \DateTimeInterface|null
*/
public function getResetPasswordTokenExpiresAt(): ?\DateTimeInterface
{
return $this->resetPasswordTokenExpiresAt;
}
/**
* Get dateCreation
*
* @return datetime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set dateCreation
*
* @param datetime $dateCreation
*
* @return User
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Check if the reset password token is valid.
*
* @return bool
*/
public function isResetPasswordTokenValid(): bool
{
// Check if the reset password token exists and has not expired
if ($this->resetPasswordToken && $this->resetPasswordTokenExpiresAt instanceof \DateTimeInterface) {
$currentDateTime = new \DateTime();
return $currentDateTime < $this->resetPasswordTokenExpiresAt;
}
return false;
}
public function eraseCredentials() : void {
}
public function getUserIdentifier() : string {
return $this->getEmail();
}
/**
* @return DateTime|null
*/
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
/**
* @param DateTime|null $createdAt
*/
public function setCreatedAt(?DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
}