recordEvent(new CompteCreated( userId: $user->id, email: (string) $user->email, role: $user->role->value, tenantId: $user->tenantId, occurredOn: $createdAt, )); return $user; } /** * Activates the account with the hashed password. * * @throws CompteNonActivableException if the account cannot be activated */ public function activer( string $hashedPassword, DateTimeImmutable $at, ConsentementParentalPolicy $consentementPolicy, ): void { if (!$this->statut->peutActiver()) { throw CompteNonActivableException::carStatutIncompatible($this->id, $this->statut); } // Check if parental consent is required if ($consentementPolicy->estRequis($this->dateNaissance)) { if ($this->consentementParental === null) { throw CompteNonActivableException::carConsentementManquant($this->id); } } $this->hashedPassword = $hashedPassword; $this->statut = StatutCompte::ACTIF; $this->activatedAt = $at; $this->recordEvent(new CompteActive( userId: (string) $this->id, email: (string) $this->email, tenantId: $this->tenantId, role: $this->role->value, occurredOn: $at, aggregateId: $this->id->value, )); } /** * Records the parental consent given by the parent. */ public function enregistrerConsentementParental(ConsentementParental $consentement): void { $this->consentementParental = $consentement; // If the account was awaiting consent, move to awaiting activation if ($this->statut === StatutCompte::CONSENTEMENT_REQUIS) { $this->statut = StatutCompte::EN_ATTENTE; } } /** * Checks if this user is a minor and requires parental consent. */ public function necessiteConsentementParental(ConsentementParentalPolicy $policy): bool { return $policy->estRequis($this->dateNaissance); } /** * Checks if the account is active and can log in. */ public function peutSeConnecter(): bool { return $this->statut->peutSeConnecter(); } /** * Changes the user's password. * * Used during password reset. */ public function changerMotDePasse(string $hashedPassword, DateTimeImmutable $at): void { $this->hashedPassword = $hashedPassword; $this->recordEvent(new MotDePasseChange( userId: (string) $this->id, email: (string) $this->email, tenantId: $this->tenantId, occurredOn: $at, )); } /** * Reconstitutes a User from storage. * * @internal For Infrastructure use only */ public static function reconstitute( UserId $id, Email $email, Role $role, TenantId $tenantId, string $schoolName, StatutCompte $statut, ?DateTimeImmutable $dateNaissance, DateTimeImmutable $createdAt, ?string $hashedPassword, ?DateTimeImmutable $activatedAt, ?ConsentementParental $consentementParental, ): self { $user = new self( id: $id, email: $email, role: $role, tenantId: $tenantId, schoolName: $schoolName, statut: $statut, dateNaissance: $dateNaissance, createdAt: $createdAt, ); $user->hashedPassword = $hashedPassword; $user->activatedAt = $activatedAt; $user->consentementParental = $consentementParental; return $user; } }