src/Entity/User.php line 21
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\UserRepository;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: 'email', message: 'Email already taken')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 100, unique: true, options: ['collation' => 'latin1_general_ci'])]
#[Assert\NotBlank]
#[Assert\Email]
private string $email;
#[Assert\Length(min: 4)]
private ?string $plainPassword = null;
#[ORM\Column(type: 'string', length: 64)]
private ?string $password = '';
#[ORM\Column(type: 'array')]
private array $roles = ['ROLE_USER'];
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Widget::class)]
private Collection $widgets;
#[ORM\ManyToOne(targetEntity: Company::class, cascade: ['persist'], inversedBy: 'employees')]
#[ORM\JoinColumn(nullable: true, onDelete: 'CASCADE')]
private ?Company $company;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?DateTimeInterface $registerDate;
#[ORM\Column(type: 'string', length: 32, nullable: true)]
private ?string $confirmationToken;
#[ORM\Column(type: 'string', length: 99, nullable: true)]
private ?string $dropboxToken;
#[ORM\Column(type: 'boolean')]
private ?bool $isCompanyAdmin;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Subscription::class, cascade: ['persist'])]
private Collection $subscriptions;
#[ORM\Column(type: 'boolean')]
private ?bool $isEnabled = false;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?DateTimeInterface $lastLogin;
#[ORM\Column(type: 'integer')]
private ?int $uploadCount = 0;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $mailerSubscription;
#[ORM\Column(type: 'string', length: 2)]
private ?string $locale = 'fr';
#[ORM\ManyToMany(targetEntity: Team::class, inversedBy: 'users')]
private Collection $team;
public function __construct()
{
$this->widgets = new ArrayCollection();
$this->subscriptions = new ArrayCollection();
$this->setRegisterDate(new DateTime('now', new DateTimeZone('Europe/Paris')));
$this->setConfirmationToken(bin2hex(random_bytes(16)));
$this->team = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email): self
{
$this->email = $email;
return $this;
}
public function getUsername()
{
return $this->getEmail();
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($password): self
{
$this->plainPassword = $password;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getSalt()
{
return null;
}
public function getRoles(): array
{
return $this->roles;
}
public function getUserIdentifier(): string
{
return $this->getUsername();
}
public function setRoles(string|array $role): void
{
// Only used in EasyAdminBundle
if (is_array($role)) {
$role = $role[0];
}
$this->roles = [strtoupper($role)];
}
public function eraseCredentials()
{
}
public function getWidgets(): Collection
{
return $this->widgets;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getRegisterDate(): ?DateTimeInterface
{
return $this->registerDate;
}
public function setRegisterDate(?DateTimeInterface $registerDate): self
{
$this->registerDate = $registerDate;
return $this;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function getDropboxToken(): ?string
{
return $this->dropboxToken;
}
public function setDropboxToken(?string $dropboxToken): self
{
$this->dropboxToken = $dropboxToken;
return $this;
}
/**
* Get all uploads tags for company
*/
public function getCompanyTags(): array
{
$companyTags = [];
/** @var Widget $widget */
foreach ($this->getCompanyWidgets() as $widget) {
foreach ($widget->getUploads() as $upload) {
$companyTags = array_merge($companyTags, $upload->getTags());
}
}
return array_unique($companyTags);
}
public function __toString(): string
{
return $this->getEmail();
}
public function getCompanyWidgets()
{
$companyWidgets = [];
// If user is in company, get company widget's
if ($this->getCompany() != null) {
foreach ($this->getCompany()->getEmployees() as $employee) {
foreach ($employee->getWidgets() as $widget) {
array_push($companyWidgets, $widget);
}
}
} else {
// Else, get user widget's
foreach ($this->getWidgets() as $widget) {
// @codeCoverageIgnoreStart
array_push($companyWidgets, $widget);
// @codeCoverageIgnoreEnd
}
}
return $companyWidgets;
}
public function getIsCompanyAdmin(): ?bool
{
return $this->isCompanyAdmin;
}
public function setIsCompanyAdmin(bool $isCompanyAdmin): self
{
$this->isCompanyAdmin = $isCompanyAdmin;
return $this;
}
public function getSubscriptions(): Collection
{
return $this->subscriptions;
}
public function getActiveSubscription()
{
/** @var Subscription $subscription */
foreach ($this->getSubscriptions() as $subscription) {
if (($subscription->getIsActive() && !$subscription->getIsDelete()) || (!$subscription->getIsActive() && !$subscription->getIsDelete())) {
return $subscription;
}
}
return null;
}
public function getIsEnabled(): ?bool
{
return $this->isEnabled;
}
public function setIsEnabled(bool $isEnabled): self
{
$this->isEnabled = $isEnabled;
return $this;
}
public function getLastLogin(): ?DateTimeInterface
{
return $this->lastLogin;
}
public function setLastLogin(?DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
public function getUploadCount()
{
return $this->uploadCount;
}
public function setUploadCount(int $uploadCount): self
{
$this->uploadCount = $uploadCount;
return $this;
}
public function getMailerSubscription()
{
return $this->mailerSubscription;
}
public function setMailerSubscription(array $mailerSubscription): self
{
$this->mailerSubscription = json_encode($mailerSubscription);
return $this;
}
public function getLocale(): ?string
{
return $this->locale;
}
public function setLocale(string $locale): self
{
$this->locale = $locale;
return $this;
}
public function getTeam(): Collection
{
return $this->team;
}
public function addTeam(Team $team): self
{
if (!$this->team->contains($team)) {
$this->team[] = $team;
}
return $this;
}
public function removeTeam(Team $team): self
{
if ($this->team->contains($team)) {
$this->team->removeElement($team);
}
return $this;
}
}