← Code-Übersicht

FallbackWetellSourceAdapter.php

Pfad: src/Infrastructure/Rechnung/FallbackWetellSourceAdapter.php

Ext: php

Größe: 2327 Bytes

Geändert: 2026-07-16T14:11:22+02:00

Frühere Version vom 2026-07-16T14:11:22+02:00 · zur aktuellen Fassung

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Rechnung;

use Demo\Domain\Rechnung\InvoiceLoggerPort;
use Demo\Domain\Rechnung\SourceInvoice;
use Demo\Domain\Rechnung\WetellSourcePort;

/**
 * Schichtet Haupt-Adapter (HTTP) mit optionalem Headless-Fallback.
 */
final class FallbackWetellSourceAdapter implements WetellSourcePort
{
    /**
     * @param WetellSourcePort $primary Primäradapter.
     * @param ?WetellSourcePort $fallback Optinaler Fallback-Adapter.
     * @param InvoiceLoggerPort $logger Logging.
     */
    public function __construct(
        private WetellSourcePort $primary,
        private ?WetellSourcePort $fallback,
        private InvoiceLoggerPort $logger
    ) {
    }

    /**
     * @inheritDoc
     */
    public function login(string $username, string $password): void
    {
        try {
            $this->primary->login($username, $password);
            return;
        } catch (\Throwable $error) {
            if ($this->fallback === null) {
                throw $error;
            }
            $this->logger->logStep('system', 'wetell', 'WARN', ['action' => 'fallback_login']);
            $this->fallback->login($username, $password);
        }
    }

    /**
     * @inheritDoc
     */
    public function discoverInvoices(): array
    {
        try {
            $result = $this->primary->discoverInvoices();
            if (count($result) > 0) {
                return $result;
            }
        } catch (\Throwable $error) {
            if ($this->fallback === null) {
                throw $error;
            }
        }

        if ($this->fallback === null) {
            return [];
        }

        $this->logger->logStep('system', 'wetell', 'WARN', ['action' => 'fallback_discover']);
        return $this->fallback->discoverInvoices();
    }

    /**
     * @inheritDoc
     */
    public function downloadInvoicePdf(string $downloadUrl): string
    {
        try {
            return $this->primary->downloadInvoicePdf($downloadUrl);
        } catch (\Throwable $error) {
            if ($this->fallback === null) {
                throw $error;
            }

            $this->logger->logStep('system', 'wetell', 'WARN', ['action' => 'fallback_download']);
            return $this->fallback->downloadInvoicePdf($downloadUrl);
        }
    }
}