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(repositoryClass: UserRepository::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 UserInterface, PasswordAuthenticatedUserInterface
  23. {
  24. #[ORM\Id]
  25. #[ORM\GeneratedValue]
  26. #[ORM\Column]
  27. private ?int $id = null;
  28. #[ORM\Column(length: 180, unique: true)]
  29. private ?string $email = null;
  30. #[ORM\Column(length: 180, unique: true)]
  31. private ?string $username = null;
  32. #[ORM\Column(length: 255)]
  33. private ?string $nom = null;
  34. #[ORM\Column(length: 255)]
  35. private ?string $prenom = null;
  36. #[ORM\Column(length: 255)]
  37. private ?string $telephone1 ;
  38. #[ORM\Column(length: 400)]
  39. private ?string $adresse = null;
  40. #[ORM\Column]
  41. private array $roles = ['ROLE_USER'];
  42. /**
  43. * @var string The hashed password
  44. */
  45. //#[ORM\Column]
  46. //private ?string $password = null;
  47. #[ORM\Column(type: 'string', length: 255)]
  48. #[Assert\NotBlank]
  49. #[Assert\Length(min: 6, max: 4096)]
  50. private ?string $password = null;
  51. // Ce champ est uniquement utilisé pour la confirmation en mémoire
  52. #[Assert\NotBlank]
  53. #[Assert\EqualTo(propertyPath: 'password', message: 'Les mots de passe doivent correspondre.')]
  54. private ?string $plainPassword = null;
  55. #[ORM\Column(nullable: true)]
  56. private ?int $credits = null;
  57. #[ORM\Column(type: 'string', nullable: true)]
  58. private ?string $resetToken = null;
  59. #[ORM\Column(type: 'datetime', nullable: true)]
  60. private ?\DateTimeInterface $resetTokenExpiresAt = null;
  61. #[ORM\OneToMany(mappedBy: 'userCredit', targetEntity: Credits::class)]
  62. private Collection $creditsTransaction;
  63. #[ORM\ManyToOne(cascade: ['persist'])]
  64. private ?TypeAbonnement $typeabonnement = null;
  65. #[ORM\OneToMany(mappedBy: 'userabonnee', targetEntity: Abonnement::class, cascade: ['remove'])]
  66. private Collection $abonnements;
  67. #[ORM\Column(type: "boolean", options: ["default" => false])]
  68. private bool $isVerified = false;
  69. #[ORM\Column(nullable: true)]
  70. private ?int $pointfidelite = 0;
  71. #[ORM\ManyToOne(inversedBy: 'users')]
  72. private ?Financement $financement = null;
  73. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  74. private ?string $confirmationToken = null;
  75. #[ORM\ManyToOne(inversedBy: 'users')]
  76. private ?PaymentMethod $paymentMethod = null;
  77. #[ORM\OneToMany(mappedBy: 'users', targetEntity: Livraisons::class)]
  78. private Collection $livraisons;
  79. #[ORM\OneToMany(mappedBy: 'usersfinancement', targetEntity: Financement::class)]
  80. private Collection $financements;
  81. // Getters and setters
  82. public function getIsVerified(): bool
  83. {
  84. return $this->isVerified;
  85. }
  86. public function setIsVerified(bool $isVerified): self
  87. {
  88. $this->isVerified = $isVerified;
  89. return $this;
  90. }
  91. public function getConfirmationToken(): ?string
  92. {
  93. return $this->confirmationToken;
  94. }
  95. public function setConfirmationToken(?string $confirmationToken): self
  96. {
  97. $this->confirmationToken = $confirmationToken;
  98. return $this;
  99. }
  100. public function getResetToken(): ?string
  101. {
  102. return $this->resetToken;
  103. }
  104. public function setResetToken(?string $resetToken): self
  105. {
  106. $this->resetToken = $resetToken;
  107. return $this;
  108. }
  109. public function getResetTokenExpiresAt(): ?\DateTimeInterface
  110. {
  111. return $this->resetTokenExpiresAt;
  112. }
  113. public function setResetTokenExpiresAt(?\DateTimeInterface $expiresAt): self
  114. {
  115. $this->resetTokenExpiresAt = $expiresAt;
  116. return $this;
  117. }
  118. public function __construct()
  119. {
  120. $this->livraisons = new ArrayCollection();
  121. $this->creditsTransaction = new ArrayCollection();
  122. $this->abonnements = new ArrayCollection();
  123. $this->financements = new ArrayCollection();
  124. }
  125. public function getId(): ?int
  126. {
  127. return $this->id;
  128. }
  129. public function getEmail(): ?string
  130. {
  131. return $this->email;
  132. }
  133. public function setEmail(string $email): self
  134. {
  135. $this->email = $email;
  136. return $this;
  137. }
  138. /**
  139. * @deprecated since Symfony 5.3, use getUserIdentifier instead
  140. */
  141. public function getUsername(): string
  142. {
  143. return (string) $this->username;
  144. }
  145. public function getRoles(): array
  146. {
  147. // Assure que ROLE_USER est toujours présent
  148. if (!in_array('ROLE_USER', $this->roles, true)) {
  149. $this->roles[] = 'ROLE_USER';
  150. }
  151. return array_unique($this->roles);
  152. }
  153. public function setRoles(array $roles): self
  154. {
  155. $this->roles = $roles;
  156. return $this;
  157. }
  158. /**
  159. * @see PasswordAuthenticatedUserInterface
  160. */
  161. public function getPassword(): string
  162. {
  163. return $this->password;
  164. }
  165. public function setPassword(string $password): self
  166. {
  167. $this->password = $password;
  168. return $this;
  169. }
  170. /**
  171. * Returning a salt is only needed, if you are not using a modern
  172. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  173. *
  174. * @see UserInterface
  175. */
  176. public function getSalt(): ?string
  177. {
  178. return null;
  179. }
  180. /**
  181. * @see UserInterface
  182. */
  183. public function eraseCredentials(): void
  184. {
  185. // If you store any temporary, sensitive data on the user, clear it here
  186. // $this->plainPassword = null;
  187. }
  188. public function setUsername(string $username)
  189. {
  190. $this->username = $username;
  191. return $this;
  192. }
  193. public function getNom(): ?string
  194. {
  195. return $this->nom;
  196. }
  197. public function setNom(string $nom)
  198. {
  199. $this->nom = $nom;
  200. return $this;
  201. }
  202. public function getPrenom(): ?string
  203. {
  204. return $this->prenom;
  205. }
  206. public function setPrenom(string $prenom)
  207. {
  208. $this->prenom = $prenom;
  209. return $this;
  210. }
  211. public function getAdresse(): ?string
  212. {
  213. return $this->adresse;
  214. }
  215. public function setAdresse(string $adresse)
  216. {
  217. $this->adresse = $adresse;
  218. return $this;
  219. }
  220. /**
  221. * @return Collection<int, Livraisons>
  222. */
  223. public function getLivraisons(): Collection
  224. {
  225. return $this->livraisons;
  226. }
  227. public function addLivraison(Livraisons $livraison)
  228. {
  229. if (!$this->livraisons->contains($livraison)) {
  230. $this->livraisons->add($livraison);
  231. $livraison->setIdClient($this);
  232. }
  233. return $this;
  234. }
  235. public function removeLivraison(Livraisons $livraison)
  236. {
  237. if ($this->livraisons->removeElement($livraison)) {
  238. // set the owning side to null (unless already changed)
  239. if ($livraison->getIdClient() === $this) {
  240. $livraison->setIdClient(null);
  241. }
  242. }
  243. return $this;
  244. }
  245. public function getCredits(): ?int
  246. {
  247. return $this->credits;
  248. }
  249. public function setCredits(?int $credits)
  250. {
  251. $this->credits = $credits;
  252. return $this;
  253. }
  254. public function __toString(): string
  255. {
  256. return $this->prenom.' '.$this->nom; // Return a string representation
  257. }
  258. public function getTelephone1(): ?string
  259. {
  260. return $this->telephone1;
  261. }
  262. public function setTelephone1(string $telephone1): self
  263. {
  264. $this->telephone1 = $telephone1;
  265. return $this;
  266. }
  267. /**
  268. * @return Collection<int, Credits>
  269. */
  270. public function getCreditsTransaction(): Collection
  271. {
  272. return $this->creditsTransaction;
  273. }
  274. public function addCreditsTransaction(Credits $creditsTransaction): static
  275. {
  276. if (!$this->creditsTransaction->contains($creditsTransaction)) {
  277. $this->creditsTransaction->add($creditsTransaction);
  278. $creditsTransaction->setUserCredit($this);
  279. }
  280. return $this;
  281. }
  282. public function removeCreditsTransaction(Credits $creditsTransaction): static
  283. {
  284. if ($this->creditsTransaction->removeElement($creditsTransaction)) {
  285. // set the owning side to null (unless already changed)
  286. if ($creditsTransaction->getUserCredit() === $this) {
  287. $creditsTransaction->setUserCredit(null);
  288. }
  289. }
  290. return $this;
  291. }
  292. /**
  293. * @return Collection<int, Abonnement>
  294. */
  295. public function getAbonnements(): Collection
  296. {
  297. return $this->abonnements;
  298. }
  299. public function addAbonnement(Abonnement $abonnement): static
  300. {
  301. if (!$this->abonnements->contains($abonnement)) {
  302. $this->abonnements->add($abonnement);
  303. $abonnement->setUserabonnee($this);
  304. }
  305. return $this;
  306. }
  307. public function removeAbonnement(Abonnement $abonnement): static
  308. {
  309. if ($this->abonnements->removeElement($abonnement)) {
  310. // set the owning side to null (unless already changed)
  311. if ($abonnement->getUserabonnee() === $this) {
  312. $abonnement->setUserabonnee(null);
  313. }
  314. }
  315. return $this;
  316. }
  317. public function isIsVerified(): ?bool
  318. {
  319. return $this->isVerified;
  320. }
  321. public function getPointfidelite(): ?int
  322. {
  323. return $this->pointfidelite;
  324. }
  325. public function setPointfidelite(?int $pointfidelite): static
  326. {
  327. $this->pointfidelite = $pointfidelite;
  328. return $this;
  329. }
  330. public function getTypeabonnement(): ?TypeAbonnement
  331. {
  332. return $this->typeabonnement;
  333. }
  334. public function setTypeabonnement(?TypeAbonnement $typeabonnement): static
  335. {
  336. $this->typeabonnement = $typeabonnement;
  337. return $this;
  338. }
  339. public function getFinancement(): ?Financement
  340. {
  341. return $this->financement;
  342. }
  343. public function setFinancement(?Financement $financement): static
  344. {
  345. $this->financement = $financement;
  346. return $this;
  347. }
  348. public function getPaymentMethod(): ?PaymentMethod
  349. {
  350. return $this->paymentMethod;
  351. }
  352. public function setPaymentMethod(?PaymentMethod $paymentMethod): static
  353. {
  354. $this->paymentMethod = $paymentMethod;
  355. return $this;
  356. }
  357. public function isOkAbonnement(): bool
  358. {
  359. // Retrieve the user's subscription
  360. $abonnement = $this->getAbonnements();
  361. if (!$abonnement) {
  362. return false; // No subscription found
  363. }
  364. // Check if the subscription has expired
  365. $now = new \DateTime();
  366. foreach ($abonnement as $subscription) {
  367. // $dateResiliation = $subscription->getDateResiliation();
  368. // Faites quelque chose avec $dateResiliation
  369. if ($subscription->getStatutabonnement() != 'closed') {
  370. $typeabonnement=$subscription->getTypeabonnement();
  371. break;
  372. }
  373. }
  374. //dd($subscription->getNombrelivraison());
  375. if (!isset($typeabonnement)) {
  376. return false;
  377. }
  378. if (($typeabonnement->getLibelle() === 'Ndimbal') && ($subscription->getNombrelivraison() > $typeabonnement->getCaracteristiquestypeabonnement()->getNbreliv())) {
  379. return false;
  380. }
  381. if (($typeabonnement->getLibelle() === 'Xarit') && ($subscription->getNombrelivraison() > $typeabonnement->getCaracteristiquestypeabonnement()->getNbreliv())) {
  382. return false;
  383. }
  384. if (($typeabonnement->getLibelle() === 'Teranga') && ($subscription->getNombrelivraison() > $typeabonnement->getCaracteristiquestypeabonnement()->getNbreliv())) {
  385. return false;
  386. }
  387. return true; // Subscription is valid
  388. }
  389. public function getUserIdentifier(): string
  390. {
  391. return $this->email ?? $this->username; // Retourne d'abord l'email, sinon le username
  392. }
  393. /**
  394. * @return Collection<int, Financement>
  395. */
  396. public function getFinancements(): Collection
  397. {
  398. return $this->financements;
  399. }
  400. public function addFinancement(Financement $financement): static
  401. {
  402. if (!$this->financements->contains($financement)) {
  403. $this->financements->add($financement);
  404. $financement->setUsersfinancement($this);
  405. }
  406. return $this;
  407. }
  408. public function removeFinancement(Financement $financement): static
  409. {
  410. if ($this->financements->removeElement($financement)) {
  411. // set the owning side to null (unless already changed)
  412. if ($financement->getUsersfinancement() === $this) {
  413. $financement->setUsersfinancement(null);
  414. }
  415. }
  416. return $this;
  417. }
  418. }