208 lines
5.7 KiB
PHP
208 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\User\Framework\Entity;
|
|
|
|
use Aimeos\Map;
|
|
use App\User\Framework\Repository\PreferencesRepository;
|
|
use App\User\Framework\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;
|
|
|
|
#[ORM\Entity(repositoryClass: UserRepository::class)]
|
|
#[ORM\HasLifecycleCallbacks]
|
|
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
|
|
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column(type: 'integer')]
|
|
private int $id;
|
|
|
|
#[ORM\Column(type: 'string', length: 180, unique: true)]
|
|
private ?string $email;
|
|
|
|
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
|
private ?string $name;
|
|
|
|
#[ORM\Column(type: 'json')]
|
|
private array $roles = [];
|
|
|
|
#[ORM\Column(type: 'string')]
|
|
private string $password;
|
|
|
|
/**
|
|
* @var Collection<int, UserPreference>
|
|
*/
|
|
#[ORM\OneToMany(targetEntity: UserPreference::class, mappedBy: 'user', cascade: ['persist', 'remove'])]
|
|
private Collection $userPreferences;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->userPreferences = new ArrayCollection();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function getName(): ?string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function setName(?string $name): self
|
|
{
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* The public representation of the user (e.g. a username, an email address, etc.)
|
|
*
|
|
* @see UserInterface
|
|
*/
|
|
public function getUserIdentifier(): 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;
|
|
}
|
|
|
|
/**
|
|
* @see UserInterface
|
|
*/
|
|
public function eraseCredentials(): void
|
|
{
|
|
// If you store any temporary, sensitive data on the user, clear it here
|
|
// $this->plainPassword = null;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, UserPreference>
|
|
*/
|
|
public function getUserPreferences(): Collection
|
|
{
|
|
return $this->userPreferences;
|
|
}
|
|
|
|
public function getUserPreference(string $preferenceName)
|
|
{
|
|
foreach ($this->userPreferences as $userPreference) {
|
|
if ($userPreference->getPreference()->getName() === $preferenceName) {
|
|
return $userPreference->getPreference();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function hasUserPreference(string $preferenceName): bool
|
|
{
|
|
foreach ($this->userPreferences as $userPreference) {
|
|
if ($userPreference->getPreference()->getId() === $preferenceName) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function updateUserPreference(string $preferenceName, mixed $preferenceValue): static
|
|
{
|
|
foreach ($this->userPreferences as $userPreference) {
|
|
if ($userPreference->getPreference()->getId() === $preferenceName) {
|
|
$userPreference->setPreferenceValue($preferenceValue);
|
|
}
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
public function addUserPreference(UserPreference $userPreference): static
|
|
{
|
|
if (!$this->userPreferences->contains($userPreference)) {
|
|
$this->userPreferences->add($userPreference);
|
|
$userPreference->setUser($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeUserPreference(UserPreference $userPreference): static
|
|
{
|
|
if ($this->userPreferences->removeElement($userPreference)) {
|
|
// set the owning side to null (unless already changed)
|
|
if ($userPreference->getUser() === $this) {
|
|
$userPreference->setUser(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getUserPreferenceValues()
|
|
{
|
|
return Map::from($this->userPreferences)
|
|
->rekey(fn(UserPreference $userPreference) => $userPreference->getPreference()->getId())
|
|
->map(function (UserPreference $userPreference) {
|
|
if (in_array($userPreference->getPreference()->getId(), ['language', 'provider'])) {
|
|
return $userPreference->getPreferenceValue();
|
|
}
|
|
foreach ($userPreference->getPreference()->getPreferenceOptions() as $preferenceOption) {
|
|
// dd((int) $userPreference->getPreferenceValue(), $preferenceOption->getId(), $preferenceOption->getValue());
|
|
if ($preferenceOption->getId() === (int) $userPreference->getPreferenceValue()) {
|
|
return $preferenceOption->getValue();
|
|
}
|
|
}
|
|
return null;
|
|
})
|
|
->toArray();
|
|
}
|
|
}
|