<?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 Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
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)
* @ORM\Table(name="`user`")
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface, BlameableInterface, TimestampableInterface
{
use TimestampableTrait;
use BlameableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @var $firstName string
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @var $lastName string
* @ORM\Column(type="string", length=255)
*/
private $lastName;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
private $role;
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @var int $contractualWeeklyHour
* @ORM\Column(type="integer", options={"default":39})
*/
private int $contractualWeeklyHour = 39;
/**
* @var $plainPassword string|null
*/
private $plainPassword;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\Column(type="boolean", options={"default":true})
*/
private $isActive = true;
/**
* @ORM\OneToMany(targetEntity="Log", mappedBy="user")
*/
private Collection $logs;
public function __construct() {
$this->logs = new ArrayCollection();
}
public static $ALL_ROLES = [
'ROLE_ADMIN',
'ROLE_CP',
'ROLE_USER',
];
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;
}
/**
* @return string
*/
public function getFirstName(): string
{
return $this->firstName;
}
/**
* @param string $firstName
* @return User
*/
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string
*/
public function getLastName(): string
{
return $this->lastName;
}
/**
* @param string $lastName
* @return User
*/
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
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(){
return $this->roles;
}
/**
* @param null $roles
* @return $this
*/
public function setRoles($roles = null){
$this->roles = !empty($roles) ? $roles : ['ROLE_USER'];
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 isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return string|null
*/
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
/**
* @param string|null $plainPassword
* @return User
*/
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* @return mixed
*/
public function getRole()
{
if (empty($this->roles)) {
$role = null;
} else {
$role = $this->roles[0];
}
return $role;
}
/**
* @param null $role
* @return User
*/
public function setRole($role = null): User
{
if (!empty($role) && !is_array($role))
$role = [$role];
return $this->setRoles($role);
}
/**
* @return mixed
*/
public function getLogs(): Collection
{
return $this->logs;
}
/**
* @param Log $log
* @return $this
*/
public function addLog(Log $log): User
{
if(!$this->logs->contains($log)){
$this->logs->add($log);
}
return $this;
}
/**
* @param Log $log
* @return $this
*/
public function removeLog(Log $log): User
{
if($this->logs->contains($log)){
$this->logs->removeElement($log);
}
return $this;
}
/**
* @return string
*/
public function getFullName(): string
{
return strtoupper($this->getLastName()).' '.$this->getFirstName();
}
public function __toString()
{
return $this->getFullName();
}
/**
* @return mixed
*/
public function getContractualWeeklyHour(): int
{
return $this->contractualWeeklyHour;
}
/**
* @param mixed $contractualWeeklyHour
* @return User
*/
public function setContractualWeeklyHour(int $contractualWeeklyHour): self
{
$this->contractualWeeklyHour = $contractualWeeklyHour;
return $this;
}
/**
* @return bool
*/
public function isActive(): bool
{
return $this->isActive;
}
/**
* @param bool $isActive
* @return User
*/
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
}