← Code-Übersicht

ExtractInvoiceDataUseCase.php

Pfad: src/Application/Rechnung/ExtractInvoiceDataUseCase.php

Ext: php

Größe: 1664 Bytes

Geändert: 2026-07-16 16:57:10+02

<?php
declare(strict_types=1);

namespace Demo\Application\Rechnung;

use Demo\Domain\Rechnung\ClockPort;
use Demo\Domain\Rechnung\ExtractedInvoiceData;
use Demo\Domain\Rechnung\InvoiceErrorCode;
use Demo\Domain\Rechnung\InvoiceLoggerPort;
use Demo\Domain\Rechnung\InvoiceRecord;
use Demo\Domain\Rechnung\TextExtractionPort;

/**
 * Extrahiert eine Rechnung und erzwingt fachliche Mindestqualitaet.
 */
final class ExtractInvoiceDataUseCase
{
    /** Injiziert Extraktor, Logger und Uhr. */
    public function __construct(private readonly TextExtractionPort $extractor, private readonly InvoiceLoggerPort $logger, private readonly ClockPort $clock)
    {
    }

    /** Extrahiert und validiert alle buchungsrelevanten Felder. */
    public function execute(string $runId, InvoiceRecord $record): ExtractedInvoiceData
    {
        if ($record->originalPfad === null || !is_file($record->originalPfad)) {
            throw new InvoiceProcessException(InvoiceErrorCode::EXTRACT, 'extract', 'Original-PDF fehlt');
        }
        $this->logger->logStep($runId, 'extract', 'START', ['sourceId' => $record->quelleId, 'at' => $this->clock->now()]);
        $data = $this->extractor->extract($record->originalPfad);
        if (!$data->hasRequiredFields() || !$data->hasPlausibleAmount(0.0001) || $data->extractionConfidence < 0.75) {
            throw new InvoiceProcessException(InvoiceErrorCode::EXTRACT, 'extract', 'Extraktion ist unvollstaendig, unplausibel oder unsicher');
        }
        $this->logger->logStep($runId, 'extract', 'DONE', ['invoiceNumber' => $data->invoiceNumber, 'confidence' => $data->extractionConfidence]);
        return $data;
    }
}

Frühere Versionen

Version Zeitpunkt Operation
15 2026-07-16 19:01:40.810908+02 UPDATE