{
"tool_response": {
"type": "update",
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/Config\/CredentialService.php",
"content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Config;\n\nuse RuntimeException;\n\n\/**\n * Centralized credential management.\n *\n * Reads credentials from environment variables (.env file).\n *\/\nfinal class CredentialService\n{\n \/**\n * Gets the MariaDB root password.\n *\/\n public static function getMariaDbPassword(): string\n {\n return self::getRequired('MARIADB_ROOT_PASSWORD');\n }\n\n \/**\n * Gets the Anthropic API key.\n *\/\n public static function getAnthropicApiKey(): string\n {\n return self::getRequired('ANTHROPIC_API_KEY');\n }\n\n \/**\n * Gets the Ollama host URL.\n *\/\n public static function getOllamaHost(): string\n {\n return self::get('OLLAMA_HOST', 'http:\/\/localhost:11434');\n }\n\n \/**\n * Gets the Qdrant host URL.\n *\/\n public static function getQdrantHost(): string\n {\n return self::get('QDRANT_HOST', 'http:\/\/localhost:6333');\n }\n\n \/**\n * Gets an environment variable with a default value.\n *\/\n public static function get(string $key, string $default = ''): string\n {\n return $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key) ?: $default;\n }\n\n \/**\n * Gets a required environment variable.\n *\n * @throws RuntimeException If the variable is not set\n *\/\n public static function getRequired(string $key): string\n {\n $value = self::get($key);\n\n if ($value === '') {\n throw new RuntimeException(\"Required environment variable not set: {$key}\");\n }\n\n return $value;\n }\n}\n",
"structuredPatch": [
{
"oldStart": 9,
"oldLines": 21,
"newStart": 9,
"newLines": 16,
"lines": [
" \/**",
" * Centralized credential management.",
" *",
"- * Reads credentials from the secure credentials file and provides",
"- * typed accessors for different services.",
"+ * Reads credentials from environment variables (.env file).",
" *\/",
" 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);",
"+ return self::getRequired('MARIADB_ROOT_PASSWORD');",
" }",
" ",
" \/**"
]
},
{
"oldStart": 31,
"oldLines": 7,
"newStart": 26,
"newLines": 7,
"lines": [
" *\/",
" public static function getAnthropicApiKey(): string",
" {",
"- return self::findCredential('Anthropic', 'API', 3);",
"+ return self::getRequired('ANTHROPIC_API_KEY');",
" }",
" ",
" \/**"
]
},
{
"oldStart": 39,
"oldLines": 9,
"newStart": 34,
"newLines": 7,
"lines": [
" *\/",
" public static function getOllamaHost(): string",
" {",
"- $host = self::findCredential('Ollama', 'Host', 3);",
"-",
"- return $host !== '' ? $host : 'http:\/\/localhost:11434';",
"+ return self::get('OLLAMA_HOST', 'http:\/\/localhost:11434');",
" }",
" ",
" \/**"
]
},
{
"oldStart": 49,
"oldLines": 65,
"newStart": 42,
"newLines": 30,
"lines": [
" *\/",
" public static function getQdrantHost(): string",
" {",
"- $host = self::findCredential('Qdrant', 'Host', 3);",
"-",
"- return $host !== '' ? $host : 'http:\/\/localhost:6333';",
"+ return self::get('QDRANT_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",
"+ * Gets an environment variable with a default value.",
" *\/",
"- public static function findCredential(string $keyword1, string $keyword2, int $valueIndex): string",
"+ public static function get(string $key, string $default = ''): 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 '';",
"+ return $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key) ?: $default;",
" }",
" ",
" \/**",
"- * Clears the credential cache.",
"- *\/",
"- public static function clearCache(): void",
"- {",
"- self::$cache = null;",
"- }",
"-",
"- \/**",
"- * Loads and caches the credentials file.",
"+ * Gets a required environment variable.",
" *",
"- * @return array<string>",
"+ * @throws RuntimeException If the variable is not set",
" *\/",
"- private static function loadCredentials(): array",
"+ public static function getRequired(string $key): string",
" {",
"- if (self::$cache !== null) {",
"- return self::$cache;",
"- }",
"+ $value = self::get($key);",
" ",
"- if (!file_exists(self::CREDENTIALS_FILE)) {",
"- throw new RuntimeException('Credentials file not found: ' . self::CREDENTIALS_FILE);",
"+ if ($value === '') {",
"+ throw new RuntimeException(\"Required environment variable not set: {$key}\");",
" }",
" ",
"- $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;",
"+ return $value;",
" }",
" }"
]
}
],
"originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Config;\n\nuse RuntimeException;\n\n\/**\n * Centralized credential management.\n *\n * Reads credentials from the secure credentials file and provides\n * typed accessors for different services.\n *\/\nfinal class CredentialService\n{\n private const string CREDENTIALS_FILE = '\/var\/www\/docs\/credentials\/credentials.md';\n\n private static ?array $cache = null;\n\n \/**\n * Gets the MariaDB root password.\n *\/\n public static function getMariaDbPassword(): string\n {\n return self::findCredential('MariaDB', 'root', 3);\n }\n\n \/**\n * Gets the Anthropic API key.\n *\/\n public static function getAnthropicApiKey(): string\n {\n return self::findCredential('Anthropic', 'API', 3);\n }\n\n \/**\n * Gets the Ollama host URL.\n *\/\n public static function getOllamaHost(): string\n {\n $host = self::findCredential('Ollama', 'Host', 3);\n\n return $host !== '' ? $host : 'http:\/\/localhost:11434';\n }\n\n \/**\n * Gets the Qdrant host URL.\n *\/\n public static function getQdrantHost(): string\n {\n $host = self::findCredential('Qdrant', 'Host', 3);\n\n return $host !== '' ? $host : 'http:\/\/localhost:6333';\n }\n\n \/**\n * Finds a credential by searching for matching keywords.\n *\n * @param string $keyword1 First keyword to match in the line\n * @param string $keyword2 Second keyword to match in the line\n * @param int $valueIndex Column index (0-based) containing the value\n *\/\n public static function findCredential(string $keyword1, string $keyword2, int $valueIndex): string\n {\n $lines = self::loadCredentials();\n\n foreach ($lines as $line) {\n if (str_contains($line, $keyword1) && str_contains($line, $keyword2)) {\n $parts = explode('|', $line);\n if (count($parts) > $valueIndex) {\n return trim($parts[$valueIndex]);\n }\n }\n }\n\n return '';\n }\n\n \/**\n * Clears the credential cache.\n *\/\n public static function clearCache(): void\n {\n self::$cache = null;\n }\n\n \/**\n * Loads and caches the credentials file.\n *\n * @return array<string>\n *\/\n private static function loadCredentials(): array\n {\n if (self::$cache !== null) {\n return self::$cache;\n }\n\n if (!file_exists(self::CREDENTIALS_FILE)) {\n throw new RuntimeException('Credentials file not found: ' . self::CREDENTIALS_FILE);\n }\n\n $content = file_get_contents(self::CREDENTIALS_FILE);\n\n if ($content === false) {\n throw new RuntimeException('Failed to read credentials file');\n }\n\n self::$cache = explode(\"\\n\", $content);\n\n return self::$cache;\n }\n}\n"
}
}