← Code-Übersicht

LexwareRuntimeConfig.php

Pfad: src/Infrastructure/Rechnung/LexwareRuntimeConfig.php

Ext: php

Größe: 1540 Bytes

Geändert: 2026-07-16 17:37:59+02

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Rechnung;

use RuntimeException;

final class LexwareRuntimeConfig
{
    /** @param array<string,mixed> $paths */
    public function __construct(
        public readonly string $baseUrl,
        public readonly string $apiKey,
        public readonly int $rateLimitPerSecond,
        public readonly array $paths,
    ) {
    }

    public static function fromFile(string $file): self
    {
        $data = JsonRuntimeConfig::load($file, 'lexoffice.json', [
            'baseUrl' => 'https://api.lexware.io',
            'rateLimitPerSecond' => 2,
        ]);
        $apiKey = JsonRuntimeConfig::string($data, 'apiKey', ['api-key', 'api_key']);
        if ($apiKey === '') {
            throw new RuntimeException('lexoffice.json: apiKey ist erforderlich');
        }
        $paths = [];
        $configuredPaths = $data['paths'] ?? [];
        if (is_array($configuredPaths)) {
            foreach ($configuredPaths as $key => $value) {
                if (is_string($key)) { $paths[$key] = $value; }
            }
        }
        $category = JsonRuntimeConfig::environment('LEXWARE_POSTING_CATEGORY_ID');
        if ($category !== null) { $paths['postingCategoryId'] = $category; }
        return new self(
            JsonRuntimeConfig::string($data, 'baseUrl', ['url_base', 'api_base_url'], 'https://api.lexware.io'),
            $apiKey,
            max(1, (int) JsonRuntimeConfig::string($data, 'rateLimitPerSecond', [], '2')),
            $paths,
        );
    }
}