Backup #124

ID124
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Config/CredentialService.php
Version1
Typ modified
Größe2.8 KB
Hashbde0018527dbb3c1e2d75f3cc3954e0cd774286108fe6f54bf4a4a2a65552da0
Datum2025-12-20 19:30:48
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Write-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\Config;

use RuntimeException;

/**
 * Centralized credential management.
 *
 * Reads credentials from the secure credentials file and provides
 * typed accessors for different services.
 */
final class CredentialService
{
    private const string CREDENTIALS_FILE = '/var/www/docs/credentials/credentials.md';

    private static ?array $cache = null;

    /**
     * Gets the MariaDB root password.
     */
    public static function getMariaDbPassword(): string
    {
        return self::findCredential('MariaDB', 'root', 3);
    }

    /**
     * Gets the Anthropic API key.
     */
    public static function getAnthropicApiKey(): string
    {
        return self::findCredential('Anthropic', 'API', 3);
    }

    /**
     * Gets the Ollama host URL.
     */
    public static function getOllamaHost(): string
    {
        $host = self::findCredential('Ollama', 'Host', 3);

        return $host !== '' ? $host : 'http://localhost:11434';
    }

    /**
     * Gets the Qdrant host URL.
     */
    public static function getQdrantHost(): string
    {
        $host = self::findCredential('Qdrant', 'Host', 3);

        return $host !== '' ? $host : 'http://localhost:6333';
    }

    /**
     * Finds a credential by searching for matching keywords.
     *
     * @param string $keyword1 First keyword to match in the line
     * @param string $keyword2 Second keyword to match in the line
     * @param int $valueIndex Column index (0-based) containing the value
     */
    public static function findCredential(string $keyword1, string $keyword2, int $valueIndex): string
    {
        $lines = self::loadCredentials();

        foreach ($lines as $line) {
            if (str_contains($line, $keyword1) && str_contains($line, $keyword2)) {
                $parts = explode('|', $line);
                if (count($parts) > $valueIndex) {
                    return trim($parts[$valueIndex]);
                }
            }
        }

        return '';
    }

    /**
     * Clears the credential cache.
     */
    public static function clearCache(): void
    {
        self::$cache = null;
    }

    /**
     * Loads and caches the credentials file.
     *
     * @return array<string>
     */
    private static function loadCredentials(): array
    {
        if (self::$cache !== null) {
            return self::$cache;
        }

        if (!file_exists(self::CREDENTIALS_FILE)) {
            throw new RuntimeException('Credentials file not found: ' . self::CREDENTIALS_FILE);
        }

        $content = file_get_contents(self::CREDENTIALS_FILE);

        if ($content === false) {
            throw new RuntimeException('Failed to read credentials file');
        }

        self::$cache = explode("\n", $content);

        return self::$cache;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

← Zurück zur Übersicht