Files
mareike/app/EventPaymentModules/AbstractEventPaymentModule.php
T

119 lines
4.3 KiB
PHP

<?php
namespace App\EventPaymentModules;
use App\Enumerations\PaymentStatus;
use App\EventPaymentModules\DTO\CreateInvoiceRequest;
use App\EventPaymentModules\DTO\CreateInvoiceResponse;
use App\EventPaymentModules\DTO\DoPaymentRequest;
use App\EventPaymentModules\DTO\DoPaymentResponse;
use App\EventPaymentModules\DTO\RegistrationSummaryRequest;
use App\EventPaymentModules\DTO\RegistrationSummaryResponse;
/**
* Gemeinsame Basis aller Zahlungsmodule. Hält den geteilten Code und die aus {@see getOptions()}
* abgeleiteten Options-Helfer; konkrete Module definieren nur ihre Identität, ihr Options-Schema
* und -- später -- die je Zahlungsart variierenden Teile (z.B. den Rechnungs-Schlusssatz).
*/
abstract class AbstractEventPaymentModule implements EventPaymentModule
{
public function defaultDescription(): ?string
{
return null;
}
// -----------------------------------------------------------------------------------------
// Aus getOptions() abgeleitete Helfer (zuvor statisch auf PaymentMethod).
// -----------------------------------------------------------------------------------------
/**
* Namen aller Pflicht-Optionen dieses Moduls.
*
* @return array<int, string>
*/
public function requiredOptionKeys(): array
{
return array_values(array_map(
static fn (array $option) => $option['name'],
array_filter($this->getOptions(), static fn (array $option) => $option['required'] ?? false)
));
}
/**
* Reduziert eine Konfiguration auf die im Schema definierten Options-Keys.
* Unbekannte Keys werden verworfen -- getOptions() ist die Autorität.
*
* @param array<string, mixed> $config
* @return array<string, mixed>
*/
public function sanitizeConfiguration(array $config): array
{
$result = [];
foreach ($this->getOptions() as $option) {
if (array_key_exists($option['name'], $config)) {
$result[$option['name']] = $config[$option['name']];
}
}
return $result;
}
/**
* Prüft, ob alle Pflicht-Optionen befüllt (non-empty) sind.
*
* @param array<string, mixed> $config
*/
public function isConfigurationComplete(array $config): bool
{
foreach ($this->requiredOptionKeys() as $key) {
$value = $config[$key] ?? null;
if ($value === null || $value === '' || $value === []) {
return false;
}
}
return true;
}
// -----------------------------------------------------------------------------------------
// Operative Methoden -- in dieser Iteration bewusst als Stubs.
// -----------------------------------------------------------------------------------------
public function registrationSummary(RegistrationSummaryRequest $request): RegistrationSummaryResponse
{
// TODO: In einer Folge-Iteration ausformulieren (zahlungsspezifischer Teil der Zusammenfassung).
return new RegistrationSummaryResponse();
}
public function doPayment(DoPaymentRequest $request): DoPaymentResponse
{
// TODO: In einer Folge-Iteration ausformulieren. Default: nichts aktiv anzustoßen (Status offen).
$response = new DoPaymentResponse();
$response->success = true;
$response->status = PaymentStatus::PAYMENT_STATUS_PENDING;
return $response;
}
/**
* Template-Method: Der gemeinsame Rechnungs-Rumpf wird hier gebaut, der variierende Schlusssatz
* kommt aus {@see invoiceClosingStatement()} des jeweiligen Moduls.
*/
public function createInvoice(CreateInvoiceRequest $request): CreateInvoiceResponse
{
$response = new CreateInvoiceResponse();
// TODO: In einer Folge-Iteration den gemeinsamen Rumpf (Betrag, Leistung, Teilnehmerdaten) befüllen
// und an die Invoice-Domain anbinden.
$response->success = true;
$response->closingStatement = $this->invoiceClosingStatement($request);
return $response;
}
/**
* Der je Zahlungsart variierende Schlusssatz der Rechnung
* ("Bitte überweise bis ..." / "wird abgebucht ..." / "wurde per PayPal beglichen").
*/
abstract protected function invoiceClosingStatement(CreateInvoiceRequest $request): string;
}