presseportale/app/Services/PressRelease/BoostService.php
Kevin Adametz c7a4c8bfd4 Boost-Geschaeftslogik (Decision-Update 2.2)
Bezahlte Platzierung gruener, veroeffentlichter PMs ueber die Credit-Wallet:

- boosts Tabelle (Zeitraum starts_at/ends_at, days, credits_charged)
- BoostService: Gate (nur Published + Green), Preis nach Laufzeit
  (7/14/30 -> 12/20/35 Credits), Mehrfachkauf verlaengert vom laufenden
  Ende, Wallet-Belastung referenziert den Boost im Ledger
- PressRelease::boosts()/isBoosted()/scopeBoosted() als Basis fuer die
  Featured-Platzierung (Frontend-Anbindung bleibt der Web-Strecke ueberlassen)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 14:22:12 +00:00

87 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services\PressRelease;
use App\Enums\PressReleaseClassification;
use App\Enums\PressReleaseStatus;
use App\Exceptions\BoostNotAllowedException;
use App\Models\Boost;
use App\Models\PressRelease;
use App\Models\User;
use App\Services\Billing\CreditPricingService;
use App\Services\Billing\CreditWalletService;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
/**
* Boost-Kauf laut Decision-Update §2.2. Gate: nur veröffentlichte, grüne PMs.
* Bezahlung über die Credit-Wallet, Preis nach Laufzeit (7/14/30 Tage).
* Mehrfachkauf verlängert: der neue Zeitraum schließt an ein laufendes Ende an.
*/
class BoostService
{
public function __construct(
private readonly CreditPricingService $pricing,
private readonly CreditWalletService $wallet,
) {}
/**
* Boostbar = veröffentlicht UND grün klassifiziert (gelb/rot nicht).
*/
public function canBoost(PressRelease $pressRelease): bool
{
return $pressRelease->status === PressReleaseStatus::Published
&& $pressRelease->classification === PressReleaseClassification::Green;
}
public function isBoosted(PressRelease $pressRelease): bool
{
return $pressRelease->boosts()->active()->exists();
}
/**
* Ende des aktuell laufenden Boosts (das späteste zukünftige `ends_at`)
* oder null, wenn die PM gerade nicht geboostet ist.
*/
public function activeUntil(PressRelease $pressRelease): ?Carbon
{
$endsAt = $pressRelease->boosts()->active()->max('ends_at');
return $endsAt ? Carbon::parse($endsAt) : null;
}
/**
* Bucht einen Boost. Wirft BoostNotAllowedException am Gate und
* InvalidArgumentException bei unbekannter Laufzeit; die Wallet-Belastung
* wirft InsufficientCreditsException, falls das Guthaben nicht reicht.
*/
public function boost(User $user, PressRelease $pressRelease, int $days): Boost
{
if (! $this->canBoost($pressRelease)) {
throw BoostNotAllowedException::notBoostable();
}
$credits = $this->pricing->boostCredits($days);
return DB::transaction(function () use ($user, $pressRelease, $days, $credits): Boost {
$startsAt = $this->activeUntil($pressRelease) ?? now();
$boost = $pressRelease->boosts()->create([
'user_id' => $user->id,
'days' => $days,
'credits_charged' => $credits,
'starts_at' => $startsAt,
'ends_at' => $startsAt->copy()->addDays($days),
]);
$this->wallet->debit(
$user,
$credits,
"Boost {$days} Tage PM #{$pressRelease->id}",
$boost,
);
return $boost;
});
}
}