<?php
namespace App\Entity;
use App\Entity\Tag\HasAuthToken;
use App\Entity\Tag\IsBlameable;
use App\Entity\Traits\AuthTokenTrait;
use App\Entity\Traits\UserFields;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Table]
#[ORM\Index(name: 'auth_token', columns: ['auth_token'])]
#[ORM\Index(name: 'username', columns: ['username'])]
#[ORM\Index(name: 'created_by', columns: ['created_by'])]
#[ORM\Index(name: 'updated_by', columns: ['updated_by'])]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
class User implements UserInterface, HasAuthToken, IsBlameable
{
use AuthTokenTrait;
use UserFields;
use BlameableEntity;
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
private $username;
#[ORM\Column(type: 'json')]
private $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column(type: 'string')]
private $password;
#[ORM\Column(type: 'string', length: 255)]
private $email;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\ManyToMany(targetEntity: Company::class, mappedBy: 'members')]
private $companies;
#[ORM\OneToMany(targetEntity: AnalysisServiceWeight::class, mappedBy: 'user', orphanRemoval: true)]
private $analysisServiceWeights;
public function __construct()
{
$this->companies = new ArrayCollection();
$this->analysisServiceWeights = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
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;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return Collection|Company[]
*/
public function getCompanies(): Collection
{
return $this->companies;
}
public function addCompany(Company $company): self
{
if (!$this->companies->contains($company)) {
$this->companies[] = $company;
$company->addMember($this);
}
return $this;
}
public function removeCompany(Company $company): self
{
if ($this->companies->contains($company)) {
$this->companies->removeElement($company);
$company->removeMember($this);
}
return $this;
}
public function getIsVerified(): ?bool
{
return $this->isVerified;
}
/**
* @return Collection|AnalysisServiceWeight[]
*/
public function getAnalysisServiceWeights(): Collection
{
return $this->analysisServiceWeights;
}
public function addAnalysisServiceWeight(AnalysisServiceWeight $analysisServiceWeight): self
{
if (!$this->analysisServiceWeights->contains($analysisServiceWeight)) {
$this->analysisServiceWeights[] = $analysisServiceWeight;
$analysisServiceWeight->setUser($this);
}
return $this;
}
public function removeAnalysisServiceWeight(AnalysisServiceWeight $analysisServiceWeight): self
{
if ($this->analysisServiceWeights->contains($analysisServiceWeight)) {
$this->analysisServiceWeights->removeElement($analysisServiceWeight);
// set the owning side to null (unless already changed)
if ($analysisServiceWeight->getUser() === $this) {
$analysisServiceWeight->setUser(null);
}
}
return $this;
}
}