<?php
namespace App\Entity;
use App\Repository\UserRepository;
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\PasswordHasher\Hasher\UserPasswordHasherInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
* @ORM\HasLifecycleCallbacks
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @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, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $address;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $city;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $date_birth;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\OneToMany(targetEntity=Subscription::class, mappedBy="user")
*/
private $subscriptions;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="users")
*/
private $partner;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="partner")
*/
private $users;
private $canCode = false;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $updatedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lname;
/**
* @ORM\OneToMany(targetEntity=DossierCPF::class, mappedBy="user")
*/
private $dossierCPFs;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $data;
public function __construct()
{
$this->subscriptions = new ArrayCollection();
$this->users = new ArrayCollection();
$this->dossierCPFs = new ArrayCollection();
}
public function __toString(): string
{
return $this->email;
}
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 getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getDateBirth(): ?\DateTimeInterface
{
return $this->date_birth;
}
public function setDateBirth(?\DateTimeInterface $date_birth): self
{
$this->date_birth = $date_birth;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return Collection|Subscription[]
*/
public function getSubscriptions(): Collection
{
return $this->subscriptions;
}
public function addSubscription(Subscription $subscription): self
{
if (!$this->subscriptions->contains($subscription)) {
$this->subscriptions[] = $subscription;
$subscription->setUser($this);
}
return $this;
}
public function removeSubscription(Subscription $subscription): self
{
if ($this->subscriptions->removeElement($subscription)) {
// set the owning side to null (unless already changed)
if ($subscription->getUser() === $this) {
$subscription->setUser(null);
}
}
return $this;
}
public function getPartner(): ?self
{
return $this->partner;
}
public function setPartner(?self $partner): self
{
$this->partner = $partner;
return $this;
}
/**
* @return Collection|self[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(self $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setPartner($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->getPartner() === $this) {
$user->setPartner(null);
}
}
return $this;
}
public function canCode()//: bool
{
$canCode = false;
$excludedPacks = array(8, 37, 38);
if($this->subscriptions){
foreach($this->subscriptions as $subscription){
// if($subscription->getPack()->getCategory() == 'code' && $subscription->getStatus() == 1) $canCode = $subscription;
if($subscription->getPack()->getCategory() == 'code' && $subscription->getStatus() == 1 && !in_array($subscription->getPack()->getId(), $excludedPacks)) $canCode = $subscription;
}
}
return $canCode;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getFname(): ?string
{
return $this->fname;
}
public function setFname(?string $fname): self
{
$this->fname = $fname;
return $this;
}
public function getLname(): ?string
{
return $this->lname;
}
public function setLname(?string $lname): self
{
$this->lname = $lname;
return $this;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
$this->setUpdatedAt(new \DateTimeImmutable('now'));
if ($this->getCreatedAt() === null) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}
}
/**
* @ORM\PreUpdate
*/
public function preUpdate( $eventArgs) {
$oldPasswordValue = $eventArgs->getOldValue('password');
$newPasswordValue = $eventArgs->getNewValue('password');
if($newPasswordValue == '' || $newPasswordValue == -1){
$this->setPassword($oldPasswordValue);
}
}
/**
* @return Collection|DossierCPF[]
*/
public function getDossierCPFs(): Collection
{
return $this->dossierCPFs;
}
public function addDossierCPF(DossierCPF $dossierCPF): self
{
if (!$this->dossierCPFs->contains($dossierCPF)) {
$this->dossierCPFs[] = $dossierCPF;
$dossierCPF->setUser($this);
}
return $this;
}
public function removeDossierCPF(DossierCPF $dossierCPF): self
{
if ($this->dossierCPFs->removeElement($dossierCPF)) {
// set the owning side to null (unless already changed)
if ($dossierCPF->getUser() === $this) {
$dossierCPF->setUser(null);
}
}
return $this;
}
public function isPartner(): bool
{
return in_array('ROLE_PARTNER', $this->roles);
}
public function getData(): array
{
return $this->data ? json_decode($this->data, true) : array();
}
public function setData(?string $data): self
{
$this->data = $data;
return $this;
}
}