key = Uuid::uuid4()->toString(); $this->expirationDate = $expirationDate; $this->enabled = true; $this->roles = new ArrayCollection(); foreach ($roleDefinitions as $roleDefinition) { $this->registerRole($roleDefinition); } } public function getExpirationDate(): ?Chronos { return $this->expirationDate; } public function isExpired(): bool { return $this->expirationDate !== null && $this->expirationDate->lt(Chronos::now()); } public function isEnabled(): bool { return $this->enabled; } public function disable(): self { $this->enabled = false; return $this; } /** * Tells if this api key is enabled and not expired */ public function isValid(): bool { return $this->isEnabled() && ! $this->isExpired(); } public function __toString(): string { return $this->key; } public function toString(): string { return $this->key; } public function spec(bool $inlined = false): Specification { $specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role, $inlined))->getValues(); return Spec::andX(...$specs); } public function isAdmin(): bool { return $this->roles->isEmpty(); } public function hasRole(string $roleName): bool { return $this->roles->containsKey($roleName); } public function getRoleMeta(string $roleName): array { /** @var ApiKeyRole|null $role */ $role = $this->roles->get($roleName); return $role === null ? [] : $role->meta(); } public function registerRole(RoleDefinition $roleDefinition): void { $roleName = $roleDefinition->roleName(); $meta = $roleDefinition->meta(); if ($this->hasRole($roleName)) { /** @var ApiKeyRole $role */ $role = $this->roles->get($roleName); $role->updateMeta($meta); } else { $role = new ApiKeyRole($roleDefinition->roleName(), $roleDefinition->meta(), $this); $this->roles[$roleName] = $role; } } public function removeRole(string $roleName): void { $this->roles->remove($roleName); } }