← Code-Übersicht

WetellRuntimeConfig.php

Pfad: src/Infrastructure/Rechnung/WetellRuntimeConfig.php

Ext: php

Größe: 3187 Bytes

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

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Rechnung;

use RuntimeException;

final class WetellRuntimeConfig
{
    public function __construct(
        public readonly string $loginUrl,
        public readonly string $invoicesUrl,
        public readonly string $username,
        public readonly string $password,
        public readonly string $usernameField,
        public readonly string $passwordField,
        public readonly string $userAgent,
        public readonly string $listSelector,
        public readonly string $pdftotextBinary,
        public readonly string $tesseractBinary,
        public readonly float $minimumConfidence,
        public readonly ?string $headlessEndpoint,
        public readonly bool $headlessPrimary,
        public readonly string $headlessSecret,
    ) {
    }

    public static function fromFile(string $file): self
    {
        $data = JsonRuntimeConfig::load($file, 'wetell.json', [
            'loginUrl' => 'https://mein.wetell.de/anmelden',
            'invoicesUrl' => 'https://mein.wetell.de/deine-rechnungen',
        ]);
        $username = JsonRuntimeConfig::string($data, 'username');
        $password = JsonRuntimeConfig::string($data, 'password');
        if ($username === '' || $password === '') {
            throw new RuntimeException('wetell.json: username und password sind erforderlich');
        }
        $endpoint = JsonRuntimeConfig::environment('RECHNUNGEN_HEADLESS_ENDPOINT')
            ?? JsonRuntimeConfig::environment('WETELL_HEADLESS_ENDPOINT')
            ?? JsonRuntimeConfig::string($data, 'headlessEndpoint', ['headless_endpoint']);
        $endpoint = $endpoint === '' ? null : $endpoint;
        $secret = JsonRuntimeConfig::string($data, 'headlessSecret');
        if ($endpoint !== null && $secret === '') {
            throw new RuntimeException('Headless-Endpoint erfordert ein Dienst-Secret');
        }
        $primaryEnv = JsonRuntimeConfig::environment('RECHNUNGEN_HEADLESS_PRIMARY');
        $primary = $primaryEnv === null
            ? JsonRuntimeConfig::bool($data['headlessUseAsPrimary'] ?? $data['headless_use_as_primary'] ?? false)
            : JsonRuntimeConfig::bool($primaryEnv);
        return new self(
            JsonRuntimeConfig::string($data, 'loginUrl', ['url_login', 'urlLogin'], 'https://mein.wetell.de/anmelden'),
            JsonRuntimeConfig::string($data, 'invoicesUrl', ['url_rechnungen', 'urlInvoices'], 'https://mein.wetell.de/deine-rechnungen'),
            $username,
            $password,
            JsonRuntimeConfig::string($data, 'usernameField', [], 'username'),
            JsonRuntimeConfig::string($data, 'passwordField', [], 'password'),
            JsonRuntimeConfig::string($data, 'userAgent', [], 'DemoBot-Rechnung/1.0'),
            JsonRuntimeConfig::string($data, 'listSelector'),
            JsonRuntimeConfig::string($data, 'pdftotextBinary', [], 'pdftotext'),
            JsonRuntimeConfig::string($data, 'tesseractBinary', [], 'tesseract'),
            (float) JsonRuntimeConfig::string($data, 'textExtractorMinConfidence', [], '0.75'),
            $endpoint,
            $primary,
            $secret,
        );
    }
}