Skip to content
Snippets Groups Projects
User.php 18.77 KiB
<?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 Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Serializer\Annotation\Ignore;

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @UniqueEntity(fields={"email"}, message="Un compte existe déjà pour cet email")
 * @UniqueEntity(fields={"pseudo"}, message="Un compte existe déjà pour ce pseudo")
 * @ORM\HasLifecycleCallbacks
 * @Vich\Uploadable
 */
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    const LANGUAGE_LEVELS = [
        'A1' => 'A1',
        'A2' => 'A2',
        'B1' => 'B1',
        'B2' => 'B2',
    ];

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     */
    private $password;

    /**
     * @var string
     * @ORM\Column(type="string", length=100)
     * @Assert\Length(max={100})
     */
    private $pseudo;

    /**
     * @var Uuid
     * @ORM\Column(type="uuid", unique=true)
     */
    private $uuid = null;

    /**
     * @ORM\OneToMany(targetEntity=OAuth2UserConsent::class, mappedBy="user", orphanRemoval=true)
     */
    private $oAuth2UserConsents;

    /**
     * @ORM\Column(type="datetime_immutable")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime_immutable", nullable=true)
     */
    private $updatedAt;

    /**
     * @var boolean
     * @ORM\Column(type="boolean")
     */
    private $enabled = true;

    /**
     * @ORM\OneToOne(targetEntity=Lexicon::class, inversedBy="user", cascade={"persist", "remove"})
     */
    private $lexicon;

    /**
     * @var boolean
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $shareDataWithOtherUsers;

    /**
     * @var boolean
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $shareDataWithResearchers;

    /**
     * @var boolean
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $activeGamification;

    /**
     * @var boolean
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $activePersonalStats;

    /**
     * @var string
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $appLanguage;

    /**
     * @var string
     * @ORM\Column(type="string", length=100)
     */
    private $nativeLanguage;

    /**
     * @ORM\OneToMany(targetEntity=StudiedLanguage::class, mappedBy="user", cascade={"persist", "remove"})
     */
    private $studiedLanguages;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\Length(max={255})
     */
    private $description;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Picture", cascade={"persist", "remove"})
     */
    private $picture;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isVerified = false;

    /**
     * @ORM\ManyToMany(targetEntity=User::class, inversedBy="friendsWithMe")
     * @ORM\JoinTable(name="user_join_friends")
     */
    private $myFriends;

    /**
     * @ORM\ManyToMany(targetEntity=User::class, mappedBy="myFriends")
     */
    private $friendsWithMe;

    /**
     * @ORM\OneToMany(targetEntity=Group::class, mappedBy="createdBy")
     */
    private $createdGroups;

    /**
     * @ORM\OneToMany(targetEntity=GraphyList::class, mappedBy="createdBy")
     */
    private $createdGraphyLists;

    /**
     * @ORM\OneToMany(targetEntity=Label::class, mappedBy="user")
     */
    private $labels;

    /**
     * @ORM\OneToMany(targetEntity=GroupMembership::class, mappedBy="user", cascade={"remove"})
     */
    private $groupMemberships;

    /**
     * @ORM\OneToMany(targetEntity=Log::class, mappedBy="createdBy", cascade={"remove"})
     */
    private $logs;

    /**
     * @ORM\OneToMany(targetEntity=Trace::class, mappedBy="createdBy", cascade={"remove"})
     */
    private $traces;

    /**
     * @ORM\OneToMany(targetEntity=MetaInfo::class, mappedBy="createdBy")
     */
    private $metaInfos;

    public function __toString()
    {
        return $this->getPseudo();
    }

    public function __construct()
    {
        $this->oAuth2UserConsents = new ArrayCollection();
        $this->uuid = new UuidV1();
        $lexicon = new Lexicon();
        $lexicon->setUser($this);
        $lexicon->setCategory(Lexicon::TYPE_USER);
        $this->setLexicon($lexicon);
        $this->studiedLanguages = new ArrayCollection();
        $this->myFriends = new ArrayCollection();
        $this->friendsWithMe = new ArrayCollection();
        $this->createdGroups = new ArrayCollection();
        $this->groupMemberships = new ArrayCollection();
        $this->labels = new ArrayCollection();
        $this->createdGraphyLists = new ArrayCollection();
        $this->logs = new ArrayCollection();
    }

    /**
     * Retourne la valeur "language" du premier StudiedLanguage de l'user
     *
     * @return string|null
     */
    public function getFirstStudiedLanguage()
    {
        if ($this->getStudiedLanguages()->isEmpty()) {
            return null;
        } else {
            return $this->getStudiedLanguages()->first()->getLanguage();
        }
    }

    /**
     * Retourne les langues étudiées (string) par l'user dans un tableau
     *
     * @return array
     */
    public function getStudiedLanguagesValues()
    {
        $result = [];
        foreach ($this->getStudiedLanguages() as $studiedLanguage) {
            $result[] = $studiedLanguage->getLanguage();
        }
        return  $result;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps(): void
    {
        $this->setUpdatedAt(new \DateTimeImmutable('now'));
        if ($this->getCreatedAt() === null) {
            $this->setCreatedAt(new \DateTimeImmutable('now'));
        }
    }

    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;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return $this->pseudo;
    }

    /**
     * @deprecated since Symfony 5.3, use getUserIdentifier instead
     */
    public function getUsername(): 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;
    }

    /**
     * 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 getUuid()
    {
        return $this->uuid;
    }

    public function setUuid($uuid): self
    {
        $this->uuid = $uuid;

        return $this;
    }

    /**
     * @return Collection<int, OAuth2UserConsent>
     */
    public function getOAuth2UserConsents(): Collection
    {
        return $this->oAuth2UserConsents;
    }

    public function addOAuth2UserConsent(OAuth2UserConsent $oAuth2UserConsent): self
    {
        if (!$this->oAuth2UserConsents->contains($oAuth2UserConsent)) {
            $this->oAuth2UserConsents[] = $oAuth2UserConsent;
            $oAuth2UserConsent->setUser($this);
        }

        return $this;
    }

    public function removeOAuth2UserConsent(OAuth2UserConsent $oAuth2UserConsent): self
    {
        if ($this->oAuth2UserConsents->removeElement($oAuth2UserConsent)) {
            // set the owning side to null (unless already changed)
            if ($oAuth2UserConsent->getUser() === $this) {
                $oAuth2UserConsent->setUser(null);
            }
        }

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getUpdatedAt(): ?\DateTimeImmutable
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    public function getPseudo(): ?string
    {
        return $this->pseudo;
    }

    public function setPseudo(string $pseudo): self
    {
        $this->pseudo = $pseudo;

        return $this;
    }

    public function getLexicon(): ?Lexicon
    {
        return $this->lexicon;
    }

    public function setLexicon(?Lexicon $lexicon): self
    {
        // unset the owning side of the relation if necessary
        if ($lexicon === null && $this->lexicon !== null) {
            $this->lexicon->setUser(null);
        }

        // set the owning side of the relation if necessary
        if ($lexicon !== null && $lexicon->getUser() !== $this) {
            $lexicon->setUser($this);
        }

        $this->lexicon = $lexicon;

        return $this;
    }

    public function isShareDataWithOtherUsers(): ?bool
    {
        return $this->shareDataWithOtherUsers;
    }

    public function setShareDataWithOtherUsers(bool $shareDataWithOtherUsers): self
    {
        $this->shareDataWithOtherUsers = $shareDataWithOtherUsers;

        return $this;
    }

    public function isShareDataWithResearchers(): ?bool
    {
        return $this->shareDataWithResearchers;
    }

    public function setShareDataWithResearchers(bool $shareDataWithResearchers): self
    {
        $this->shareDataWithResearchers = $shareDataWithResearchers;

        return $this;
    }

    public function getAppLanguage(): ?string
    {
        return $this->appLanguage;
    }

    public function setAppLanguage(string $appLanguage): self
    {
        $this->appLanguage = $appLanguage;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getIsVerified(): ?bool
    {
        return $this->isVerified;
    }

    public function setIsVerified(bool $isVerified): self
    {
        $this->isVerified = $isVerified;

        return $this;
    }

    public function getNativeLanguage(): ?string
    {
        return $this->nativeLanguage;
    }

    public function setNativeLanguage(string $nativeLanguage): self
    {
        $this->nativeLanguage = $nativeLanguage;

        return $this;
    }

    public function isIsVerified(): ?bool
    {
        return $this->isVerified;
    }

    public function getPicture(): ?Picture
    {
        return $this->picture;
    }

    public function setPicture(?Picture $picture): self
    {
        $this->picture = $picture;

        return $this;
    }

    /**
     * @return Collection<int, StudiedLanguage>
     */
    public function getStudiedLanguages(): Collection
    {
        return $this->studiedLanguages;
    }

    public function addStudiedLanguage(StudiedLanguage $studiedLanguage): self
    {
        if (!$this->studiedLanguages->contains($studiedLanguage)) {
            $this->studiedLanguages[] = $studiedLanguage;
            $studiedLanguage->setUser($this);
        }

        return $this;
    }
    public function removeStudiedLanguage(StudiedLanguage $studiedLanguage): self
    {
        if ($this->studiedLanguages->removeElement($studiedLanguage)) {
            // set the owning side to null (unless already changed)
            if ($studiedLanguage->getUser() === $this) {
                $studiedLanguage->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, self>
     */
    public function getMyFriends(): Collection
    {
        return $this->myFriends;
    }

    public function addMyFriend(self $myFriend): self
    {
        if (!$this->myFriends->contains($myFriend)) {
            $this->myFriends[] = $myFriend;
        }

        return $this;
    }

    public function removeMyFriend(self $myFriend): self
    {
        $this->myFriends->removeElement($myFriend);

        return $this;
    }

    /**
     * @return Collection<int, self>
     */
    public function getFriendsWithMe(): Collection
    {
        return $this->friendsWithMe;
    }

    public function addFriendsWithMe(self $friendsWithMe): self
    {
        if (!$this->friendsWithMe->contains($friendsWithMe)) {
            $this->friendsWithMe[] = $friendsWithMe;
            $friendsWithMe->addMyFriend($this);
        }

        return $this;
    }

    public function removeFriendsWithMe(self $friendsWithMe): self
    {
        if ($this->friendsWithMe->removeElement($friendsWithMe)) {
            $friendsWithMe->removeMyFriend($this);
        }

        return $this;
    }

    /**
     * @return Collection<int, Group>
     */
    public function getCreatedGroups(): Collection
    {
        return $this->createdGroups;
    }

    public function addCreatedGroup(Group $createdGroup): self
    {
        if (!$this->createdGroups->contains($createdGroup)) {
            $this->createdGroups[] = $createdGroup;
            $createdGroup->setCreatedBy($this);
        }

        return $this;
    }

    public function removeCreatedGroup(Group $createdGroup): self
    {
        if ($this->createdGroups->removeElement($createdGroup)) {
            // set the owning side to null (unless already changed)
            if ($createdGroup->getCreatedBy() === $this) {
                $createdGroup->setCreatedBy(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, GroupMembership>
     */
    public function getGroupMemberships(): Collection
    {
        return $this->groupMemberships;
    }

    public function addGroupMembership(GroupMembership $groupMembership): self
    {
        if (!$this->groupMemberships->contains($groupMembership)) {
            $this->groupMemberships[] = $groupMembership;
            $groupMembership->setUser($this);
        }

        return $this;
    }

    public function removeGroupMembership(GroupMembership $groupMembership): self
    {
        if ($this->groupMemberships->removeElement($groupMembership)) {
            // set the owning side to null (unless already changed)
            if ($groupMembership->getUser() === $this) {
                $groupMembership->setUser(null);
            }
        }

        return $this;
    }

    public function isActiveGamification(): ?bool
    {
        return $this->activeGamification;
    }

    public function setActiveGamification(?bool $activeGamification): self
    {
        $this->activeGamification = $activeGamification;

        return $this;
    }

    public function isActivePersonalStats(): ?bool
    {
        return $this->activePersonalStats;
    }
    public function setActivePersonalStats(?bool $activePersonalStats): self
    {
        $this->activePersonalStats = $activePersonalStats;

        return $this;
    }

    public function isEnabled(): ?bool
    {
        return $this->enabled;
    }

    public function setEnabled(bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    /**
     * @return Collection<int, Label>
     */
    public function getLabels(): Collection
    {
        return $this->labels;
    }

    public function addLabel(Label $label): self
    {
        if (!$this->labels->contains($label)) {
            $this->labels[] = $label;
            $label->setUser($this);
        }

        return $this;
    }

    public function removeLabel(Label $label): self
    {
        if ($this->labels->removeElement($label)) {
            // set the owning side to null (unless already changed)
            if ($label->getUser() === $this) {
                $label->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, GraphyList>
     */
    public function getCreatedGraphyLists(): Collection
    {
        return $this->createdGraphyLists;
    }

    public function addCreatedGraphyList(GraphyList $createdGraphyList): self
    {
        if (!$this->createdGraphyLists->contains($createdGraphyList)) {
            $this->createdGraphyLists[] = $createdGraphyList;
            $createdGraphyList->setCreatedBy($this);
        }

        return $this;
    }

    public function removeCreatedGraphyList(GraphyList $createdGraphyList): self
    {
        if ($this->createdGraphyLists->removeElement($createdGraphyList)) {
            // set the owning side to null (unless already changed)
            if ($createdGraphyList->getCreatedBy() === $this) {
                $createdGraphyList->setCreatedBy(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Log>
     */
    public function getLogs(): Collection
    {
        return $this->logs;
    }

    public function addLog(Log $log): self
    {
        if (!$this->logs->contains($log)) {
            $this->logs[] = $log;
            $log->setCreatedBy($this);
        }

        return $this;
    }

    public function removeLog(Log $log): self
    {
        if ($this->logs->removeElement($log)) {
            // set the owning side to null (unless already changed)
            if ($log->getCreatedBy() === $this) {
                $log->setCreatedBy(null);
            }
        }

        return $this;
    }
}