src/Entity/User.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\UserRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Uid\Uuid;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. #[ORM\Entity(repositoryClassUserRepository::class)]
  15. #[ORM\Table(name'`user`')]
  16. #[ApiResource]
  17. #[UniqueEntity(
  18.     fields: ['email'],
  19.     message'Cet e-mail est déjà utilisé. Veuillez en essayer un autre.'
  20. )]
  21. #[UniqueEntity(fields: ['username'], message'This username is already taken.')]
  22. class User implements UserInterfacePasswordAuthenticatedUserInterface
  23. {
  24.     #[ORM\Id]
  25.     #[ORM\GeneratedValue]
  26.     #[ORM\Column]
  27.     private ?int $id null;
  28.     #[ORM\Column(length180uniquetrue)]
  29.     private ?string $email null;
  30.     #[ORM\Column(length180nullabletrue)]
  31.     private ?string $username null;
  32.     #[ORM\Column(length255)]
  33.     private ?string $nom null;
  34.     #[ORM\Column(length255)]
  35.     private ?string $prenom null;
  36.     #[ORM\Column(length255,nullabletrue)]
  37.     private ?string $telephone1 null;
  38.     #[ORM\Column(length400nullabletrue)]
  39.     private ?string $adresse null;
  40.     #[ORM\ManyToOne(targetEntityEntreprise::class, inversedBy'employes')]
  41.     #[ORM\JoinColumn(nullabletrue)]
  42.     #[ORM\JoinColumn(onDelete'SET NULL')]
  43.     private $entreprise;
  44.     #[ORM\OneToMany(mappedBy'employe'targetEntityPointage::class)]
  45.     private $pointages;
  46.     #[ORM\Column]
  47.     private array $roles = ['ROLE_USER'];
  48.    
  49.     #[ORM\Column(type'string'length255)]
  50.    // #[Assert\NotBlank]
  51.     //#[Assert\Length(min: 6, max: 4096)]
  52.     private ?string $password null;
  53.     // Ce champ est uniquement utilisé pour la confirmation en mémoire
  54.     #[Assert\NotBlank(groups: ['registration'])]
  55.     #[Assert\EqualTo(propertyPath'password'message'Les mots de passe doivent correspondre.')]
  56.     private ?string $plainPassword null;
  57.     
  58.     #[ORM\Column(nullabletrue)]
  59.     private ?int $credits null;
  60.     #[ORM\Column(type'string'nullabletrue)]
  61.     private ?string $resetToken null;
  62.     #[ORM\Column(type'datetime'nullabletrue)]
  63.     private ?\DateTimeInterface $resetTokenExpiresAt null;
  64.     #[ORM\OneToMany(mappedBy'userCredit'targetEntityCredits::class)]
  65.     private Collection $creditsTransaction;
  66.     
  67.     #[ORM\OneToMany(mappedBy'userabonnee'targetEntityAbonnement::class, cascade: ['remove'])]
  68.        private Collection $abonnements;
  69.     #[ORM\Column(type"boolean"options: ["default" => false])]
  70.     private bool $isVerified false;
  71.     #[ORM\Column(nullabletrue)]
  72.     private ?int $pointfidelite 0;
  73.     
  74.     #[ORM\Column(type'string'length255nullabletrue)]
  75.     private ?string $confirmationToken null;
  76.     
  77.     #[ORM\OneToMany(mappedBy'users'targetEntityLivraisons::class)]
  78.     private Collection $livraisons;
  79.     #[ORM\OneToMany(mappedBy'usersfinancement'targetEntityFinancement::class)]
  80.     private Collection $financements;
  81.     private ?TypeAbonnement $typeabonnement null;
  82.       // Getters and setters
  83.       public function getIsVerified(): bool
  84.       {
  85.           return $this->isVerified;
  86.       }
  87.   
  88.       public function setIsVerified(bool $isVerified): self
  89.       {
  90.           $this->isVerified $isVerified;
  91.   
  92.           return $this;
  93.       }
  94.  public function getConfirmationToken(): ?string
  95.                                                                          {
  96.                                                                              return $this->confirmationToken;
  97.                                                                          }
  98.     public function setConfirmationToken(?string $confirmationToken): self
  99.     {
  100.         $this->confirmationToken $confirmationToken;
  101.         return $this;
  102.     }
  103.    
  104.     public function getResetToken(): ?string
  105.     {
  106.         return $this->resetToken;
  107.     }
  108.     public function setResetToken(?string $resetToken): self
  109.     {
  110.         $this->resetToken $resetToken;
  111.         return $this;
  112.     }
  113.     public function getResetTokenExpiresAt(): ?\DateTimeInterface
  114.     {
  115.         return $this->resetTokenExpiresAt;
  116.     }
  117.     public function setResetTokenExpiresAt(?\DateTimeInterface $expiresAt): self
  118.     {
  119.         $this->resetTokenExpiresAt $expiresAt;
  120.         return $this;
  121.     }
  122.     public function __construct()
  123.     {
  124.         $this->livraisons = new ArrayCollection();
  125.         $this->creditsTransaction = new ArrayCollection();
  126.         $this->abonnements = new ArrayCollection();
  127.         $this->financements = new ArrayCollection();
  128.         $this->pointages = new ArrayCollection();
  129.     }
  130.     public function getId(): ?int
  131.     {
  132.         return $this->id;
  133.     }
  134.     public function getEmail(): ?string
  135.     {
  136.         return $this->email;
  137.     }
  138.     public function setEmail(string $email): self
  139.     {
  140.         $this->email $email;
  141.         return $this;
  142.     }
  143.    
  144.     /**
  145.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  146.      */
  147.     public function getUsername(): string
  148.     {
  149.         return (string) $this->username;
  150.     }
  151.     public function getRoles(): array
  152.     {
  153.         // Assure que ROLE_USER est toujours présent
  154.         if (!in_array('ROLE_USER'$this->rolestrue)) {
  155.             $this->roles[] = 'ROLE_USER';
  156.         }
  157.         return array_unique($this->roles);
  158.     }
  159.     public function setRoles(array $roles): self
  160.     {
  161.         $this->roles $roles;
  162.         return $this;
  163.     }
  164.     /**
  165.      * @see PasswordAuthenticatedUserInterface
  166.      */
  167.     public function getPassword(): string
  168.     {
  169.         return $this->password;
  170.     }
  171.     public function setPassword(string $password): self
  172.     {
  173.         $this->password $password;
  174.         return $this;
  175.     }
  176.     /**
  177.      * Returning a salt is only needed, if you are not using a modern
  178.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  179.      *
  180.      * @see UserInterface
  181.      */
  182.     public function getSalt(): ?string
  183.     {
  184.         return null;
  185.     }
  186.     /**
  187.      * @see UserInterface
  188.      */
  189.     public function eraseCredentials(): void
  190.     {
  191.         // If you store any temporary, sensitive data on the user, clear it here
  192.         // $this->plainPassword = null;
  193.     }
  194.     public function setUsername(string $username)
  195.     {
  196.         $this->username $username;
  197.         return $this;
  198.     }
  199.     public function getNom(): ?string
  200.     {
  201.         return $this->nom;
  202.     }
  203.     public function setNom(string $nom)
  204.     {
  205.         $this->nom $nom;
  206.         return $this;
  207.     }
  208.     public function getPrenom(): ?string
  209.     {
  210.         return $this->prenom;
  211.     }
  212.     public function setPrenom(string $prenom)
  213.     {
  214.         $this->prenom $prenom;
  215.         return $this;
  216.     }
  217.     public function getAdresse(): ?string
  218.     {
  219.         return $this->adresse;
  220.     }
  221.     public function setAdresse(string $adresse)
  222.     {
  223.         $this->adresse $adresse;
  224.         return $this;
  225.     }
  226.     /**
  227.      * @return Collection<int, Livraisons>
  228.      */
  229.     public function getLivraisons(): Collection
  230.     {
  231.         return $this->livraisons;
  232.     }
  233.     public function addLivraison(Livraisons $livraison)
  234.     {
  235.         if (!$this->livraisons->contains($livraison)) {
  236.             $this->livraisons->add($livraison);
  237.             $livraison->setIdClient($this);
  238.         }
  239.         return $this;
  240.     }
  241.     public function removeLivraison(Livraisons $livraison)
  242.     {
  243.         if ($this->livraisons->removeElement($livraison)) {
  244.             // set the owning side to null (unless already changed)
  245.             if ($livraison->getIdClient() === $this) {
  246.                 $livraison->setIdClient(null);
  247.             }
  248.         }
  249.         return $this;
  250.     }
  251.     public function getCredits(): ?int
  252.     {
  253.         return $this->credits;
  254.     }
  255.     public function setCredits(?int $credits)
  256.     {
  257.         $this->credits $credits;
  258.         return $this;
  259.     }
  260.     public function __toString(): string
  261.     {
  262.         return $this->prenom.' '.$this->nom// Return a string representation
  263.     }
  264.     public function getTelephone1(): ?string
  265.     {
  266.         return $this->telephone1;
  267.     }
  268.     public function setTelephone1(string $telephone1): self
  269.     {
  270.         $this->telephone1 $telephone1;
  271.         return $this;
  272.     }
  273.    
  274.     /**
  275.      * @return Collection<int, Credits>
  276.      */
  277.     public function getCreditsTransaction(): Collection
  278.     {
  279.         return $this->creditsTransaction;
  280.     }
  281.     public function addCreditsTransaction(Credits $creditsTransaction): static
  282.     {
  283.         if (!$this->creditsTransaction->contains($creditsTransaction)) {
  284.             $this->creditsTransaction->add($creditsTransaction);
  285.             $creditsTransaction->setUserCredit($this);
  286.         }
  287.         return $this;
  288.     }
  289.     public function removeCreditsTransaction(Credits $creditsTransaction): static
  290.     {
  291.         if ($this->creditsTransaction->removeElement($creditsTransaction)) {
  292.             // set the owning side to null (unless already changed)
  293.             if ($creditsTransaction->getUserCredit() === $this) {
  294.                 $creditsTransaction->setUserCredit(null);
  295.             }
  296.         }
  297.         return $this;
  298.     }
  299.     /**
  300.      * @return Collection<int, Abonnement>
  301.      */
  302.     public function getAbonnements(): Collection
  303.     {
  304.         return $this->abonnements;
  305.     }
  306.     public function addAbonnement(Abonnement $abonnement): static
  307.     {
  308.         if (!$this->abonnements->contains($abonnement)) {
  309.             $this->abonnements->add($abonnement);
  310.             $abonnement->setUserabonnee($this);
  311.         }
  312.         return $this;
  313.     }
  314.     public function removeAbonnement(Abonnement $abonnement): static
  315.     {
  316.         if ($this->abonnements->removeElement($abonnement)) {
  317.             // set the owning side to null (unless already changed)
  318.             if ($abonnement->getUserabonnee() === $this) {
  319.                 $abonnement->setUserabonnee(null);
  320.             }
  321.         }
  322.         return $this;
  323.     }
  324.     public function isIsVerified(): ?bool
  325.     {
  326.         return $this->isVerified;
  327.     }
  328.     public function getPointfidelite(): ?int
  329.     {
  330.         return $this->pointfidelite;
  331.     }
  332.     public function setPointfidelite(?int $pointfidelite): static
  333.     {
  334.         $this->pointfidelite $pointfidelite;
  335.         return $this;
  336.     }
  337.     
  338.     public function isOkAbonnement(): bool
  339.     {
  340.         // Retrieve the user's subscription
  341.         $abonnement $this->getAbonnements();
  342.         if (!$abonnement) {
  343.             return false// No subscription found
  344.         }
  345.         // Check if the subscription has expired
  346.         $now = new \DateTime();
  347.         foreach ($abonnement as $subscription) {
  348.           //  $dateResiliation = $subscription->getDateResiliation();
  349.             // Faites quelque chose avec $dateResiliation
  350.         
  351.             if ($subscription->getStatutabonnement() == 'ongoing') {
  352.                 $typeabonnement=$subscription->getTypeabonnement();
  353.                 break;
  354.             }
  355.              }
  356.              
  357.              if (!isset($typeabonnement) || ($subscription->getDateresiliation() < new \DateTimeImmutable())) {
  358.                 return false;
  359.             }
  360.             
  361.     
  362.            if (in_array($typeabonnement->getId(), [123]) 
  363.            && $subscription->getNombrelivraison() === $typeabonnement->getCaracteristiquestypeabonnement()->getNbreliv()) {
  364.            return false;
  365.        }
  366.        
  367.    
  368.    
  369.         return true// Subscription is valid
  370.     }
  371.     public function getActiveAbonnement(): ?Abonnement
  372.     {
  373.         $now = new \DateTimeImmutable();
  374.     
  375.         foreach ($this->abonnements as $abonnement) {
  376.             if ($abonnement->getDateResiliation() > $now ) {
  377.                 return $abonnement;
  378.             }
  379.         }
  380.     
  381.         return null// Aucun abonnement actif
  382.     }
  383.     public function getNombreEmployes():  int
  384. {
  385.     
  386.     // Récupérer le nombre total (plus performant que count($users))
  387.     $count $userRepository->createQueryBuilder('u')
  388.         ->select('COUNT(u.id)')
  389.         ->join('u.entreprise''e')
  390.         ->where('e.createdBy = :creator')
  391.         ->setParameter('creator'$this->getUser())
  392.         ->getQuery()
  393.         ->getSingleScalarResult();
  394.     return (int)$count;
  395.     
  396. }
  397.     public function getUserIdentifier(): string
  398.     {
  399.         return $this->email ?? $this->username// Retourne d'abord l'email, sinon le username
  400.     }
  401.   
  402.     public function getEntreprise(): ?Entreprise
  403.     {
  404.         return $this->entreprise;
  405.     }
  406.     public function setEntreprise(?Entreprise $entreprise): static
  407.     {
  408.         $this->entreprise $entreprise;
  409.         return $this;
  410.     }
  411.     /**
  412.      * @return Collection<int, Pointage>
  413.      */
  414.     public function getPointages(): Collection
  415.     {
  416.         return $this->pointages;
  417.     }
  418.     public function addPointage(Pointage $pointage): static
  419.     {
  420.         if (!$this->pointages->contains($pointage)) {
  421.             $this->pointages->add($pointage);
  422.             $pointage->setEmploye($this);
  423.         }
  424.         return $this;
  425.     }
  426.     public function removePointage(Pointage $pointage): static
  427.     {
  428.         if ($this->pointages->removeElement($pointage)) {
  429.             // set the owning side to null (unless already changed)
  430.             if ($pointage->getEmploye() === $this) {
  431.                 $pointage->setEmploye(null);
  432.             }
  433.         }
  434.         return $this;
  435.     }
  436.     public function getTypeabonnement(): ?TypeAbonnement
  437.     {
  438.         return $this->typeabonnement;
  439.     }
  440.     public function setTypeabonnement(?TypeAbonnement $typeabonnement): static
  441.     {
  442.         $this->typeabonnement $typeabonnement;
  443.         return $this;
  444.     }
  445.     public function getFinancement(): ?Financement
  446.     {
  447.         return $this->financement;
  448.     }
  449.     public function setFinancement(?Financement $financement): static
  450.     {
  451.         $this->financement $financement;
  452.         return $this;
  453.     }
  454.     /**
  455.      * @return Collection<int, Financement>
  456.      */
  457.     public function getFinancements(): Collection
  458.     {
  459.         return $this->financements;
  460.     }
  461.     public function addFinancement(Financement $financement): static
  462.     {
  463.         if (!$this->financements->contains($financement)) {
  464.             $this->financements->add($financement);
  465.             $financement->setUsersfinancement($this);
  466.         }
  467.         return $this;
  468.     }
  469.     public function removeFinancement(Financement $financement): static
  470.     {
  471.         if ($this->financements->removeElement($financement)) {
  472.             // set the owning side to null (unless already changed)
  473.             if ($financement->getUsersfinancement() === $this) {
  474.                 $financement->setUsersfinancement(null);
  475.             }
  476.         }
  477.         return $this;
  478.     }
  479.     // src/Entity/User.php
  480. public function getNomComplet(): string
  481. {
  482.     return $this->prenom ' ' $this->nom;
  483. }
  484. }