← Code-Übersicht

FallbackWetellSourceAdapter.php

Pfad: src/Infrastructure/Rechnung/FallbackWetellSourceAdapter.php

Ext: php

Größe: 2171 Bytes

Geändert: 2026-07-16 18:20:06+02

<?php

declare (strict_types=1);
namespace Demo\Infrastructure\Rechnung;

use Demo\Domain\Rechnung\InvoiceLoggerPort;
use Demo\Domain\Rechnung\SourceInvoice;
use Demo\Domain\Rechnung\WetellAuthenticationException;
use Demo\Domain\Rechnung\WetellSourcePort;
use Throwable;
/**
 * Waehlt beim Login genau einen aktiven WEtell-Adapter pro Session.
 */
final class FallbackWetellSourceAdapter implements WetellSourcePort
{
    private ?WetellSourcePort $active = null;
    /** Injiziert Primary, Fallback und Logger. */
    public function __construct(private readonly WetellSourcePort $primary, private readonly WetellSourcePort $fallback, private readonly InvoiceLoggerPort $logger)
    {
    }
    /** Aktiviert Primary oder nach technischem Fehler einen frisch eingeloggten Fallback. */
    public function login(string $username, string $password): \Demo\Domain\Rechnung\WetellSessionHandle
    {
        try {
            $this->primary->login($username, $password);
            $this->active = $this->primary;
            $this->logger->logStep('portal', 'adapter', 'PRIMARY', []);
        } catch (WetellAuthenticationException $error) {
            throw $error;
        } catch (Throwable $error) {
            $this->logger->logStep('portal', 'adapter', 'FALLBACK', ['reason' => $error::class]);
            $this->fallback->login($username, $password);
            $this->active = $this->fallback;
        }
        return \Demo\Domain\Rechnung\WetellSessionHandle::issue($this);
    }
    /** @return list<SourceInvoice> Discovery ausschliesslich in aktiver Session. */
    public function discoverInvoices(): array
    {
        return $this->session()->discoverInvoices();
    }
    /** Download ausschliesslich in aktiver Session. */
    public function downloadInvoicePdf(string $downloadUrl): string
    {
        return $this->session()->downloadInvoicePdf($downloadUrl);
    }
    /** Liefert den erfolgreich eingeloggten Adapter. */
    private function session(): WetellSourcePort
    {
        if ($this->active === null) {
            throw new \RuntimeException('WEtell-Session ist nicht eingeloggt');
        }
        return $this->active;
    }
}

Frühere Versionen

Version Zeitpunkt Operation
59 2026-07-16 20:20:52.017557+02 UPDATE
53 2026-07-16 20:18:22.87213+02 UPDATE
38 2026-07-16 19:55:01.887691+02 UPDATE