<?php
namespace App\Entity\User;
use App\Entity\Application;
use App\Entity\AvailableDay;
use App\Entity\PrimaryIdTrait;
use App\Entity\Security\ServiceSession;
use App\Entity\SMS;
use App\Entity\Survey\Survey;
use App\Entity\Survey\SurveyNotification;
use App\Entity\Technic;
use App\Entity\Vacation;
use App\Repository\User\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Table(name: '`user`')]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const ROUTE_NAME_CREATE = null;
public const ROUTE_NAME_EDIT = 'USER_EDIT';
public const ROUTE_NAME_DETAIL = 'USER_DETAIL';
public const ROUTE_NAME_DELETE = 'USER_DELETE';
public const DEPARTMENT_BED = 0;
public const DEPARTMENT_FED = 1;
public const DEPARTMENT_QA = 2;
public const DEPARTMENT_CM = 3;
public const DEPARTMENT_AM = 4;
public const DEPARTMENT_DES = 5;
public const DEPARTMENT_BACK_OFFICE = 6;
public const DEPARTMENT_HR = 7;
public const DEPARTMENT_CEO = 8;
public const DEPARTMENTS = [
self::DEPARTMENT_BED => 'user.department.bed',
self::DEPARTMENT_FED => 'user.department.fed',
self::DEPARTMENT_QA => 'user.department.qa',
self::DEPARTMENT_CM => 'user.department.cm',
self::DEPARTMENT_AM => 'user.department.am',
self::DEPARTMENT_DES => 'user.department.des',
self::DEPARTMENT_BACK_OFFICE => 'user.department.backOffice',
self::DEPARTMENT_HR => 'user.department.hr',
self::DEPARTMENT_CEO => 'user.department.ceo'
];
public const POSITION_JUNIOR = 0;
public const POSITION_MIDDLE = 1;
public const POSITION_SENIOR = 2;
public const POSITION_GROUP_HEAD = 3;
public const POSITION_TEAM_LEAD = 4;
public const POSITIONS = [
self::POSITION_JUNIOR => 'user.position.junior',
self::POSITION_MIDDLE => 'user.position.middle',
self::POSITION_SENIOR => 'user.position.senior',
self::POSITION_GROUP_HEAD => 'user.position.groupHead',
self::POSITION_TEAM_LEAD => 'user.position.teamLead'
];
public const DEPARTMENT_ROLES = [
self::DEPARTMENT_BED => ['ROLE_BED'],
self::DEPARTMENT_FED => ['ROLE_FED'],
self::DEPARTMENT_QA => ['ROLE_QA'],
self::DEPARTMENT_CM => ['ROLE_CM'],
self::DEPARTMENT_AM => ['ROLE_AM'],
self::DEPARTMENT_DES => ['ROLE_DES'],
self::DEPARTMENT_BACK_OFFICE => ['ROLE_BACK_OFFICE'],
self::DEPARTMENT_HR => ['ROLE_HR'],
self::DEPARTMENT_CEO => ['ROLE_CEO']
];
public const POSITION_ROLES = [
self::POSITION_JUNIOR => ['ROLE_JUNIOR'],
self::POSITION_MIDDLE => ['ROLE_MIDDLE'],
self::POSITION_SENIOR => ['ROLE_SENIOR'],
self::POSITION_GROUP_HEAD => ['ROLE_GROUP_HEAD'],
self::POSITION_TEAM_LEAD => ['ROLE_TEAM_LEAD']
];
use PrimaryIdTrait;
use TimestampableEntity;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['poll'])]
private ?string $firstname;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['poll'])]
private ?string $lastname;
#[ORM\Column(type: 'string', length: 255)]
private string $login;
#[ORM\Column(type: 'json')]
private $roles = [];
#[ORM\Column(type: 'string', length: 255)]
private string $email;
#[ORM\Column(type: 'string', length: 255)]
private string $password = '';
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['poll'])]
private ?string $avatar;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $slackNick;
#[ORM\Column(type: 'integer', nullable: true)]
private ?int $position;
#[ORM\Column(type: 'integer', nullable: true)]
private ?int $department;
#[ORM\OneToMany(targetEntity: 'App\Entity\Survey\Survey', mappedBy: 'author', cascade: ['remove'])]
#[ORM\OrderBy(['createdAt' => 'DESC'])]
public $survey;
#[ORM\OneToMany(targetEntity: 'App\Entity\Survey\SurveyNotification', mappedBy: 'user', cascade: ['remove'])]
public $surveyNotification;
#[ORM\Column(type: 'integer')]
private int $status = 0;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $slackId;
#[ORM\OneToMany(targetEntity: Vacation::class, mappedBy: 'user', cascade: ['remove'])]
private $vacations;
#[ORM\OneToMany(targetEntity: Application::class, mappedBy: 'user')]
private $applications;
#[ORM\OneToMany(targetEntity: AvailableDay::class, mappedBy: 'user', cascade: ['remove'])]
private $availableDays;
#[ORM\OneToMany(targetEntity: SMS::class, mappedBy: 'user')]
private $sms;
#[ORM\OneToMany(targetEntity: ServiceSession::class, mappedBy: 'userId', cascade: ['remove'])]
private $serviceSession;
#[ORM\Column(name: 'last_activity_at', type: 'datetime', nullable: true)]
protected ?\Datetime $lastActivityAt = null;
#[ORM\OneToMany(targetEntity: Technic::class, mappedBy: 'user')]
private $technic;
public function __construct()
{
$this->vacations = new ArrayCollection();
$this->applications = new ArrayCollection();
$this->sms = new ArrayCollection();
$this->survey = new ArrayCollection();
$this->surveyNotification = new ArrayCollection();
$this->technic = new ArrayCollection();
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(?int $position): void
{
$this->position = $position;
}
public function getDepartment(): ?int
{
return $this->department;
}
public function setDepartment(?int $department): void
{
$this->department = $department;
}
public function getSlackNick(): ?string
{
return $this->slackNick;
}
public function setSlackNick(?string $slackNick): self
{
$this->slackNick = $slackNick;
return $this;
}
public function getSlackId(): ?string
{
return $this->slackId;
}
public function setSlackId(?string $slackId): self
{
$this->slackId = $slackId;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
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 getUsername(): ?string
{
return $this->login;
}
public function setLogin(string $login): self
{
$this->login = $login;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
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;
}
/**
* @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;
}
public function hasRoles(string $role){
if (in_array($role, $this->getRoles(), true)) {
return true;
}
return false;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
/**
* @return Collection|Vacation[]
*/
public function getVacations(): Collection
{
return $this->vacations;
}
public function getAvailableDays(): AvailableDay
{
return $this->availableDays;
}
public function addVacation(Vacation $vacation): self
{
if (!$this->vacations->contains($vacation)) {
$this->vacations[] = $vacation;
$vacation->setUser($this);
}
return $this;
}
public function removeVacation(Vacation $vacation): self
{
if ($this->vacations->removeElement($vacation)) {
// set the owning side to null (unless already changed)
if ($vacation->getUser() === $this) {
$vacation->setUser(null);
}
}
return $this;
}
public function getSms(): Collection
{
return $this->sms;
}
public function addSms(SMS $sms): void
{
$sms->setUser($this);
$this->sms->add($sms);
}
public function removeSms(SMS $sms): void
{
if ($this->sms->contains($sms)) {
$this->sms->removeElement($sms);
}
}
/**
* @return Collection|Survey[]
*/
public function getSurvey(): Collection
{
return $this->survey;
}
/**
* @return Collection|SurveyNotification[]
*/
public function getSurveyNotification(): Collection
{
return $this->surveyNotification;
}
/**
* @param \Datetime $lastActivityAt
*/
public function setLastActivityAt($lastActivityAt)
{
$this->lastActivityAt = $lastActivityAt;
}
/**
* @return \Datetime
*/
public function getLastActivityAt()
{
return $this->lastActivityAt;
}
/**
* @return Bool Whether the user is active or not
*/
public function isActiveNow()
{
$delay = new \DateTime('2 minutes ago');
return ( $this->getLastActivityAt() > $delay );
}
public function getStatus(): int
{
return $this->status;
}
public function setStatus(int $status): void
{
$this->status = $status;
}
/**
* @return Collection<int, Technic>
*/
public function getTechnic(): Collection
{
return $this->technic;
}
public function addTechnic(Technic $technic): self
{
if (!$this->technic->contains($technic)) {
$this->technic[] = $technic;
$technic->setUser($this);
}
return $this;
}
public function removeTechnic(Technic $technic): self
{
if ($this->technic->removeElement($technic)) {
// set the owning side to null (unless already changed)
if ($technic->getUser() === $this) {
$technic->setUser(null);
}
}
return $this;
}
public function getFullName():string
{
return sprintf('%s %s', $this->firstname, $this->lastname);
}
}