Statt bis zu 300 s Cache-TTL zu warten, verwirft der BoostService nach der Buchung die Home-/Kategorie-Caches der betroffenen Ausgaben (inkl. Eltern-Rubrik) — die bezahlte Platzierung ist direkt sichtbar. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
123 lines
4.2 KiB
PHP
123 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Services\PressRelease;
|
||
|
||
use App\Enums\Portal;
|
||
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\Cache;
|
||
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);
|
||
|
||
$boost = 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;
|
||
});
|
||
|
||
$this->forgetPlacementCaches($pressRelease);
|
||
|
||
return $boost;
|
||
}
|
||
|
||
/**
|
||
* Gecachte Start-/Branchenseiten der betroffenen Ausgaben verwerfen,
|
||
* damit die bezahlte Platzierung sofort sichtbar wird statt erst nach
|
||
* Ablauf der Cache-TTL (HomePageData/CategoryPageData, 300 s).
|
||
*/
|
||
private function forgetPlacementCaches(PressRelease $pressRelease): void
|
||
{
|
||
$portals = $pressRelease->portal === Portal::Both
|
||
? [Portal::Presseecho->value, Portal::Businessportal24->value]
|
||
: [$pressRelease->portal->value];
|
||
|
||
// Branchenseite der eigenen Rubrik plus Eltern-Rubrik (deren Seite
|
||
// schließt Unterkategorien mit ein), je hinterlegter Sprache.
|
||
$category = $pressRelease->category()
|
||
->with(['translations', 'parent.translations'])
|
||
->first();
|
||
$translations = collect($category?->translations ?? [])
|
||
->merge($category?->parent?->translations ?? []);
|
||
|
||
foreach ($portals as $portal) {
|
||
Cache::forget(sprintf('web.home.%s.%s', $portal, $pressRelease->language));
|
||
|
||
foreach ($translations as $translation) {
|
||
if (filled($translation->slug)) {
|
||
Cache::forget(sprintf('web.category.%s.%s.%s', $portal, $translation->locale, $translation->slug));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|