presseportale/app/Services/Customer/CustomerStatusSummary.php
Kevin Adametz 9ed429fe74 Optimierung 3.4: Performance (Logging terminable, Single-Purchase memoisiert)
- LogApiUsage als terminable Middleware: der ApiUsageLog-INSERT laeuft nach
  gesendeter Antwort und verlaengert nicht mehr die Request-Dauer. Startzeit
  wird ueber ein Request-Attribut uebergeben (terminate() laeuft auf frischer
  Instanz).
- User::openSinglePurchaseCount() memoisiert die offenen Einmalkaeufe pro
  Request. Vorher wurde derselbe grantingSubmission-Count im Status-Widget
  mehrfach (Quota-Total, Quota-Remaining, openSinglePurchases) plus ein
  exists() in hasActiveBooking abgesetzt -> jetzt eine Query.
- CustomerCompanyContext::selectedCompany() nutzt eine einzige first()-Query
  statt exists()+first(); das Verwerfen nicht (mehr) erreichbarer Auswahlen
  bleibt erhalten.
- LegacySubscriptionService::cancelBundleFor() ist ein einziges Bulk-Update
  statt Update-pro-Iteration (UserPaymentOption hat keine Observer/Events).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:05:07 +00:00

120 lines
3.6 KiB
PHP

<?php
namespace App\Services\Customer;
use App\Enums\Tier;
use App\Models\Plan;
use App\Models\User;
/**
* Read-only Statusüberblick für das immer verfügbare Buchungs-Widget im
* Header (aktuelle Buchung, verbleibendes PM-Kontingent, Credit-Guthaben).
*
* Single Source of Truth für die Gating-Logik bleibt das User-Modell; dieses
* DTO bündelt nur die fürs UI nötigen Werte, damit die Blade-Komponente keine
* eigene Geschäftslogik trägt.
*/
class CustomerStatusSummary
{
public function __construct(
public readonly Tier $tier,
public readonly ?string $planName,
public readonly ?string $billingInterval,
public readonly bool $hasActiveBooking,
public readonly bool $hasSubscription,
public readonly bool $hasGrandfathered,
public readonly int $openSinglePurchases,
public readonly bool $unlimitedQuota,
public readonly ?int $quotaTotal,
public readonly ?int $quotaRemaining,
public readonly int $creditBalance,
) {}
public static function fromUser(User $user): self
{
$plan = $user->currentPlan();
$subscription = $user->subscription();
$hasSubscription = (bool) $subscription?->valid();
$billingInterval = null;
if ($plan instanceof Plan && $subscription?->stripe_price) {
$billingInterval = $subscription->stripe_price === $plan->stripe_price_id_yearly
? 'yearly'
: 'monthly';
}
$hasGrandfathered = $user->userPaymentOptions()
->grandfatheredNow()
->exists();
return new self(
tier: $user->currentTier(),
planName: $plan?->name,
billingInterval: $billingInterval,
hasActiveBooking: $user->hasActiveBooking(),
hasSubscription: $hasSubscription,
hasGrandfathered: $hasGrandfathered,
openSinglePurchases: $user->openSinglePurchaseCount(),
unlimitedQuota: $user->hasUnlimitedPressReleaseQuota(),
quotaTotal: $user->pressReleaseQuotaTotal(),
quotaRemaining: $user->pressReleaseQuotaRemaining(),
creditBalance: $user->creditBalance(),
);
}
public function hasCredits(): bool
{
return $this->creditBalance > 0;
}
/**
* Menschlich lesbares Etikett der aktuellen Buchung.
*/
public function bookingLabel(): string
{
if ($this->planName !== null) {
return $this->planName;
}
if ($this->hasGrandfathered) {
return __('Bestandstarif');
}
if ($this->openSinglePurchases > 0) {
return trans_choice(
'{1}:count Einzelbuchung|[2,*]:count Einzelbuchungen',
$this->openSinglePurchases,
['count' => $this->openSinglePurchases],
);
}
return __('Kein Abo');
}
public function billingIntervalLabel(): ?string
{
return match ($this->billingInterval) {
'monthly' => __('monatlich'),
'yearly' => __('jährlich'),
default => null,
};
}
/**
* Kompakter Text für das Trigger-Badge in der Topbar.
*/
public function badgeLabel(): string
{
$parts = [];
$parts[] = $this->unlimitedQuota
? __(':icon PM', ['icon' => '∞'])
: trans_choice('{0}:count PM|{1}:count PM|[2,*]:count PM', (int) $this->quotaRemaining, ['count' => (int) $this->quotaRemaining]);
if ($this->hasCredits()) {
$parts[] = __(':count Cr', ['count' => $this->creditBalance]);
}
return implode(' · ', $parts);
}
}