← Code-Übersicht

InvoiceApplicationFactory.php

Pfad: src/Infrastructure/Rechnung/InvoiceApplicationFactory.php

Ext: php

Größe: 2693 Bytes

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

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Rechnung;

use Demo\Application\Rechnung\DiscoverInvoicesUseCase;
use Demo\Application\Rechnung\DownloadInvoiceUseCase;
use Demo\Application\Rechnung\ExtractInvoiceDataUseCase;
use Demo\Application\Rechnung\IngestInvoicesUseCase;
use Demo\Application\Rechnung\PostInvoiceUseCase;
use Demo\Application\Rechnung\ResolveVendorUseCase;
use Demo\Domain\Rechnung\WetellSourcePort;

final class InvoiceApplicationFactory
{
    public function create(
        InvoiceRuntimeConfig $runtime,
        WetellRuntimeConfig $wetell,
        LexwareRuntimeConfig $lexware,
    ): IngestInvoicesUseCase {
        $logger = new JsonLineLogger($runtime->logPath);
        $clock = new SystemClock();
        $source = $this->source($wetell, $logger);
        $extractor = new FallbackTextExtractor(
            new PdftotextExtractor($wetell->pdftotextBinary),
            new TesseractInvoiceExtractor($wetell->tesseractBinary),
            $wetell->minimumConfidence,
        );
        $lexwarePort = new LexofficeApiAdapter(
            $lexware->baseUrl,
            $lexware->apiKey,
            $lexware->rateLimitPerSecond,
            $lexware->paths,
        );
        return new IngestInvoicesUseCase(
            new JsonInvoiceStateAdapter($runtime->statePath),
            new DiscoverInvoicesUseCase($source, $logger),
            new DownloadInvoiceUseCase($source, new DateBasedInvoiceStorage($runtime->originalsPath), $logger, $clock),
            new ExtractInvoiceDataUseCase($extractor, $logger, $clock),
            new ResolveVendorUseCase($lexwarePort, $logger),
            new PostInvoiceUseCase($lexwarePort, $logger),
            $logger,
            $clock,
            $runtime->maxRetries,
            $runtime->amountTolerance,
        );
    }

    private function source(WetellRuntimeConfig $config, JsonLineLogger $logger): WetellSourcePort
    {
        $http = new HttpWetellSourceAdapter(
            $config->loginUrl,
            $config->invoicesUrl,
            $config->listSelector,
            $config->usernameField,
            $config->passwordField,
            $logger,
            $config->userAgent,
        );
        if ($config->headlessEndpoint === null) {
            return $http;
        }
        $headless = new HeadlessWetellSourceAdapter(
            $config->headlessEndpoint,
            $config->username,
            $config->password,
            $config->headlessSecret,
        );
        return $config->headlessPrimary
            ? new FallbackWetellSourceAdapter($headless, $http, $logger)
            : new FallbackWetellSourceAdapter($http, $headless, $logger);
    }
}