src/Entity/User.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  * @UniqueEntity(fields={"email"}, message="Il existe déjà un compte avec cette adresse e-mail.")
  13.  * @ORM\HasLifecycleCallbacks()
  14.  */
  15. class User implements UserInterfacePasswordAuthenticatedUserInterface
  16. {
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\GeneratedValue
  20.      * @ORM\Column(type="integer")
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=180, unique=true)
  25.      */
  26.     private $email;
  27.     /**
  28.      * @ORM\Column(type="json")
  29.      */
  30.     private $roles = [];
  31.     /**
  32.      * @var string The hashed password
  33.      * @ORM\Column(type="string")
  34.      */
  35.     private $password;
  36.     /**
  37.      * @ORM\Column(type="string", length=100)
  38.      */
  39.     private $nom;
  40.     /**
  41.      * @ORM\Column(type="string", length=100)
  42.      */
  43.     private $prenom;
  44.     /**
  45.      * @ORM\Column(type="date", nullable=true)
  46.      */
  47.     private $dateNaissance null;
  48.     /**
  49.      * @ORM\Column(type="string", length=20)
  50.      */
  51.     private $telephone;
  52.     /**
  53.      * @ORM\OneToMany(targetEntity=Trajet::class, mappedBy="conducteur")
  54.      */
  55.     private $trajets;
  56.     /**
  57.      * @ORM\OneToMany(targetEntity=Reservation::class, mappedBy="passager")
  58.      */
  59.     private $reservations;
  60.     /**
  61.      * @ORM\OneToMany(targetEntity=Message::class, mappedBy="expediteur")
  62.      */
  63.     private $messagesExpediteur;
  64.     /**
  65.      * @ORM\OneToMany(targetEntity=Message::class, mappedBy="destinataire")
  66.      */
  67.     private $messagesDestinataire;
  68.     /**
  69.      * @ORM\OneToMany(targetEntity=Notes::class, mappedBy="noteur")
  70.      */
  71.     private $notesNoteur;
  72.     /**
  73.      * @ORM\OneToMany(targetEntity=Notes::class, mappedBy="notePour")
  74.      */
  75.     private $notesPour;
  76.     /**
  77.      * @ORM\Column(type="boolean", nullable=true)
  78.      */
  79.     private $conducteurVerifie;
  80.     /**
  81.      * @ORM\Column(type="boolean")
  82.      */
  83.     private $isVerified false;
  84.     /**
  85.      * @ORM\OneToMany(targetEntity=Document::class, mappedBy="user", cascade={"persist", "remove"})
  86.      */
  87.     private $documents;
  88.     /**
  89.      * @ORM\Column(type="string", length=255, nullable=true)
  90.      */
  91.     private $photo;
  92.     /**
  93.      * @ORM\Column(type="datetime")
  94.      */
  95.     private $createdAt;
  96.     /**
  97.      * @ORM\OneToMany(targetEntity=PushSubscription::class, mappedBy="user")
  98.      */
  99.     private $pushSubscriptions;
  100.     /**
  101.      * @ORM\Column(type="text", nullable=true)
  102.      */
  103.     private $description;
  104.     /**
  105.      * @ORM\PrePersist
  106.      */
  107.     public function setCreatedAtValue(): void
  108.     {
  109.         $this->createdAt = new \DateTime();
  110.     }
  111.     /**
  112.      * @ORM\Column(type="string", length=255, nullable=true)
  113.      */
  114.     private $stripeAccountId;
  115.     /**
  116.      * @ORM\Column(type="json", nullable=true)
  117.      */
  118.     private $preferences = [];
  119.     /**
  120.      * @ORM\OneToMany(targetEntity=Notification::class, mappedBy="user", orphanRemoval=true)
  121.      */
  122.     private $notifications;
  123.     public function __construct()
  124.     {
  125.         $this->trajets = new ArrayCollection();
  126.         $this->reservations = new ArrayCollection();
  127.         $this->messages = new ArrayCollection();
  128.         $this->messagesExpediteur = new ArrayCollection();
  129.         $this->messagesDestinataire = new ArrayCollection();
  130.         $this->notesNoteur = new ArrayCollection();
  131.         $this->notesPour = new ArrayCollection();
  132.         $this->documents = new ArrayCollection();
  133.         $this->pushSubscriptions = new ArrayCollection();
  134.         $this->notifications = new ArrayCollection();
  135.     }
  136.     public function getId(): ?int
  137.     {
  138.         return $this->id;
  139.     }
  140.     public function getEmail(): ?string
  141.     {
  142.         return $this->email;
  143.     }
  144.     public function setEmail(string $email): self
  145.     {
  146.         $this->email $email;
  147.         return $this;
  148.     }
  149.     /**
  150.      * A visual identifier that represents this user.
  151.      *
  152.      * @see UserInterface
  153.      */
  154.     public function getUserIdentifier(): string
  155.     {
  156.         return (string) $this->email;
  157.     }
  158.     /**
  159.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  160.      */
  161.     public function getUsername(): string
  162.     {
  163.         return (string) $this->email;
  164.     }
  165.     /**
  166.      * @see UserInterface
  167.      */
  168.     public function getRoles(): array
  169.     {
  170.         $roles $this->roles;
  171.         // guarantee every user at least has ROLE_USER
  172.         $roles[] = 'ROLE_USER';
  173.         return array_unique($roles);
  174.     }
  175.     public function setRoles(array $roles): self
  176.     {
  177.         $this->roles $roles;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @see PasswordAuthenticatedUserInterface
  182.      */
  183.     public function getPassword(): string
  184.     {
  185.         return $this->password;
  186.     }
  187.     public function setPassword(string $password): self
  188.     {
  189.         $this->password $password;
  190.         return $this;
  191.     }
  192.     /**
  193.      * Returning a salt is only needed, if you are not using a modern
  194.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  195.      *
  196.      * @see UserInterface
  197.      */
  198.     public function getSalt(): ?string
  199.     {
  200.         return null;
  201.     }
  202.     /**
  203.      * @see UserInterface
  204.      */
  205.     public function eraseCredentials()
  206.     {
  207.         // If you store any temporary, sensitive data on the user, clear it here
  208.         // $this->plainPassword = null;
  209.     }
  210.     public function getNom(): ?string
  211.     {
  212.         return $this->nom;
  213.     }
  214.     public function setNom(string $nom): self
  215.     {
  216.         $this->nom $nom;
  217.         return $this;
  218.     }
  219.     public function getPrenom(): ?string
  220.     {
  221.         return $this->prenom;
  222.     }
  223.     public function setPrenom(string $prenom): self
  224.     {
  225.         $this->prenom $prenom;
  226.         return $this;
  227.     }
  228.     public function getDateNaissance(): ?\DateTimeInterface
  229.     {
  230.         return $this->dateNaissance;
  231.     }
  232.     public function setDateNaissance(?\DateTimeInterface $dateNaissance): self
  233.     {
  234.         $this->dateNaissance $dateNaissance;
  235.         return $this;
  236.     }
  237.     public function getTelephone(): ?string
  238.     {
  239.         return $this->telephone;
  240.     }
  241.     public function setTelephone(string $telephone): self
  242.     {
  243.         $this->telephone $telephone;
  244.         return $this;
  245.     }
  246.     /**
  247.      * @return Collection<int, Trajet>
  248.      */
  249.     public function getTrajets(): Collection
  250.     {
  251.         return $this->trajets;
  252.     }
  253.     public function addTrajet(Trajet $trajet): self
  254.     {
  255.         if (!$this->trajets->contains($trajet)) {
  256.             $this->trajets[] = $trajet;
  257.             $trajet->setConducteur($this);
  258.         }
  259.         return $this;
  260.     }
  261.     public function removeTrajet(Trajet $trajet): self
  262.     {
  263.         if ($this->trajets->removeElement($trajet)) {
  264.             // set the owning side to null (unless already changed)
  265.             if ($trajet->getConducteur() === $this) {
  266.                 $trajet->setConducteur(null);
  267.             }
  268.         }
  269.         return $this;
  270.     }
  271.     /**
  272.      * @return Collection<int, Reservation>
  273.      */
  274.     public function getReservations(): Collection
  275.     {
  276.         return $this->reservations;
  277.     }
  278.     public function addReservation(Reservation $reservation): self
  279.     {
  280.         if (!$this->reservations->contains($reservation)) {
  281.             $this->reservations[] = $reservation;
  282.             $reservation->setPassager($this);
  283.         }
  284.         return $this;
  285.     }
  286.     public function removeReservation(Reservation $reservation): self
  287.     {
  288.         if ($this->reservations->removeElement($reservation)) {
  289.             // set the owning side to null (unless already changed)
  290.             if ($reservation->getPassager() === $this) {
  291.                 $reservation->setPassager(null);
  292.             }
  293.         }
  294.         return $this;
  295.     }
  296.     /**
  297.      * @return Collection<int, Message>
  298.      */
  299.     public function getMessagesExpediteur(): Collection
  300.     {
  301.         return $this->messagesExpediteur;
  302.     }
  303.     public function addMessagesExpediteur(Message $messagesExpediteur): self
  304.     {
  305.         if (!$this->messagesExpediteur->contains($messagesExpediteur)) {
  306.             $this->messagesExpediteur[] = $messagesExpediteur;
  307.             $messagesExpediteur->setExpediteur($this);
  308.         }
  309.         return $this;
  310.     }
  311.     public function removeMessagesExpediteur(Message $messagesExpediteur): self
  312.     {
  313.         if ($this->messagesExpediteur->removeElement($messagesExpediteur)) {
  314.             // set the owning side to null (unless already changed)
  315.             if ($messagesExpediteur->getExpediteur() === $this) {
  316.                 $messagesExpediteur->setExpediteur(null);
  317.             }
  318.         }
  319.         return $this;
  320.     }
  321.     /**
  322.      * @return Collection<int, Message>
  323.      */
  324.     public function getMessagesDestinataire(): Collection
  325.     {
  326.         return $this->messagesDestinataire;
  327.     }
  328.     public function addMessagesDestinataire(Message $messagesDestinataire): self
  329.     {
  330.         if (!$this->messagesDestinataire->contains($messagesDestinataire)) {
  331.             $this->messagesDestinataire[] = $messagesDestinataire;
  332.             $messagesDestinataire->setDestinataire($this);
  333.         }
  334.         return $this;
  335.     }
  336.     public function removeMessagesDestinataire(Message $messagesDestinataire): self
  337.     {
  338.         if ($this->messagesDestinataire->removeElement($messagesDestinataire)) {
  339.             // set the owning side to null (unless already changed)
  340.             if ($messagesDestinataire->getDestinataire() === $this) {
  341.                 $messagesDestinataire->setDestinataire(null);
  342.             }
  343.         }
  344.         return $this;
  345.     }
  346.     /**
  347.      * @return Collection<int, Notes>
  348.      */
  349.     public function getNotesNoteur(): Collection
  350.     {
  351.         return $this->notesNoteur;
  352.     }
  353.     public function addNotesNoteur(Notes $notesNoteur): self
  354.     {
  355.         if (!$this->notesNoteur->contains($notesNoteur)) {
  356.             $this->notesNoteur[] = $notesNoteur;
  357.             $notesNoteur->setNoteur($this);
  358.         }
  359.         return $this;
  360.     }
  361.     public function removeNotesNoteur(Notes $notesNoteur): self
  362.     {
  363.         if ($this->notesNoteur->removeElement($notesNoteur)) {
  364.             // set the owning side to null (unless already changed)
  365.             if ($notesNoteur->getNoteur() === $this) {
  366.                 $notesNoteur->setNoteur(null);
  367.             }
  368.         }
  369.         return $this;
  370.     }
  371.     /**
  372.      * @return Collection<int, Notes>
  373.      */
  374.     public function getNotesPour(): Collection
  375.     {
  376.         return $this->notesPour;
  377.     }
  378.     public function addNotesPour(Notes $notesPour): self
  379.     {
  380.         if (!$this->notesPour->contains($notesPour)) {
  381.             $this->notesPour[] = $notesPour;
  382.             $notesPour->setNotePour($this);
  383.         }
  384.         return $this;
  385.     }
  386.     public function removeNotesPour(Notes $notesPour): self
  387.     {
  388.         if ($this->notesPour->removeElement($notesPour)) {
  389.             // set the owning side to null (unless already changed)
  390.             if ($notesPour->getNotePour() === $this) {
  391.                 $notesPour->setNotePour(null);
  392.             }
  393.         }
  394.         return $this;
  395.     }
  396.     public function isConducteurVerifie(): ?bool
  397.     {
  398.         return $this->conducteurVerifie;
  399.     }
  400.     public function setConducteurVerifie(?bool $conducteurVerifie): self
  401.     {
  402.         $this->conducteurVerifie $conducteurVerifie;
  403.         return $this;
  404.     }
  405.     public function isVerified(): bool
  406.     {
  407.         return $this->isVerified;
  408.     }
  409.     public function setIsVerified(bool $isVerified): self
  410.     {
  411.         $this->isVerified $isVerified;
  412.         return $this;
  413.     }
  414.     /**
  415.      * @return Collection<int, Document>
  416.      */
  417.     public function getDocuments(): Collection
  418.     {
  419.         return $this->documents;
  420.     }
  421.     public function addDocument(Document $document): self
  422.     {
  423.         if (!$this->documents->contains($document)) {
  424.             $this->documents[] = $document;
  425.             $document->setUser($this);
  426.         }
  427.         return $this;
  428.     }
  429.     public function removeDocument(Document $document): self
  430.     {
  431.         if ($this->documents->removeElement($document)) {
  432.             // set the owning side to null (unless already changed)
  433.             if ($document->getUser() === $this) {
  434.                 $document->setUser(null);
  435.             }
  436.         }
  437.         return $this;
  438.     }
  439.     public function getDocumentByType(string $type): ?Document
  440.     {
  441.         foreach ($this->documents as $doc) {
  442.             if ($doc->getTypeDocument() === $type) {
  443.                 return $doc;
  444.             }
  445.         }
  446.         return null;
  447.     }
  448.     public function getPhoto(): ?string
  449.     {
  450.         return $this->photo;
  451.     }
  452.     public function setPhoto(?string $photo): self
  453.     {
  454.         $this->photo $photo;
  455.         return $this;
  456.     }
  457.     public function getCreatedAt(): ?\DateTimeInterface
  458.     {
  459.         return $this->createdAt;
  460.     }
  461.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  462.     {
  463.         $this->createdAt $createdAt;
  464.         return $this;
  465.     }
  466.     /**
  467.      * @return Collection<int, PushSubscription>
  468.      */
  469.     public function getPushSubscriptions(): Collection
  470.     {
  471.         return $this->pushSubscriptions;
  472.     }
  473.     public function addPushSubscription(PushSubscription $pushSubscription): self
  474.     {
  475.         if (!$this->pushSubscriptions->contains($pushSubscription)) {
  476.             $this->pushSubscriptions[] = $pushSubscription;
  477.             $pushSubscription->setUser($this);
  478.         }
  479.         return $this;
  480.     }
  481.     public function removePushSubscription(PushSubscription $pushSubscription): self
  482.     {
  483.         if ($this->pushSubscriptions->removeElement($pushSubscription)) {
  484.             // set the owning side to null (unless already changed)
  485.             if ($pushSubscription->getUser() === $this) {
  486.                 $pushSubscription->setUser(null);
  487.             }
  488.         }
  489.         return $this;
  490.     }
  491.     public function getDescription(): ?string
  492.     {
  493.         return $this->description;
  494.     }
  495.     public function setDescription(?string $description): self
  496.     {
  497.         $this->description $description;
  498.         return $this;
  499.     }
  500.     public function getPreferences(): array
  501.     {
  502.         return $this->preferences ?? [];
  503.     }
  504.     public function setPreferences(array $preferences): self
  505.     {
  506.         $this->preferences $preferences;
  507.         return $this;
  508.     }
  509.     public function isProfilVerifieComplet(): bool
  510.     {
  511.         $doc $this->getDocumentByType("identite");
  512.         return $this->isVerified() && $this->hasVerifiedIdentity()
  513.             // && $this->hasVerifiedPhone() // Pour plus tard
  514.         ;
  515.     }
  516.     public function hasVerifiedIdentity(): bool
  517.     {
  518.         $doc $this->getDocumentByType("identite");
  519.         return $doc && $doc->getStatus() === Document::STATUS_APPROVED;
  520.     }
  521.     public function hasVerifiedPhone(): bool
  522.     {
  523.         return false// à remplacer plus tard par vraie vérif
  524.     }
  525.     // Compte supprimé ! 
  526.     public function anonymiser(): void
  527.     {
  528.         $this->setNom('');
  529.         $this->setPrenom('Utilisateur supprimé');
  530.         $this->setDateNaissance(null);
  531.         $this->setEmail('deleted_' $this->getId() . '@halogari.yt');
  532.         $this->setTelephone(0000 $this->getId());
  533.         $this->setPassword('');
  534.         $this->setPhoto(null);
  535.         $this->setDescription(null);
  536.         $this->setIsVerified(false);
  537.     }
  538.     public function getStripeAccountId(): ?string
  539.     {
  540.         return $this->stripeAccountId;
  541.     }
  542.     public function setStripeAccountId(?string $stripeAccountId): self
  543.     {
  544.         $this->stripeAccountId $stripeAccountId;
  545.         return $this;
  546.     }
  547.     public function getAge(): ?int
  548.     {
  549.         if (!$this->dateNaissance) {
  550.             return null;
  551.         }
  552.         $today = new \DateTimeImmutable();
  553.         return $today->diff($this->dateNaissance)->y;
  554.     }
  555.     public function getNotifications(): Collection
  556.     {
  557.         return $this->notifications;
  558.     }
  559. }