<?php
namespace App\Entity;
use App\Repository\UserRepository;
use DateTime;
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;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
const INSTRUCTOR = 'instructor';
const CUSTOMER = 'customer';
const ROLE_INSTRUCTOR = 'ROLE_INSTRUCTOR';
const ROLE_CUSTOMER = 'ROLE_CUSTOMER';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @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")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastName;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $subscriptionDuedate;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\OneToMany(targetEntity=Cours::class, mappedBy="author")
*/
private $cours;
/**
* @ORM\Column(type="array", nullable=true)
* [
* 0 => read later []
* 1 => last view []
* ]
*/
private $story = [];
/**
* @ORM\Column(type="boolean")
*/
private $paswwordChanged;
/**
* @ORM\OneToMany(targetEntity=ResetPassword::class, mappedBy="user")
*/
private $resetPasswords;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $adminEmailRecovery;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $readLater = [];
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->subscriptionDuedate = new \DateTime();
$this->cours = new ArrayCollection();
$this->paswwordChanged = false;
$this->resetPasswords = 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;
}
/**
* 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;
}
/**
* 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 getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getSubscriptionDuedate(): ?\DateTime
{
return $this->subscriptionDuedate;
}
public function setSubscriptionDuedate(?\DateTime $subscriptionDuedate): self
{
$this->subscriptionDuedate = $subscriptionDuedate;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getFullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function __toString()
{
return $this->getFullName();
}
public function getSubscriptionActive(): bool
{
$now = new \DateTime();
return $this->subscriptionDuedate > $now;
}
public function extendsMembershipsFromNow(int $days): self
{
$this->subscriptionDuedate->modify("+" . $days . " days");
return $this;
}
/**
* @return Collection<int, Cours>
*/
public function getCours(): Collection
{
return $this->cours;
}
public function addCour(Cours $cour): self
{
if (!$this->cours->contains($cour)) {
$this->cours[] = $cour;
$cour->setAuthor($this);
}
return $this;
}
public function removeCour(Cours $cour): self
{
if ($this->cours->removeElement($cour)) {
// set the owning side to null (unless already changed)
if ($cour->getAuthor() === $this) {
$cour->setAuthor(null);
}
}
return $this;
}
public function getStory(): ?array
{
return $this->story;
}
public function setStory(?array $story): self
{
$this->story = $story;
return $this;
}
public function getLastViews(): array
{
if($this->story == null){
return [];
}
return array_slice($this->story, 0, 1);
}
public function getReadlerList(): array
{
if($this->readLater == null){
return [];
}
return array_slice($this->readLater, 0, 6);
}
public function addToReadLater(int $id): self
{
if($this->readLater == null){
$this->readLater = [];
}
if(in_array($id, $this->readLater)){
return $this;
}
array_unshift($this->readLater, $id);
return $this;
}
public function isViewlater(int $id): bool
{
return in_array($id, $this->readLater);
}
public function removeReadLater(int $id): void
{
if($this->readLater == null){
$this->readLater = [];
}
$this->readLater = array_filter($this->readLater, function($item) use($id){
if($item != $id){
return $item;
}
});
}
public function removeHistory(int $id): void
{
if($this->story == null){
$this->story = [];
}
$this->story = array_filter($this->story, function($item) use($id){
if($item != $id){
return $item;
}
});
}
public function addToLastView(int $id): self
{
if($this->story == null){
$this->story = [];
}
array_unshift($this->story, $id);
return $this;
}
public function isPaswwordChanged(): ?bool
{
return $this->paswwordChanged;
}
public function setPaswwordChanged(bool $paswwordChanged): self
{
$this->paswwordChanged = $paswwordChanged;
return $this;
}
/**
* @return Collection<int, ResetPassword>
*/
public function getResetPasswords(): Collection
{
return $this->resetPasswords;
}
public function addResetPassword(ResetPassword $resetPassword): self
{
if (!$this->resetPasswords->contains($resetPassword)) {
$this->resetPasswords[] = $resetPassword;
$resetPassword->setUser($this);
}
return $this;
}
public function removeResetPassword(ResetPassword $resetPassword): self
{
if ($this->resetPasswords->removeElement($resetPassword)) {
// set the owning side to null (unless already changed)
if ($resetPassword->getUser() === $this) {
$resetPassword->setUser(null);
}
}
return $this;
}
public function getAdminEmailRecovery(): ?string
{
return $this->adminEmailRecovery;
}
public function setAdminEmailRecovery(?string $adminEmailRecovery): self
{
$this->adminEmailRecovery = $adminEmailRecovery;
return $this;
}
public function getReadLater(): ?array
{
return $this->readLater;
}
public function setReadLater(?array $readLater): self
{
$this->readLater = $readLater;
return $this;
}
}